-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
NavigationExtension.php
110 lines (100 loc) · 3.16 KB
/
NavigationExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace Pimcore\Twig\Extension;
use Pimcore\Model\Document;
use Pimcore\Navigation\Container;
use Pimcore\Navigation\Renderer\RendererInterface;
use Pimcore\Templating\Helper\Navigation;
class NavigationExtension extends \Twig_Extension
{
/**
* @var Navigation
*/
private $navigationHelper;
/**
* @param Navigation $navigationHelper
*/
public function __construct(Navigation $navigationHelper)
{
$this->navigationHelper = $navigationHelper;
}
public function getFunctions(): array
{
return [
new \Twig_SimpleFunction('pimcore_build_nav', [$this, 'buildNavigation']),
new \Twig_SimpleFunction('pimcore_render_nav', [$this, 'render'], [
'is_safe' => ['html']
]),
new \Twig_SimpleFunction('pimcore_nav_renderer', [$this, 'getRenderer']),
];
}
/**
* This is essentially the same as the buildNavigation method on the
* Navigation helper, but without a pageCallback as it does not make
* sense in Twig context. If you need a more customized navigation
* with a callback, build the navigation externally, pass it to the
* view and just call render through the extension.
*
* @param Document $activeDocument
* @param Document|null $navigationRootDocument
* @param string|null $htmlMenuPrefix
* @param bool|string $cache
*
* @return Container
*/
public function buildNavigation(
Document $activeDocument,
Document $navigationRootDocument = null,
string $htmlMenuPrefix = null,
$cache = true
): Container {
return $this->navigationHelper->buildNavigation(
$activeDocument,
$navigationRootDocument,
$htmlMenuPrefix,
null,
$cache
);
}
/**
* Loads a renderer instance
*
* @param string $alias
*
* @return RendererInterface
*/
public function getRenderer(string $alias): RendererInterface
{
return $this->navigationHelper->getRenderer($alias);
}
/**
* Renders a navigation with the given renderer
*
* @param Container $container
* @param string $rendererName
* @param string|null $renderMethod Optional render method to use (e.g. menu -> renderMenu)
* @param array $rendererArguments Option arguments to pass to the render method after the container
*
* @return string
*/
public function render(
Container $container,
string $rendererName = 'menu',
string $renderMethod = null,
...$rendererArguments
) {
return call_user_func_array([$this->navigationHelper, 'render'], func_get_args());
}
}