]> Raphaël G. Git Repositories - blogbundle/commitdiff
New context layout
authorRaphaël Gertz <git@rapsys.eu>
Tue, 14 Oct 2025 10:12:11 +0000 (12:12 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Tue, 14 Oct 2025 10:12:11 +0000 (12:12 +0200)
Controller/AbstractController.php

index 10353cbf421db5281fb3189231f89e4745624b03..2efdd34244d3bf009e653bfdec0c1eebfca03937 100644 (file)
@@ -294,36 +294,8 @@ abstract class AbstractController extends BaseAbstractController implements Serv
 
                //Set the context
                $this->context = [
-                       'head' => [
-                               'alternates' => $alternates,
-                               'canonical' => $canonical,
-                               'icon' => $this->config['icon'],
-                               'keywords' => null,
-                               'locale' => str_replace('_', '-', $this->locale),
-                               'logo' => [
-                                       'png' => $this->config['logo']['png'],
-                                       'svg' => $this->config['logo']['svg'],
-                                       'alt' => $this->translator->trans($this->config['logo']['alt'])
-                               ],
-                               'root' => $this->config['root'],
-                               'site' => $this->translator->trans($this->config['title']),
-                               'title' => null,
-                               'facebook' => [
-                                               'og:type' => 'article',
-                                               'og:site_name' => $this->translator->trans($this->config['title']),
-                                               'og:url' => $canonical,
-                                               //TODO: review this value
-                                               'fb:app_id' => $this->config['facebook']['apps']
-                               ],
-                               'fbimage' => [
-                                       'texts' => [
-                                               $this->translator->trans($this->config['title']) => [
-                                                       'font' => 'irishgrover',
-                                                       'size' => 110
-                                               ]
-                                       ]
-                               ]
-                       ],
+                       'alternates' => $alternates,
+                       'canonical' => $canonical,
                        'contact' => [
                                'address' => $this->config['contact']['address'],
                                'name' => $this->translator->trans($this->config['contact']['name'])
@@ -335,10 +307,42 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                'short' => $this->translator->trans($this->config['copy']['short']),
                                'title' => $this->config['copy']['title']
                        ],
-                       'forms' => [],
                        'description' => null,
-                       'section' => null,
-                       'title' => null
+                       'donate' => $this->config['donate'],
+                       'facebook' => [
+                               'og:type' => 'article',
+                               'og:site_name' => $title = $this->translator->trans($this->config['title']),
+                               'og:url' => $canonical,
+                               #'fb:admins' => $this->config['facebook']['admins'],
+                               'fb:app_id' => $this->config['facebook']['apps']
+                       ],
+                       //XXX: TODO: only generate it when fb robot request the url ???
+                       'fbimage' => [
+                               'texts' => [
+                                       $title => [
+                                               //'font' => 'irishgrover',
+                                               //'size' => 110
+                                       ]
+                               ]
+                       ],
+                       'forms' => [],
+                       'icon' => $this->config['icon'],
+                       'keywords' => null,
+                       'locale' => str_replace('_', '-', $this->locale),
+                       #'logo' => [
+                       #       'png' => $this->config['logo']['png'],
+                       #       'svg' => $this->config['logo']['svg'],
+                       #       'alt' => $this->translator->trans($this->config['logo']['alt'])
+                       #],
+                       'logo' => $this->config['logo'],
+                       'next' => null,
+                       'prev' => null,
+                       'root' => $this->router->generate($this->config['root']),
+                       'title' => [
+                               'page' => null,
+                               'section' => null,
+                               'site' => $title
+                       ]
                ];
        }
 
@@ -352,7 +356,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                $response ??= new Response();
 
                //Without alternates
-               if (empty($parameters['head']['alternates'])) {
+               if (count($parameters['alternates']) <= 1) {
                        //Iterate on locales excluding current one
                        foreach($this->config['locales'] as $locale) {
                                //Set routeParams
@@ -377,7 +381,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                        }
 
                                        //Set locale locales context
-                                       $parameters['head']['alternates'][str_replace('_', '-', $locale)] = [
+                                       $parameters['alternates'][str_replace('_', '-', $locale)] = [
                                                'absolute' => $this->router->generate($this->route, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL),
                                                'relative' => $this->router->generate($this->route, $routeParams),
                                                'title' => implode('/', $titles),
@@ -385,103 +389,73 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                        ];
 
                                        //Add shorter locale
-                                       if (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
+                                       if (empty($parameters['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
                                                //Set locale locales context
-                                               $parameters['head']['alternates'][$shortCurrent] = $parameters['head']['alternates'][str_replace('_', '-', $locale)];
+                                               $parameters['alternates'][$shortCurrent] = $parameters['alternates'][str_replace('_', '-', $locale)];
                                        }
-                               //Add shorter locale
-                               } elseif (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
-                                       //Set titles
-                                       $titles = [];
-
-                                       //Set route params locale
-                                       $routeParams['_locale'] = $locale;
-
-                                       //Iterate on other locales
-                                       foreach(array_diff($this->config['locales'], [$locale]) as $other) {
-                                               //Set other locale title
-                                               $titles[$other] = $this->translator->trans($this->config['languages'][$locale], [], null, $other);
-                                       }
-
-                                       //Set locale locales context
-                                       $parameters['head']['alternates'][$shortCurrent] = [
-                                               'absolute' => $this->router->generate($this->route, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL),
-                                               'relative' => $this->router->generate($this->route, $routeParams),
-                                               'title' => implode('/', $titles),
-                                               'translated' => $this->translator->trans($this->config['languages'][$locale], [], null, $locale)
-                                       ];
                                }
                        }
                }
 
-               //With empty head title and section
-               if (empty($parameters['head']['title']) && !empty($parameters['section'])) {
-                       //Set head title
-                       $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['section'], $parameters['head']['site']]);
-               //With empty head title
-               } elseif (empty($parameters['head']['title'])) {
-                       //Set head title
-                       $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['head']['site']]);
-               }
-
-               //With empty head description and description
-               if (empty($parameters['head']['description']) && !empty($parameters['description'])) {
-                       //Set head description
-                       $parameters['head']['description'] = $parameters['description'];
+               //With canonical
+               if (!empty($parameters['canonical'])) {
+                       //Set facebook url
+                       $parameters['facebook']['og:url'] = $parameters['canonical'];
                }
 
                //With empty facebook title and title
-               if (empty($parameters['head']['facebook']['og:title']) && !empty($parameters['title'])) {
+               if (empty($parameters['facebook']['og:title']) && !empty($parameters['title']['page'])) {
                        //Set facebook title
-                       $parameters['head']['facebook']['og:title'] = $parameters['title'];
+                       $parameters['facebook']['og:title'] = $parameters['title']['page'];
                }
 
                //With empty facebook description and description
-               if (empty($parameters['head']['facebook']['og:description']) && !empty($parameters['description'])) {
+               if (empty($parameters['facebook']['og:description']) && !empty($parameters['description'])) {
                        //Set facebook description
-                       $parameters['head']['facebook']['og:description'] = $parameters['description'];
+                       $parameters['facebook']['og:description'] = $parameters['description'];
                }
 
                //With locale
                if (!empty($this->locale)) {
                        //Set facebook locale
-                       $parameters['head']['facebook']['og:locale'] = $this->locale;
+                       $parameters['facebook']['og:locale'] = $this->locale;
 
                        //With alternates
                        //XXX: locale change when fb_locale=xx_xx is provided is done in FacebookSubscriber
                        //XXX: see https://stackoverflow.com/questions/20827882/in-open-graph-markup-whats-the-use-of-oglocalealternate-without-the-locati
-                       if (!empty($parameters['head']['alternates'])) {
+                       if (!empty($parameters['alternates'])) {
                                //Iterate on alternates
-                               foreach($parameters['head']['alternates'] as $lang => $alternate) {
+                               foreach($parameters['alternates'] as $lang => $alternate) {
                                        if (strlen($lang) == 5) {
                                                //Set facebook locale alternate
-                                               $parameters['head']['facebook']['og:locale:alternate'] = str_replace('-', '_', $lang);
+                                               $parameters['facebook']['og:locale:alternate'] = str_replace('-', '_', $lang);
                                        }
                                }
                        }
                }
 
+               //Without facebook image defined and texts
+               if (empty($parameters['facebook']['og:image']) && !empty($this->request) && !empty($parameters['fbimage']['texts']) && !empty($this->modified)) {
+                       //Get facebook image
+                       //XXX: decode getPathInfo (see https://github.com/symfony/symfony/issues/2579)
+                       $parameters['facebook'] += $this->image->getFacebook(urldecode($this->request->getPathInfo()), $parameters['fbimage']['texts'], $this->modified->getTimestamp());
+               }
+
                //With count
                if (!empty($this->count)) {
                        //With prev link
                        if ($this->page > 0) {
                                //Set head prev
-                               $parameters['head']['prev'] = $this->generateUrl($this->request->get('_route'), ['page' => $this->page - 1]+$this->request->get('_route_params'));
+                               $parameters['prev'] = $this->generateUrl($this->request->get('_route'), ['page' => $this->page - 1]+$this->request->get('_route_params'));
                        }
 
                        //With next link
                        if ($this->count > ($this->page + 1) * $this->limit) {
                                //Set head next
-                               $parameters['head']['next'] = $this->generateUrl($this->request->get('_route'), ['page' => $this->page + 1]+$this->request->get('_route_params'));
+                               $parameters['next'] = $this->generateUrl($this->request->get('_route'), ['page' => $this->page + 1]+$this->request->get('_route_params'));
                        }
                }
 
-               //Without facebook image defined and texts
-               if (empty($parameters['head']['facebook']['og:image']) && !empty($this->request) && !empty($parameters['head']['fbimage']['texts']) && !empty($this->modified)) {
-                       //Get facebook image
-                       $parameters['head']['facebook'] += $this->facebook->getImage($this->request->getPathInfo(), $parameters['head']['fbimage']['texts'], $this->modified->getTimestamp());
-               }
-
                //Call twig render method
                $content = $this->twig->render($view, $parameters);