vendor/nambu-private/pimcore-base/src/BaseBundle/Event/ControllerInitEvent.php line 30

Open in your IDE?
  1. <?php
  2. namespace BaseBundle\Event;
  3. use BaseBundle\Service\TwigLocaleExtension;
  4. use Pimcore\Config;
  5. use Pimcore\Controller\FrontendController;
  6. use Pimcore\Model\Asset\Image;
  7. use Pimcore\Model\Document;
  8. use Pimcore\Model\Site;
  9. use Pimcore\Model\WebsiteSetting;
  10. use Pimcore\Tool;
  11. use Pimcore\Twig\Extension\Templating\HeadLink;
  12. use Pimcore\Twig\Extension\Templating\HeadMeta;
  13. use Pimcore\Twig\Extension\Templating\HeadScript;
  14. use Pimcore\Twig\Extension\Templating\HeadTitle;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  17. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  18. class ControllerInitEvent {
  19.     public function __construct(private HeadTitle           $headTitle,
  20.                                 private HeadMeta            $headMeta,
  21.                                 private HeadLink            $headLink,
  22.                                 private HeadScript          $headScript,
  23.                                 private Config              $config,
  24.                                 private TwigLocaleExtension $localeExtension) {
  25.     }
  26.     public function onKernelController(ControllerEvent $e) {
  27.         if ($e->isMasterRequest() && \Pimcore\Tool::isFrontend()) {
  28.             $controller $e->getController();
  29.             if (is_array($controller)) $controller array_shift($controller);
  30.             $locale $e->getRequest()->getLocale();
  31.             $this->addPimcoreTranslationsJavascript($locale);
  32.             $this->addGoogleMapsApi();
  33.             if ($controller instanceof FrontendController) {
  34.                 $siteId Site::isSiteRequest() ? Site::getCurrentSite()->getId() : null;
  35.                 $document $controller->document;
  36.                 $pathInfo $e->getRequest()->getPathInfo();
  37.                 //
  38.                 $this->addCanonicalTag($e->getRequest());
  39.                 $this->addHreflangTags($e->getRequest(), $document);
  40.                 $this->addHeadMetaTags($pathInfo$document$siteId$locale);
  41.             }
  42.         }
  43.     }
  44.     private function addHreflangTags(Request $requestDocument $document): void {
  45.         if (count(\Pimcore\Tool::getValidLanguages()) > 1) {
  46.             foreach ($this->localeExtension->getSiteLocales($document$request) as $key => $language) {
  47.                 $this->headLink->append($this->headLink->createData([
  48.                     "rel"      => "alternate",
  49.                     "hreflang" => $key,
  50.                     "href"     => $request->getScheme().'://'.$request->getHttpHost().$language["href"],
  51.                 ]));
  52.             }
  53.         }
  54.     }
  55.     private function addCanonicalTag(Request $request): void {
  56.         $href $request->getScheme().'://'.$request->getHttpHost().$request->getPathInfo();
  57.         $this->headLink->append($this->headLink->createData([
  58.             "rel"  => "canonical",
  59.             "href" => $href,
  60.         ]));
  61.     }
  62.     private function addHeadMetaTags($pathInfo, ?Document $document, ?int $siteId, ?string $locale): void {
  63.         if (!$document instanceof Document\Page) return;
  64.         if ($document->getTitle()) {
  65.             $this->headTitle->set($document->getTitle());
  66.         }
  67.         $websiteTitleSetting WebsiteSetting::getByName('websiteTitle'$siteId$locale);
  68.         if ($websiteTitleSetting instanceof WebsiteSetting) {
  69.             $this->headTitle->append($websiteTitleSetting->getData());
  70.         }
  71.         $this->headTitle->setSeparator(" : ");
  72.         //
  73.         if ($document->getDescription()) {
  74.             $this->headMeta->setDescription($document->getDescription());
  75.             $this->headMeta->appendProperty("og:description"$document->getDescription());
  76.         }
  77.         $this->headMeta->appendProperty("og:type""website");
  78.         $this->headMeta->appendProperty("og:url"\Pimcore\Tool::getHostUrl().$pathInfo);
  79.         $ogimage null;
  80.         if ($document->hasProperty("ogimage")) {
  81.             $ogimage $document->getProperty("ogimage");
  82.         } else {
  83.             $ogsetting WebsiteSetting::getByName('ogimage'$siteId$locale);
  84.             if ($ogsetting instanceof WebsiteSetting) {
  85.                 $ogimage $ogsetting->getData();
  86.             }
  87.         }
  88.         if ($ogimage instanceof Image) {
  89.             $this->headMeta->appendProperty("og:image"Tool::getHostUrl().$ogimage->getFullPath());
  90.             $this->headMeta->appendProperty("og:image:width"$ogimage->getWidth());
  91.             $this->headMeta->appendProperty("og:image:height"$ogimage->getHeight());
  92.         }
  93.     }
  94.     private function addPimcoreTranslationsJavascript(string $locale): void {
  95.         if (count(\Pimcore\Tool::getValidLanguages()) > 1) {
  96.             \BaseBundle\Service\TranslationService::generateJsFiles();
  97.             $this->headScript->appendFile('/bundles/base/js/class.translation.js');
  98.             if (is_file(PIMCORE_WEB_ROOT.'/var/tmp/translation-'.$locale.'.js')) {
  99.                 $this->headScript->appendFile('/var/tmp/translation-'.$locale.'.js');
  100.             }
  101.         }
  102.     }
  103.     private function addGoogleMapsApi(): void {
  104.         $key $this->config['services']['google']['browser_api_key'];
  105.         if (!empty($key)) {
  106.             $this->headMeta->appendProperty("googlemaps:key"$key);
  107.         }
  108.     }
  109. }