<?php
namespace Boab\CmsBundle\EventListener;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Sonata\SeoBundle\Seo\SeoPageInterface;
use Boab\CmsBundle\Event\PageRenderEvent;
use Boab\CmsBundle\Events;
use Symfony\Component\Asset\Packages;
class SeoListener implements EventSubscriberInterface
{
private $seoPage;
private $router;
private $asset;
public function __construct(SeoPageInterface $seoPage, RouterInterface $router, Packages $asset)
{
$this->seoPage = $seoPage;
$this->router = $router;
$this->asset = $asset;
}
public function onPageRender(PageRenderEvent $event)
{
$node = $event->getNode();
$request = $event->getRequest();
$this->seoPage
->setTitle($node->getTitle())
//->addMeta('name', 'keywords', $node->getMetaKeywords())
->addMeta('name', 'description', $node->getSummary())
->addMeta('name', 'author', $node->getMetaAuthor())
->addMeta('property', 'og:title', $node->getTitle())
->addMeta('property', 'og:image', $this->getThumbnailUrl($request, $node))
->addMeta('property', 'og:type', 'blog')
->addMeta('property', 'og:description', $node->getSummary());
//->addMeta('property', 'og:url', $this->generateUrl('sonata_news_view', [], true));
}
private function getThumbnailUrl($request, $node)
{
return $request->getSchemeAndHttpHost().$this->asset->getUrl($node->getMetaThumbnail());
}
public static function getSubscribedEvents():array
{
return [
Events::PAGE_RENDER => ['onPageRender']
];
}
}