1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Everlution\NavigationBundle\Twig; |
6
|
|
|
|
7
|
|
|
use Everlution\Navigation\Builder\NoCurrentItemFoundException; |
8
|
|
|
use Twig\Environment; |
9
|
|
|
use Twig\Extension\AbstractExtension; |
10
|
|
|
use Twig\TwigFilter; |
11
|
|
|
use Twig\TwigFunction; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class NavigationExtension. |
15
|
|
|
* |
16
|
|
|
* @author Ivan Barlog <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class NavigationExtension extends AbstractExtension |
19
|
|
|
{ |
20
|
|
|
/** @var ItemHelper */ |
21
|
|
|
private $helper; |
22
|
|
|
/** @var NavigationHelper */ |
23
|
|
|
private $navigationHelper; |
24
|
|
|
|
25
|
|
|
public function __construct(ItemHelper $helper, NavigationHelper $navigationHelper) |
26
|
|
|
{ |
27
|
|
|
$this->helper = $helper; |
28
|
|
|
$this->navigationHelper = $navigationHelper; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function renderNavigation( |
35
|
|
|
Environment $environment, |
36
|
|
|
string $identifier, |
37
|
|
|
string $template = '@EverlutionNavigation/bootstrap_navigation.html.twig' |
38
|
|
|
): string { |
39
|
|
|
return $environment->render( |
40
|
|
|
$template, |
41
|
|
|
[ |
42
|
|
|
'root' => $this->navigationHelper->getNavigation($identifier)->getRoot(), |
43
|
|
|
'identifier' => $identifier, |
44
|
|
|
'helper' => $this->helper, |
45
|
|
|
'navigation_helper' => $this->navigationHelper, |
46
|
|
|
] |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function renderBreadcrumbs( |
54
|
|
|
Environment $environment, |
55
|
|
|
string $identifier, |
56
|
|
|
string $template = '@EverlutionNavigation/bootstrap_breadcrumbs.html.twig' |
57
|
|
|
): string { |
58
|
|
|
try { |
59
|
|
|
$items = $this->navigationHelper->getNavigation($identifier)->getBreadcrumbs(); |
60
|
|
|
} catch (NoCurrentItemFoundException $exception) { |
61
|
|
|
return 'missing breadcrumbs'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $environment->render( |
65
|
|
|
$template, |
66
|
|
|
[ |
67
|
|
|
'items' => $items, |
68
|
|
|
'identifier' => $identifier, |
69
|
|
|
'helper' => $this->helper, |
70
|
|
|
'navigation_helper' => $this->navigationHelper, |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getFunctions() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
new TwigFunction( |
79
|
|
|
'render_navigation', |
80
|
|
|
[$this, 'renderNavigation'], |
81
|
|
|
['needs_environment' => true, 'is_safe' => ['html']] |
82
|
|
|
), |
83
|
|
|
new TwigFunction( |
84
|
|
|
'render_breadcrumbs', |
85
|
|
|
[$this, 'renderBreadcrumbs'], |
86
|
|
|
['needs_environment' => true, 'is_safe' => ['html']] |
87
|
|
|
), |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getFilters() |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
new TwigFilter('url', [$this->helper, 'getUrl']), |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|