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
|
|
|
|
8
|
|
|
class BlockExtension extends \Twig_Extension |
9
|
|
|
{ |
10
|
|
|
private $blockHelper; |
11
|
|
|
private $container; |
12
|
|
|
private $blockConfiguration; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* BlockExtension constructor. |
16
|
|
|
* |
17
|
|
|
* @param BlockHelper $blockHelper |
18
|
|
|
* @param $container |
19
|
|
|
* @param $blockConfiguration |
20
|
|
|
*/ |
21
|
|
|
public function __construct(BlockHelper $blockHelper, $container, $blockConfiguration) |
22
|
|
|
{ |
23
|
|
|
$this->blockHelper = $blockHelper; |
24
|
|
|
$this->container = $container; |
25
|
|
|
$this->blockConfiguration = $blockConfiguration; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getName() |
32
|
|
|
{ |
33
|
|
|
return 'cms_block'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function getFunctions() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
new \Twig_SimpleFunction('cms_block', [$this, 'displayBlock'], [ |
43
|
|
|
'is_safe' => ['html'], |
44
|
|
|
'needs_environment' => true, |
45
|
|
|
]), |
46
|
|
|
new \Twig_SimpleFunction('cms_block_object', [$this, 'getCMSBlockObject']), |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Twig_Environment $twig |
52
|
|
|
* @param $blockName |
53
|
|
|
* |
54
|
|
|
* @return string|void |
55
|
|
|
*/ |
56
|
|
|
public function displayBlock(\Twig_Environment $twig, $blockName) |
57
|
|
|
{ |
58
|
|
|
$block = $this->blockHelper->loadBlock($blockName); |
59
|
|
|
|
60
|
|
|
if ($block === null) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$blockConf = $this->blockConfiguration[$blockName]; |
65
|
|
|
if (!empty($blockConf['service'])) { |
66
|
|
|
$controller = $this->container->get($blockConf['service']); |
67
|
|
|
|
68
|
|
|
return $controller->renderAction(); |
69
|
|
|
} else { |
70
|
|
|
if (!isset($blockConf['template'])) { |
71
|
|
|
$template = 'AlpixelCMSBundle:front:blocks/base_block.html.twig'; |
72
|
|
|
} else { |
73
|
|
|
$template = $blockConf['template']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $twig->render($template, [ |
77
|
|
|
'block' => $block, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $blockName |
84
|
|
|
* @return \Alpixel\Bundle\CMSBundle\Entity\Block|null |
85
|
|
|
*/ |
86
|
|
|
public function getCMSBlockObject($blockName) |
87
|
|
|
{ |
88
|
|
|
return $this->blockHelper->loadBlock($blockName); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|