1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\PagePartBundle\Twig\Extension; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdmin; |
6
|
|
|
use Twig\Environment; |
7
|
|
|
use Twig\Extension\AbstractExtension; |
8
|
|
|
use Twig\TwigFunction; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PagePartAdminTwigExtension |
12
|
|
|
* |
13
|
|
|
* @final since 5.4 |
14
|
|
|
*/ |
15
|
|
|
class PagePartAdminTwigExtension extends AbstractExtension |
16
|
|
|
{ |
17
|
|
|
private $usesExtendedPagePartChooser = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return array |
|
|
|
|
21
|
|
|
*/ |
22
|
|
|
public function getFunctions() |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
|
|
new TwigFunction('pagepartadmin_widget', [$this, 'renderWidget'], ['needs_environment' => true, 'is_safe' => ['html']]), |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Renders the HTML for a given pagepart |
31
|
|
|
* |
32
|
|
|
* Example usage in Twig: |
33
|
|
|
* |
34
|
|
|
* {{ pagepartadmin_widget(ppAdmin) }} |
35
|
|
|
* |
36
|
|
|
* You can pass options during the call: |
37
|
|
|
* |
38
|
|
|
* {{ pagepartadmin_widget(ppAdmin, {'attr': {'class': 'foo'}}) }} |
39
|
|
|
* |
40
|
|
|
* {{ pagepartadmin_widget(ppAdmin, {'separator': '+++++'}) }} |
41
|
|
|
* |
42
|
|
|
* @param Environment $env |
43
|
|
|
* @param PagePartAdmin $ppAdmin The pagepart admin to render |
44
|
|
|
* @param Form $form The form |
|
|
|
|
45
|
|
|
* @param array $parameters Additional variables passed to the template |
46
|
|
|
* @param string $templateName |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @return string The html markup |
49
|
|
|
*/ |
50
|
|
|
public function renderWidget(Environment $env, PagePartAdmin $ppAdmin, $form = null, array $parameters = [], $templateName = null) |
51
|
|
|
{ |
52
|
|
|
if ($templateName === null) { |
53
|
|
|
$templateName = '@KunstmaanPagePart/PagePartAdminTwigExtension/widget.html.twig'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$template = $env->load($templateName); |
57
|
|
|
|
58
|
|
|
return $template->render(array_merge($parameters, [ |
59
|
|
|
'pagepartadmin' => $ppAdmin, |
60
|
|
|
'page' => $ppAdmin->getPage(), |
61
|
|
|
'form' => $form, |
62
|
|
|
'extended' => $this->usesExtendedPagePartChooser, |
63
|
|
|
])); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get usesExtendedPagePartChooser. |
68
|
|
|
* |
69
|
|
|
* @return usesExtendedPagePartChooser |
|
|
|
|
70
|
|
|
*/ |
71
|
|
|
public function getUsesExtendedPagePartChooser() |
72
|
|
|
{ |
73
|
|
|
return $this->usesExtendedPagePartChooser; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set usesExtendedPagePartChooser. |
78
|
|
|
* |
79
|
|
|
* @param usesExtendedPagePartChooser the value to set |
80
|
|
|
*/ |
81
|
|
|
public function setUsesExtendedPagePartChooser($usesExtendedPagePartChooser) |
82
|
|
|
{ |
83
|
|
|
$this->usesExtendedPagePartChooser = $usesExtendedPagePartChooser; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.