|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\SeoBundle\Block\Breadcrumb; |
|
13
|
|
|
|
|
14
|
|
|
use Knp\Menu\FactoryInterface; |
|
15
|
|
|
use Knp\Menu\ItemInterface; |
|
16
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
|
17
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
|
18
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractBlockService; |
|
19
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
|
20
|
|
|
use Sonata\CoreBundle\Form\Type\ImmutableArrayType; |
|
21
|
|
|
use Sonata\CoreBundle\Model\Metadata; |
|
22
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
23
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
|
24
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
25
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
27
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Abstract class for breadcrumb menu services. |
|
31
|
|
|
* |
|
32
|
|
|
* @author Sylvain Deloux <[email protected]> |
|
33
|
|
|
* @author Christian Gripp <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract class AbstractBreadcrumbMenuService extends AbstractBlockService |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var FactoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $factory; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $name |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($name, EngineInterface $templating, FactoryInterface $factory) |
|
46
|
|
|
{ |
|
47
|
|
|
parent::__construct($name, $templating); |
|
48
|
|
|
|
|
49
|
|
|
$this->factory = $factory; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$responseSettings = [ |
|
55
|
|
|
'menu' => $this->getMenu($blockContext), |
|
56
|
|
|
'menu_options' => $this->getMenuOptions($blockContext->getSettings()), |
|
57
|
|
|
'block' => $blockContext->getBlock(), |
|
58
|
|
|
'context' => $blockContext, |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
if ('private' === $blockContext->getSetting('cache_policy')) { |
|
62
|
|
|
return $this->renderPrivateResponse($blockContext->getTemplate(), $responseSettings, $response); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->renderResponse($blockContext->getTemplate(), $responseSettings, $response); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function buildEditForm(FormMapper $form, BlockInterface $block) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$form->add('settings', ImmutableArrayType::class, [ |
|
71
|
|
|
'keys' => $this->getFormSettingsKeys(), |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getBlockMetadata($code = null) |
|
76
|
|
|
{ |
|
77
|
|
|
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataBlockBundle', [ |
|
78
|
|
|
'class' => 'fa fa-bars', |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function configureSettings(OptionsResolver $resolver) |
|
83
|
|
|
{ |
|
84
|
|
|
$resolver->setDefaults([ |
|
85
|
|
|
'title' => $this->getName(), |
|
86
|
|
|
'cache_policy' => 'public', |
|
87
|
|
|
'template' => '@SonataBlock/Block/block_core_menu.html.twig', |
|
88
|
|
|
'menu_name' => '', |
|
89
|
|
|
'safe_labels' => false, |
|
90
|
|
|
'current_class' => 'active', |
|
91
|
|
|
'first_class' => false, |
|
92
|
|
|
'last_class' => false, |
|
93
|
|
|
'current_uri' => null, |
|
94
|
|
|
'menu_class' => 'list-group', |
|
95
|
|
|
'children_class' => 'list-group-item', |
|
96
|
|
|
'menu_template' => '@SonataSeo/Block/breadcrumb.html.twig', |
|
97
|
|
|
'include_homepage_link' => true, |
|
98
|
|
|
'context' => false, |
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getFormSettingsKeys() |
|
106
|
|
|
{ |
|
107
|
|
|
return [ |
|
108
|
|
|
['title', TextType::class, ['required' => false]], |
|
109
|
|
|
['cache_policy', ChoiceType::class, ['choices' => ['public', 'private']]], |
|
110
|
|
|
['safe_labels', CheckboxType::class, ['required' => false]], |
|
111
|
|
|
['current_class', TextType::class, ['required' => false]], |
|
112
|
|
|
['first_class', TextType::class, ['required' => false]], |
|
113
|
|
|
['last_class', TextType::class, ['required' => false]], |
|
114
|
|
|
['menu_class', TextType::class, ['required' => false]], |
|
115
|
|
|
['children_class', TextType::class, ['required' => false]], |
|
116
|
|
|
['menu_template', TextType::class, ['required' => false]], |
|
117
|
|
|
]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Gets the menu to render. |
|
122
|
|
|
* |
|
123
|
|
|
* @return ItemInterface|string |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getMenu(BlockContextInterface $blockContext) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getRootMenu($blockContext); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Replaces setting keys with knp menu item options keys. |
|
132
|
|
|
* |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function getMenuOptions(array $settings) |
|
136
|
|
|
{ |
|
137
|
|
|
$mapping = [ |
|
138
|
|
|
'current_class' => 'currentClass', |
|
139
|
|
|
'first_class' => 'firstClass', |
|
140
|
|
|
'last_class' => 'lastClass', |
|
141
|
|
|
'safe_labels' => 'allow_safe_labels', |
|
142
|
|
|
'menu_template' => 'template', |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
$options = []; |
|
146
|
|
|
|
|
147
|
|
|
foreach ($settings as $key => $value) { |
|
148
|
|
|
if (array_key_exists($key, $mapping) && null !== $value) { |
|
149
|
|
|
$options[$mapping[$key]] = $value; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $options; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return FactoryInterface |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function getFactory() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->factory; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Initialize breadcrumb menu. |
|
166
|
|
|
* |
|
167
|
|
|
* @return ItemInterface |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function getRootMenu(BlockContextInterface $blockContext) |
|
170
|
|
|
{ |
|
171
|
|
|
$settings = $blockContext->getSettings(); |
|
172
|
|
|
/* |
|
173
|
|
|
* @todo : Use the router to get the homepage URI |
|
174
|
|
|
*/ |
|
175
|
|
|
|
|
176
|
|
|
$menu = $this->factory->createItem('breadcrumb'); |
|
177
|
|
|
|
|
178
|
|
|
$menu->setChildrenAttribute('class', 'breadcrumb'); |
|
179
|
|
|
|
|
180
|
|
|
if (method_exists($menu, 'setCurrentUri')) { |
|
181
|
|
|
$menu->setCurrentUri($settings['current_uri']); |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
if (method_exists($menu, 'setCurrent')) { |
|
185
|
|
|
$menu->setCurrent($settings['current_uri']); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
if ($settings['include_homepage_link']) { |
|
189
|
|
|
$menu->addChild('sonata_seo_homepage_breadcrumb', ['uri' => '/']); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $menu; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.