|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Twig\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CMSBundle\Entity\Block; |
|
6
|
|
|
use Alpixel\Bundle\CMSBundle\Helper\BlockHelper; |
|
7
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
9
|
|
|
|
|
10
|
|
|
class BlockExtension extends \Twig_Extension |
|
11
|
|
|
{ |
|
12
|
|
|
private $blockHelper; |
|
13
|
|
|
private $container; |
|
14
|
|
|
private $blockConfiguration; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(BlockHelper $blockHelper, $container, $blockConfiguration) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->blockHelper = $blockHelper; |
|
19
|
|
|
$this->container = $container; |
|
20
|
|
|
$this->blockConfiguration = $blockConfiguration; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function getName() |
|
24
|
|
|
{ |
|
25
|
|
|
return 'cms_block'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getFunctions() |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
new \Twig_SimpleFunction('cms_block', [$this, 'displayBlock'], [ |
|
32
|
|
|
'is_safe' => ['html'], |
|
33
|
|
|
'needs_environment' => true, |
|
34
|
|
|
]), |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function displayBlock(\Twig_Environment $twig, $blockName) |
|
39
|
|
|
{ |
|
40
|
|
|
$block = $this->blockHelper->loadBlock($blockName); |
|
41
|
|
|
|
|
42
|
|
|
if ($block === null) |
|
43
|
|
|
return null; |
|
44
|
|
|
|
|
45
|
|
|
$blockConf = $this->blockConfiguration[$blockName]; |
|
46
|
|
|
if (!empty($blockConf['service'])) { |
|
47
|
|
|
$controller = $this->container->get($blockConf['service']); |
|
48
|
|
|
return $controller->renderAction(); |
|
49
|
|
|
} else { |
|
50
|
|
|
$template = $blockConf['template']; |
|
51
|
|
|
if ($template === null) { |
|
52
|
|
|
$template = 'AlpixelCMSBundle:front:blocks/base_block.html.twig'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $twig->render($template, [ |
|
56
|
|
|
'block' => $block, |
|
57
|
|
|
]); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|