vendor/sonata-project/seo-bundle/src/Event/BreadcrumbListener.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <[email protected]>
  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 Sonata\SeoBundle\Event;
  12. use Sonata\BlockBundle\Block\BlockServiceInterface;
  13. use Sonata\BlockBundle\Event\BlockEvent;
  14. use Sonata\BlockBundle\Model\Block;
  15. /**
  16.  * BreadcrumbListener for Block Event.
  17.  *
  18.  * @author Sylvain Deloux <[email protected]>
  19.  */
  20. class BreadcrumbListener
  21. {
  22.     /**
  23.      * @var array
  24.      */
  25.     protected $blockServices = [];
  26.     /**
  27.      * Add a renderer to the status services list.
  28.      *
  29.      * @param string $type
  30.      */
  31.     public function addBlockService($typeBlockServiceInterface $blockService)
  32.     {
  33.         $this->blockServices[$type] = $blockService;
  34.     }
  35.     /**
  36.      * Add context related BlockService, if found.
  37.      */
  38.     public function onBlock(BlockEvent $event)
  39.     {
  40.         $context $event->getSetting('context'null);
  41.         if (null === $context) {
  42.             return;
  43.         }
  44.         foreach ($this->blockServices as $type => $blockService) {
  45.             if ($blockService->handleContext($context)) {
  46.                 $block = new Block();
  47.                 $block->setId(uniqid());
  48.                 $block->setSettings($event->getSettings());
  49.                 $block->setType($type);
  50.                 $event->addBlock($block);
  51.                 return;
  52.             }
  53.         }
  54.     }
  55. }