1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\BlocksModule\Twig\Extension; |
15
|
|
|
|
16
|
|
|
use RuntimeException; |
17
|
|
|
use Twig\Extension\AbstractExtension; |
18
|
|
|
use Twig\TwigFunction; |
19
|
|
|
use Twig\TwigTest; |
20
|
|
|
use Zikula\BlocksModule\Api\ApiInterface\BlockApiInterface; |
21
|
|
|
use Zikula\BlocksModule\Api\ApiInterface\BlockFilterApiInterface; |
22
|
|
|
use Zikula\BlocksModule\Collectible\PendingContentCollectible; |
23
|
|
|
use Zikula\BlocksModule\Entity\BlockEntity; |
24
|
|
|
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface; |
25
|
|
|
use Zikula\ThemeModule\Engine\Engine; |
26
|
|
|
|
27
|
|
|
class BlocksExtension extends AbstractExtension |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var BlockApiInterface |
31
|
|
|
*/ |
32
|
|
|
private $blockApi; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var BlockFilterApiInterface |
36
|
|
|
*/ |
37
|
|
|
private $blockFilter; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Engine |
41
|
|
|
*/ |
42
|
|
|
private $themeEngine; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ZikulaHttpKernelInterface |
46
|
|
|
*/ |
47
|
|
|
private $kernel; |
48
|
|
|
|
49
|
|
|
public function __construct( |
50
|
|
|
BlockApiInterface $blockApi, |
51
|
|
|
BlockFilterApiInterface $blockFilterApi, |
52
|
|
|
Engine $themeEngine, |
53
|
|
|
ZikulaHttpKernelInterface $kernel |
54
|
|
|
) { |
55
|
|
|
$this->blockApi = $blockApi; |
56
|
|
|
$this->blockFilter = $blockFilterApi; |
57
|
|
|
$this->themeEngine = $themeEngine; |
58
|
|
|
$this->kernel = $kernel; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getFunctions() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
new TwigFunction('showblockposition', [$this, 'showBlockPosition'], ['is_safe' => ['html']]), |
65
|
|
|
new TwigFunction('showblock', [$this, 'showBlock'], ['is_safe' => ['html']]), |
66
|
|
|
new TwigFunction('positionavailable', [$this, 'isPositionAvailable']), |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getTests() |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
new TwigTest('pendingContentCollectible', function ($obj) { return $obj instanceof PendingContentCollectible; }), |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Show all the blocks in a position by name. |
79
|
|
|
* |
80
|
|
|
* @return array|string |
81
|
|
|
*/ |
82
|
|
|
public function showBlockPosition(string $positionName, bool $implode = true) |
83
|
|
|
{ |
84
|
|
|
$instance = $this->kernel->getModule('ZikulaBlocksModule'); |
85
|
|
|
if (!isset($instance)) { |
86
|
|
|
return 'Blocks not currently available.'; |
87
|
|
|
} |
88
|
|
|
$blocks = $this->blockApi->getBlocksByPosition($positionName); |
89
|
|
|
foreach ($blocks as $key => $block) { |
90
|
|
|
$blocks[$key] = $this->showBlock($block, $positionName); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $implode ? implode("\n", $blocks) : $blocks; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Display one block. |
98
|
|
|
*/ |
99
|
|
|
public function showBlock(BlockEntity $block, string $positionName = ''): string |
100
|
|
|
{ |
101
|
|
|
$blocksModuleInstance = $this->kernel->getModule('ZikulaBlocksModule'); |
102
|
|
|
if (!isset($blocksModuleInstance)) { |
103
|
|
|
return 'Blocks not currently available.'; |
104
|
|
|
} |
105
|
|
|
// Check if providing module not available, if block is inactive, if block filter prevents display. |
106
|
|
|
$bundleName = $block->getModule()->getName(); |
107
|
|
|
$moduleInstance = $this->kernel->getModule($bundleName); |
108
|
|
|
if (!isset($moduleInstance) || !$block->getActive() || !$this->blockFilter->isDisplayable($block)) { |
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$blockInstance = $this->blockApi->createInstanceFromBKey($block->getBkey()); |
114
|
|
|
} catch (RuntimeException $exception) { |
115
|
|
|
//return 'Error during block creation: ' . $exception->getMessage(); |
116
|
|
|
return ''; |
117
|
|
|
} |
118
|
|
|
$blockProperties = $block->getProperties(); |
119
|
|
|
$blockProperties['bid'] = $block->getBid(); |
120
|
|
|
$blockProperties['title'] = $block->getTitle(); |
121
|
|
|
$blockProperties['position'] = $positionName; |
122
|
|
|
$content = $blockInstance->display($blockProperties); |
123
|
|
|
if (isset($moduleInstance)) { |
124
|
|
|
// add module stylesheet to page |
125
|
|
|
$moduleInstance->addStylesheet(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->themeEngine->wrapBlockContentInTheme($content, $block->getTitle(), $block->getBlocktype(), $block->getBid(), $positionName); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function isPositionAvailable(string $name): bool |
132
|
|
|
{ |
133
|
|
|
return $this->themeEngine->positionIsAvailableInTheme($name); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|