local/modules/CanonicalUrl/Hook/MetaHook.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace CanonicalUrl\Hook;
  12. use CanonicalUrl\Event\CanonicalUrlEvent;
  13. use CanonicalUrl\Event\CanonicalUrlEvents;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Thelia\Core\Event\Hook\HookRenderEvent;
  16. use Thelia\Core\Hook\BaseHook;
  17. /**
  18.  * Class MetaHook.
  19.  *
  20.  * @author Gilles Bourgeat <gilles.bourgeat@gmail.com>
  21.  */
  22. class MetaHook extends BaseHook
  23. {
  24.     /** @var EventDispatcherInterface */
  25.     protected $eventDispatcher;
  26.     public function __construct(EventDispatcherInterface $eventDispatcher)
  27.     {
  28.         $this->eventDispatcher $eventDispatcher;
  29.     }
  30.     public function onMainHeadBottom(HookRenderEvent $hookRender): void
  31.     {
  32.         $event = new CanonicalUrlEvent();
  33.         $this->eventDispatcher->dispatch(
  34.             $event,
  35.             CanonicalUrlEvents::GENERATE_CANONICAL,
  36.         );
  37.         if ($event->getUrl()) {
  38.             $hookRender->add('<link rel="canonical" href="'.$event->getUrl().'" />');
  39.         }
  40.     }
  41. }