<?php
namespace WAM\Bundle\BlockBundle\Twig;
use Twig\Environment;
use WAM\Bundle\BlockBundle\Entity\AbstractBlock;
use WAM\Bundle\BlockBundle\Exception\BlockNotFoundException;
use WAM\Bundle\BlockBundle\Factory\Interfaces\BlockRenderFactoryInterface;
use WAM\Bundle\BlockBundle\Services\BlockManagerService;
use WAM\Bundle\BlockBundle\Services\Interfaces\AttributeResolverInterface;
/**
* Render block and collection of blocks.
*
* @author Edgar Tébar <etebar@wearemarketing.com>
*/
class BlockExtension extends \Twig_Extension
{
private $blockManagerService;
private $blockRenderFactory;
private $attributeResolver;
public function __construct(
BlockManagerService $blockManagerService,
BlockRenderFactoryInterface $blockRenderFactory,
AttributeResolverInterface $attributeResolver
) {
$this->blockManagerService = $blockManagerService;
$this->blockRenderFactory = $blockRenderFactory;
$this->attributeResolver = $attributeResolver;
}
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('render_block', array($this, 'renderBlock'), array('is_safe' => array('html'), 'needs_environment' => true)),
new \Twig_SimpleFunction('render_url', array($this, 'renderAllBlocksFromUrl'), array('is_safe' => array('html'), 'needs_environment' => true)),
new \Twig_SimpleFunction('render_block_blocks', array($this, 'renderAllBlocksFromBlock'), array('is_safe' => array('html'), 'needs_environment' => true)),
new \Twig_SimpleFunction('get_block', array($this, 'getBlock'), array('is_safe' => array('html'))),
new \Twig_SimpleFunction('get_objects', array($this, 'getObjects'), array('is_safe' => array('html'))),
new \Twig_SimpleFunction('resolve_attribute_value', array($this, 'resolveAttributeValue')),
);
}
public function renderBlock(Environment $environment, $canonicalName, $template = null, $parameters = array())
{
try {
$block = $this->blockManagerService->getBlock($canonicalName);
if ($block->isHidden()) {
return '';
}
$blockRender = $this->blockRenderFactory->create(
$block,
$template,
$parameters
);
} catch (BlockNotFoundException $exception) {
return '';
}
return $environment->render($blockRender->getTemplate(), $blockRender->getParameters());
}
public function renderAllBlocksFromUrl(Environment $environment, $url)
{
$content = '';
$blockRenders = $this->blockRenderFactory->createAllFromUrl($url);
foreach ($blockRenders as $blockRender) {
$content .= $environment->render($blockRender->getTemplate(), $blockRender->getParameters());
}
return $content;
}
public function renderAllBlocksFromBlock(Environment $environment, AbstractBlock $block)
{
$content = '';
$blockRenders = $this->blockRenderFactory->createAllFromBlock($block);
foreach ($blockRenders as $blockRender) {
$content .= $environment->render($blockRender->getTemplate(), $blockRender->getParameters());
}
return $content;
}
public function getBlock($canonicalName)
{
try {
$block = $this->blockManagerService->getBlock($canonicalName);
} catch (BlockNotFoundException $exception) {
return null;
}
return $block;
}
public function getObjects($block, $additionalCriteria = [])
{
return $this->blockManagerService->getObjects($block, $additionalCriteria);
}
public function resolveAttributeValue($block, $attribute, $default = null, $parent = null)
{
return $this->attributeResolver->resolveAttributeValue($block, $attribute, $default, $parent);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'wam.block.twig.extension';
}
}