vendor/wearemarketing/blockbundle/Twig/BlockExtension.php line 47

Open in your IDE?
  1. <?php
  2. namespace WAM\Bundle\BlockBundle\Twig;
  3. use Twig\Environment;
  4. use WAM\Bundle\BlockBundle\Entity\AbstractBlock;
  5. use WAM\Bundle\BlockBundle\Exception\BlockNotFoundException;
  6. use WAM\Bundle\BlockBundle\Factory\Interfaces\BlockRenderFactoryInterface;
  7. use WAM\Bundle\BlockBundle\Services\BlockManagerService;
  8. use WAM\Bundle\BlockBundle\Services\Interfaces\AttributeResolverInterface;
  9. /**
  10.  * Render block and collection of blocks.
  11.  *
  12.  * @author Edgar Tébar <etebar@wearemarketing.com>
  13.  */
  14. class BlockExtension extends \Twig_Extension
  15. {
  16.     private $blockManagerService;
  17.     private $blockRenderFactory;
  18.     private $attributeResolver;
  19.     public function __construct(
  20.         BlockManagerService $blockManagerService,
  21.         BlockRenderFactoryInterface $blockRenderFactory,
  22.         AttributeResolverInterface $attributeResolver
  23.     ) {
  24.         $this->blockManagerService $blockManagerService;
  25.         $this->blockRenderFactory $blockRenderFactory;
  26.         $this->attributeResolver $attributeResolver;
  27.     }
  28.     public function getFunctions()
  29.     {
  30.         return array(
  31.             new \Twig_SimpleFunction('render_block', array($this'renderBlock'), array('is_safe' => array('html'), 'needs_environment' => true)),
  32.             new \Twig_SimpleFunction('render_url', array($this'renderAllBlocksFromUrl'), array('is_safe' => array('html'), 'needs_environment' => true)),
  33.             new \Twig_SimpleFunction('render_block_blocks', array($this'renderAllBlocksFromBlock'), array('is_safe' => array('html'), 'needs_environment' => true)),
  34.             new \Twig_SimpleFunction('get_block', array($this'getBlock'), array('is_safe' => array('html'))),
  35.             new \Twig_SimpleFunction('get_objects', array($this'getObjects'), array('is_safe' => array('html'))),
  36.             new \Twig_SimpleFunction('resolve_attribute_value', array($this'resolveAttributeValue')),
  37.         );
  38.     }
  39.     public function renderBlock(Environment $environment$canonicalName$template null$parameters = array())
  40.     {
  41.         try {
  42.             $block $this->blockManagerService->getBlock($canonicalName);
  43.             if ($block->isHidden()) {
  44.                 return '';
  45.             }
  46.             $blockRender $this->blockRenderFactory->create(
  47.                 $block,
  48.                 $template,
  49.                 $parameters
  50.             );
  51.         } catch (BlockNotFoundException $exception) {
  52.             return '';
  53.         }
  54.         return $environment->render($blockRender->getTemplate(), $blockRender->getParameters());
  55.     }
  56.     public function renderAllBlocksFromUrl(Environment $environment$url)
  57.     {
  58.         $content '';
  59.         $blockRenders $this->blockRenderFactory->createAllFromUrl($url);
  60.         foreach ($blockRenders as $blockRender) {
  61.             $content .= $environment->render($blockRender->getTemplate(), $blockRender->getParameters());
  62.         }
  63.         return $content;
  64.     }
  65.     public function renderAllBlocksFromBlock(Environment $environmentAbstractBlock $block)
  66.     {
  67.         $content '';
  68.         $blockRenders $this->blockRenderFactory->createAllFromBlock($block);
  69.         foreach ($blockRenders as $blockRender) {
  70.             $content .= $environment->render($blockRender->getTemplate(), $blockRender->getParameters());
  71.         }
  72.         return $content;
  73.     }
  74.     public function getBlock($canonicalName)
  75.     {
  76.         try {
  77.             $block $this->blockManagerService->getBlock($canonicalName);
  78.         } catch (BlockNotFoundException $exception) {
  79.             return null;
  80.         }
  81.         return $block;
  82.     }
  83.     public function getObjects($block$additionalCriteria = [])
  84.     {
  85.         return $this->blockManagerService->getObjects($block$additionalCriteria);
  86.     }
  87.     public function resolveAttributeValue($block$attribute$default null$parent null)
  88.     {
  89.         return $this->attributeResolver->resolveAttributeValue($block$attribute$default$parent);
  90.     }
  91.     /**
  92.      * Returns the name of the extension.
  93.      *
  94.      * @return string The extension name
  95.      */
  96.     public function getName()
  97.     {
  98.         return 'wam.block.twig.extension';
  99.     }
  100. }