|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Created by Florian Merle - Dedi Agency <[email protected]> <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace BitBag\SyliusCmsPlugin\Twig\Extension; |
|
10
|
|
|
|
|
11
|
|
|
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface; |
|
12
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
|
13
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
14
|
|
|
|
|
15
|
|
|
class RenderLinkExtension extends \Twig_Extension |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
/** @var LocaleContextInterface */ |
|
18
|
|
|
private $localeContext; |
|
19
|
|
|
|
|
20
|
|
|
/** @var PageRepositoryInterface */ |
|
21
|
|
|
private $pageRepository; |
|
22
|
|
|
|
|
23
|
|
|
/** @var RouterInterface */ |
|
24
|
|
|
private $router; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $defaultTemplate; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct( |
|
30
|
|
|
LocaleContextInterface $localeContext, |
|
31
|
|
|
PageRepositoryInterface $pageRepository, |
|
32
|
|
|
RouterInterface $router, |
|
33
|
|
|
string $defaultTemplate |
|
34
|
|
|
) { |
|
35
|
|
|
$this->localeContext = $localeContext; |
|
36
|
|
|
$this->pageRepository = $pageRepository; |
|
37
|
|
|
$this->router = $router; |
|
38
|
|
|
$this->defaultTemplate = $defaultTemplate; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getFunctions() |
|
42
|
|
|
{ |
|
43
|
|
|
return [ |
|
44
|
|
|
new \Twig_Function('bitbag_cms_render_link_for_code', [$this, 'renderLinkForCode'], [ |
|
|
|
|
|
|
45
|
|
|
'needs_environment' => true, |
|
46
|
|
|
'is_safe' => ['html'] |
|
47
|
|
|
]), |
|
48
|
|
|
new \Twig_Function('bitbag_cms_get_link_for_code', [$this, 'getLinkForCode']), |
|
|
|
|
|
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function renderLinkForCode( |
|
53
|
|
|
\Twig_Environment $environment, |
|
54
|
|
|
string $code, |
|
55
|
|
|
array $options = [], |
|
56
|
|
|
?string $template = null |
|
57
|
|
|
): string { |
|
58
|
|
|
$page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode()); |
|
59
|
|
|
|
|
60
|
|
|
return $environment->render($template ?? $this->defaultTemplate, [ |
|
61
|
|
|
'page' => $page, |
|
62
|
|
|
'options' => $options, |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getLinkForCode( |
|
67
|
|
|
string $code, |
|
68
|
|
|
array $options = [] |
|
69
|
|
|
): string { |
|
70
|
|
|
$page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode()); |
|
71
|
|
|
if (!$page) { |
|
72
|
|
|
return isset($options['notFoundMessage']) ? $options['notFoundMessage'] : ''; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', ['slug' => $page->getSlug()]); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|