<?php
namespace BaseBundle\Event;
use BaseBundle\Service\TwigLocaleExtension;
use Pimcore\Config;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\Document;
use Pimcore\Model\Site;
use Pimcore\Model\WebsiteSetting;
use Pimcore\Tool;
use Pimcore\Twig\Extension\Templating\HeadLink;
use Pimcore\Twig\Extension\Templating\HeadMeta;
use Pimcore\Twig\Extension\Templating\HeadScript;
use Pimcore\Twig\Extension\Templating\HeadTitle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class ControllerInitEvent {
public function __construct(private HeadTitle $headTitle,
private HeadMeta $headMeta,
private HeadLink $headLink,
private HeadScript $headScript,
private Config $config,
private TwigLocaleExtension $localeExtension) {
}
public function onKernelController(ControllerEvent $e) {
if ($e->isMasterRequest() && \Pimcore\Tool::isFrontend()) {
$controller = $e->getController();
if (is_array($controller)) $controller = array_shift($controller);
$locale = $e->getRequest()->getLocale();
$this->addPimcoreTranslationsJavascript($locale);
$this->addGoogleMapsApi();
if ($controller instanceof FrontendController) {
$siteId = Site::isSiteRequest() ? Site::getCurrentSite()->getId() : null;
$document = $controller->document;
$pathInfo = $e->getRequest()->getPathInfo();
//
$this->addCanonicalTag($e->getRequest());
$this->addHreflangTags($e->getRequest(), $document);
$this->addHeadMetaTags($pathInfo, $document, $siteId, $locale);
}
}
}
private function addHreflangTags(Request $request, Document $document): void {
if (count(\Pimcore\Tool::getValidLanguages()) > 1) {
foreach ($this->localeExtension->getSiteLocales($document, $request) as $key => $language) {
$this->headLink->append($this->headLink->createData([
"rel" => "alternate",
"hreflang" => $key,
"href" => $request->getScheme().'://'.$request->getHttpHost().$language["href"],
]));
}
}
}
private function addCanonicalTag(Request $request): void {
$href = $request->getScheme().'://'.$request->getHttpHost().$request->getPathInfo();
$this->headLink->append($this->headLink->createData([
"rel" => "canonical",
"href" => $href,
]));
}
private function addHeadMetaTags($pathInfo, ?Document $document, ?int $siteId, ?string $locale): void {
if (!$document instanceof Document\Page) return;
if ($document->getTitle()) {
$this->headTitle->set($document->getTitle());
}
$websiteTitleSetting = WebsiteSetting::getByName('websiteTitle', $siteId, $locale);
if ($websiteTitleSetting instanceof WebsiteSetting) {
$this->headTitle->append($websiteTitleSetting->getData());
}
$this->headTitle->setSeparator(" : ");
//
if ($document->getDescription()) {
$this->headMeta->setDescription($document->getDescription());
$this->headMeta->appendProperty("og:description", $document->getDescription());
}
$this->headMeta->appendProperty("og:type", "website");
$this->headMeta->appendProperty("og:url", \Pimcore\Tool::getHostUrl().$pathInfo);
$ogimage = null;
if ($document->hasProperty("ogimage")) {
$ogimage = $document->getProperty("ogimage");
} else {
$ogsetting = WebsiteSetting::getByName('ogimage', $siteId, $locale);
if ($ogsetting instanceof WebsiteSetting) {
$ogimage = $ogsetting->getData();
}
}
if ($ogimage instanceof Image) {
$this->headMeta->appendProperty("og:image", Tool::getHostUrl().$ogimage->getFullPath());
$this->headMeta->appendProperty("og:image:width", $ogimage->getWidth());
$this->headMeta->appendProperty("og:image:height", $ogimage->getHeight());
}
}
private function addPimcoreTranslationsJavascript(string $locale): void {
if (count(\Pimcore\Tool::getValidLanguages()) > 1) {
\BaseBundle\Service\TranslationService::generateJsFiles();
$this->headScript->appendFile('/bundles/base/js/class.translation.js');
if (is_file(PIMCORE_WEB_ROOT.'/var/tmp/translation-'.$locale.'.js')) {
$this->headScript->appendFile('/var/tmp/translation-'.$locale.'.js');
}
}
}
private function addGoogleMapsApi(): void {
$key = $this->config['services']['google']['browser_api_key'];
if (!empty($key)) {
$this->headMeta->appendProperty("googlemaps:key", $key);
}
}
}