lib/boab/cms-bundle/src/EventListener/SeoListener.php line 25

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\EventListener;
  3. use Symfony\Component\Routing\RouterInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Sonata\SeoBundle\Seo\SeoPageInterface;
  6. use Boab\CmsBundle\Event\PageRenderEvent;
  7. use Boab\CmsBundle\Events;
  8. use Symfony\Component\Asset\Packages;
  9. class SeoListener implements EventSubscriberInterface
  10. {
  11.     private $seoPage;
  12.     private $router;
  13.     private $asset;
  14.     public function __construct(SeoPageInterface $seoPageRouterInterface $routerPackages $asset)
  15.     {
  16.         $this->seoPage $seoPage;
  17.         $this->router $router;
  18.         $this->asset $asset;
  19.     }
  20.     public function onPageRender(PageRenderEvent $event)
  21.     {
  22.         $node $event->getNode();
  23.         $request $event->getRequest();
  24.         $this->seoPage
  25.             ->setTitle($node->getTitle())
  26.             //->addMeta('name', 'keywords', $node->getMetaKeywords())
  27.             ->addMeta('name''description'$node->getSummary())
  28.             ->addMeta('name''author'$node->getMetaAuthor())
  29.             ->addMeta('property''og:title'$node->getTitle())
  30.             ->addMeta('property''og:image'$this->getThumbnailUrl($request$node))
  31.             ->addMeta('property''og:type''blog')
  32.             ->addMeta('property''og:description'$node->getSummary());   
  33.             //->addMeta('property', 'og:url',  $this->generateUrl('sonata_news_view', [], true));    
  34.     }
  35.     private function getThumbnailUrl($request$node)
  36.     {
  37.         return $request->getSchemeAndHttpHost().$this->asset->getUrl($node->getMetaThumbnail());
  38.     }
  39.     public static function getSubscribedEvents():array
  40.     {
  41.         return [
  42.            Events::PAGE_RENDER => ['onPageRender']
  43.         ];
  44.     }    
  45. }