Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Layout;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\Definition\DerivablePluginDefinitionInterface;
|
Chris@0
|
6 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
|
Chris@0
|
7 use Drupal\Component\Plugin\Definition\PluginDefinition;
|
Chris@0
|
8 use Drupal\Core\Plugin\Definition\DependentPluginDefinitionInterface;
|
Chris@0
|
9 use Drupal\Core\Plugin\Definition\DependentPluginDefinitionTrait;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Provides an implementation of a layout definition and its metadata.
|
Chris@0
|
13 */
|
Chris@0
|
14 class LayoutDefinition extends PluginDefinition implements PluginDefinitionInterface, DerivablePluginDefinitionInterface, DependentPluginDefinitionInterface {
|
Chris@0
|
15
|
Chris@0
|
16 use DependentPluginDefinitionTrait;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * The name of the deriver of this layout definition, if any.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var string|null
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $deriver;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * The human-readable name.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var string
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $label;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * An optional description for advanced layouts.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var string
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $description;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * The human-readable category.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var string
|
Chris@0
|
43 */
|
Chris@0
|
44 protected $category;
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * The template file to render this layout (relative to the 'path' given).
|
Chris@0
|
48 *
|
Chris@0
|
49 * @var string|null
|
Chris@0
|
50 */
|
Chris@0
|
51 protected $template;
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * The path to the template.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @var string
|
Chris@0
|
57 */
|
Chris@0
|
58 protected $templatePath;
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * The theme hook used to render this layout.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @var string|null
|
Chris@0
|
64 */
|
Chris@0
|
65 protected $theme_hook;
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Path (relative to the module or theme) to resources like icon or template.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @var string
|
Chris@0
|
71 */
|
Chris@0
|
72 protected $path;
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * The asset library.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @var string|null
|
Chris@0
|
78 */
|
Chris@0
|
79 protected $library;
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * The path to the preview image.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @var string
|
Chris@0
|
85 */
|
Chris@0
|
86 protected $icon;
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * An associative array of regions in this layout.
|
Chris@0
|
90 *
|
Chris@0
|
91 * The key of the array is the machine name of the region, and the value is
|
Chris@0
|
92 * an associative array with the following keys:
|
Chris@0
|
93 * - label: (string) The human-readable name of the region.
|
Chris@0
|
94 *
|
Chris@0
|
95 * Any remaining keys may have special meaning for the given layout plugin,
|
Chris@0
|
96 * but are undefined here.
|
Chris@0
|
97 *
|
Chris@0
|
98 * @var array
|
Chris@0
|
99 */
|
Chris@0
|
100 protected $regions = [];
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * The default region.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @var string
|
Chris@0
|
106 */
|
Chris@0
|
107 protected $default_region;
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Any additional properties and values.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @var array
|
Chris@0
|
113 */
|
Chris@0
|
114 protected $additional = [];
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * LayoutDefinition constructor.
|
Chris@0
|
118 *
|
Chris@0
|
119 * @param array $definition
|
Chris@0
|
120 * An array of values from the annotation.
|
Chris@0
|
121 */
|
Chris@0
|
122 public function __construct(array $definition) {
|
Chris@0
|
123 foreach ($definition as $property => $value) {
|
Chris@0
|
124 $this->set($property, $value);
|
Chris@0
|
125 }
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * Gets any arbitrary property.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @param string $property
|
Chris@0
|
132 * The property to retrieve.
|
Chris@0
|
133 *
|
Chris@0
|
134 * @return mixed
|
Chris@0
|
135 * The value for that property, or NULL if the property does not exist.
|
Chris@0
|
136 */
|
Chris@0
|
137 public function get($property) {
|
Chris@0
|
138 if (property_exists($this, $property)) {
|
Chris@0
|
139 $value = isset($this->{$property}) ? $this->{$property} : NULL;
|
Chris@0
|
140 }
|
Chris@0
|
141 else {
|
Chris@0
|
142 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
|
Chris@0
|
143 }
|
Chris@0
|
144 return $value;
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Sets a value to an arbitrary property.
|
Chris@0
|
149 *
|
Chris@0
|
150 * @param string $property
|
Chris@0
|
151 * The property to use for the value.
|
Chris@0
|
152 * @param mixed $value
|
Chris@0
|
153 * The value to set.
|
Chris@0
|
154 *
|
Chris@0
|
155 * @return $this
|
Chris@0
|
156 */
|
Chris@0
|
157 public function set($property, $value) {
|
Chris@0
|
158 if (property_exists($this, $property)) {
|
Chris@0
|
159 $this->{$property} = $value;
|
Chris@0
|
160 }
|
Chris@0
|
161 else {
|
Chris@0
|
162 $this->additional[$property] = $value;
|
Chris@0
|
163 }
|
Chris@0
|
164 return $this;
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 /**
|
Chris@0
|
168 * Gets the human-readable name of the layout definition.
|
Chris@0
|
169 *
|
Chris@0
|
170 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
|
Chris@0
|
171 * The human-readable name of the layout definition.
|
Chris@0
|
172 */
|
Chris@0
|
173 public function getLabel() {
|
Chris@0
|
174 return $this->label;
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * Sets the human-readable name of the layout definition.
|
Chris@0
|
179 *
|
Chris@0
|
180 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $label
|
Chris@0
|
181 * The human-readable name of the layout definition.
|
Chris@0
|
182 *
|
Chris@0
|
183 * @return $this
|
Chris@0
|
184 */
|
Chris@0
|
185 public function setLabel($label) {
|
Chris@0
|
186 $this->label = $label;
|
Chris@0
|
187 return $this;
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 /**
|
Chris@0
|
191 * Gets the description of the layout definition.
|
Chris@0
|
192 *
|
Chris@0
|
193 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
|
Chris@0
|
194 * The description of the layout definition.
|
Chris@0
|
195 */
|
Chris@0
|
196 public function getDescription() {
|
Chris@0
|
197 return $this->description;
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Sets the description of the layout definition.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $description
|
Chris@0
|
204 * The description of the layout definition.
|
Chris@0
|
205 *
|
Chris@0
|
206 * @return $this
|
Chris@0
|
207 */
|
Chris@0
|
208 public function setDescription($description) {
|
Chris@0
|
209 $this->description = $description;
|
Chris@0
|
210 return $this;
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 /**
|
Chris@0
|
214 * Gets the human-readable category of the layout definition.
|
Chris@0
|
215 *
|
Chris@0
|
216 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
|
Chris@0
|
217 * The human-readable category of the layout definition.
|
Chris@0
|
218 */
|
Chris@0
|
219 public function getCategory() {
|
Chris@0
|
220 return $this->category;
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 /**
|
Chris@0
|
224 * Sets the human-readable category of the layout definition.
|
Chris@0
|
225 *
|
Chris@0
|
226 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $category
|
Chris@0
|
227 * The human-readable category of the layout definition.
|
Chris@0
|
228 *
|
Chris@0
|
229 * @return $this
|
Chris@0
|
230 */
|
Chris@0
|
231 public function setCategory($category) {
|
Chris@0
|
232 $this->category = $category;
|
Chris@0
|
233 return $this;
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * Gets the template name.
|
Chris@0
|
238 *
|
Chris@0
|
239 * @return string|null
|
Chris@0
|
240 * The template name, if it exists.
|
Chris@0
|
241 */
|
Chris@0
|
242 public function getTemplate() {
|
Chris@0
|
243 return $this->template;
|
Chris@0
|
244 }
|
Chris@0
|
245
|
Chris@0
|
246 /**
|
Chris@0
|
247 * Sets the template name.
|
Chris@0
|
248 *
|
Chris@0
|
249 * @param string|null $template
|
Chris@0
|
250 * The template name.
|
Chris@0
|
251 *
|
Chris@0
|
252 * @return $this
|
Chris@0
|
253 */
|
Chris@0
|
254 public function setTemplate($template) {
|
Chris@0
|
255 $this->template = $template;
|
Chris@0
|
256 return $this;
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 /**
|
Chris@0
|
260 * Gets the template path.
|
Chris@0
|
261 *
|
Chris@0
|
262 * @return string
|
Chris@0
|
263 * The template path.
|
Chris@0
|
264 */
|
Chris@0
|
265 public function getTemplatePath() {
|
Chris@0
|
266 return $this->templatePath;
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 /**
|
Chris@0
|
270 * Sets the template path.
|
Chris@0
|
271 *
|
Chris@0
|
272 * @param string $template_path
|
Chris@0
|
273 * The template path.
|
Chris@0
|
274 *
|
Chris@0
|
275 * @return $this
|
Chris@0
|
276 */
|
Chris@0
|
277 public function setTemplatePath($template_path) {
|
Chris@0
|
278 $this->templatePath = $template_path;
|
Chris@0
|
279 return $this;
|
Chris@0
|
280 }
|
Chris@0
|
281
|
Chris@0
|
282 /**
|
Chris@0
|
283 * Gets the theme hook.
|
Chris@0
|
284 *
|
Chris@0
|
285 * @return string|null
|
Chris@0
|
286 * The theme hook, if it exists.
|
Chris@0
|
287 */
|
Chris@0
|
288 public function getThemeHook() {
|
Chris@0
|
289 return $this->theme_hook;
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 /**
|
Chris@0
|
293 * Sets the theme hook.
|
Chris@0
|
294 *
|
Chris@0
|
295 * @param string $theme_hook
|
Chris@0
|
296 * The theme hook.
|
Chris@0
|
297 *
|
Chris@0
|
298 * @return $this
|
Chris@0
|
299 */
|
Chris@0
|
300 public function setThemeHook($theme_hook) {
|
Chris@0
|
301 $this->theme_hook = $theme_hook;
|
Chris@0
|
302 return $this;
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 /**
|
Chris@0
|
306 * Gets the base path for this layout definition.
|
Chris@0
|
307 *
|
Chris@0
|
308 * @return string
|
Chris@0
|
309 * The base path.
|
Chris@0
|
310 */
|
Chris@0
|
311 public function getPath() {
|
Chris@0
|
312 return $this->path;
|
Chris@0
|
313 }
|
Chris@0
|
314
|
Chris@0
|
315 /**
|
Chris@0
|
316 * Sets the base path for this layout definition.
|
Chris@0
|
317 *
|
Chris@0
|
318 * @param string $path
|
Chris@0
|
319 * The base path.
|
Chris@0
|
320 *
|
Chris@0
|
321 * @return $this
|
Chris@0
|
322 */
|
Chris@0
|
323 public function setPath($path) {
|
Chris@0
|
324 $this->path = $path;
|
Chris@0
|
325 return $this;
|
Chris@0
|
326 }
|
Chris@0
|
327
|
Chris@0
|
328 /**
|
Chris@0
|
329 * Gets the asset library for this layout definition.
|
Chris@0
|
330 *
|
Chris@0
|
331 * @return string|null
|
Chris@0
|
332 * The asset library, if it exists.
|
Chris@0
|
333 */
|
Chris@0
|
334 public function getLibrary() {
|
Chris@0
|
335 return $this->library;
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 /**
|
Chris@0
|
339 * Sets the asset library for this layout definition.
|
Chris@0
|
340 *
|
Chris@0
|
341 * @param string|null $library
|
Chris@0
|
342 * The asset library.
|
Chris@0
|
343 *
|
Chris@0
|
344 * @return $this
|
Chris@0
|
345 */
|
Chris@0
|
346 public function setLibrary($library) {
|
Chris@0
|
347 $this->library = $library;
|
Chris@0
|
348 return $this;
|
Chris@0
|
349 }
|
Chris@0
|
350
|
Chris@0
|
351 /**
|
Chris@0
|
352 * Gets the icon path for this layout definition.
|
Chris@0
|
353 *
|
Chris@0
|
354 * @return string|null
|
Chris@0
|
355 * The icon path, if it exists.
|
Chris@0
|
356 */
|
Chris@0
|
357 public function getIconPath() {
|
Chris@0
|
358 return $this->icon;
|
Chris@0
|
359 }
|
Chris@0
|
360
|
Chris@0
|
361 /**
|
Chris@0
|
362 * Sets the icon path for this layout definition.
|
Chris@0
|
363 *
|
Chris@0
|
364 * @param string|null $icon
|
Chris@0
|
365 * The icon path.
|
Chris@0
|
366 *
|
Chris@0
|
367 * @return $this
|
Chris@0
|
368 */
|
Chris@0
|
369 public function setIconPath($icon) {
|
Chris@0
|
370 $this->icon = $icon;
|
Chris@0
|
371 return $this;
|
Chris@0
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 /**
|
Chris@0
|
375 * Gets the regions for this layout definition.
|
Chris@0
|
376 *
|
Chris@0
|
377 * @return array[]
|
Chris@0
|
378 * The layout regions. The keys of the array are the machine names of the
|
Chris@0
|
379 * regions, and the values are an associative array with the following keys:
|
Chris@0
|
380 * - label: (string) The human-readable name of the region.
|
Chris@0
|
381 * Any remaining keys may have special meaning for the given layout plugin,
|
Chris@0
|
382 * but are undefined here.
|
Chris@0
|
383 */
|
Chris@0
|
384 public function getRegions() {
|
Chris@0
|
385 return $this->regions;
|
Chris@0
|
386 }
|
Chris@0
|
387
|
Chris@0
|
388 /**
|
Chris@0
|
389 * Sets the regions for this layout definition.
|
Chris@0
|
390 *
|
Chris@0
|
391 * @param array[] $regions
|
Chris@0
|
392 * An array of regions, see ::getRegions() for the format.
|
Chris@0
|
393 *
|
Chris@0
|
394 * @return $this
|
Chris@0
|
395 */
|
Chris@0
|
396 public function setRegions(array $regions) {
|
Chris@0
|
397 $this->regions = $regions;
|
Chris@0
|
398 return $this;
|
Chris@0
|
399 }
|
Chris@0
|
400
|
Chris@0
|
401 /**
|
Chris@0
|
402 * Gets the machine-readable region names.
|
Chris@0
|
403 *
|
Chris@0
|
404 * @return string[]
|
Chris@0
|
405 * An array of machine-readable region names.
|
Chris@0
|
406 */
|
Chris@0
|
407 public function getRegionNames() {
|
Chris@0
|
408 return array_keys($this->getRegions());
|
Chris@0
|
409 }
|
Chris@0
|
410
|
Chris@0
|
411 /**
|
Chris@0
|
412 * Gets the human-readable region labels.
|
Chris@0
|
413 *
|
Chris@0
|
414 * @return string[]
|
Chris@0
|
415 * An array of human-readable region labels.
|
Chris@0
|
416 */
|
Chris@0
|
417 public function getRegionLabels() {
|
Chris@0
|
418 $regions = $this->getRegions();
|
Chris@0
|
419 return array_combine(array_keys($regions), array_column($regions, 'label'));
|
Chris@0
|
420 }
|
Chris@0
|
421
|
Chris@0
|
422 /**
|
Chris@0
|
423 * Gets the default region.
|
Chris@0
|
424 *
|
Chris@0
|
425 * @return string
|
Chris@0
|
426 * The machine-readable name of the default region.
|
Chris@0
|
427 */
|
Chris@0
|
428 public function getDefaultRegion() {
|
Chris@0
|
429 return $this->default_region;
|
Chris@0
|
430 }
|
Chris@0
|
431
|
Chris@0
|
432 /**
|
Chris@0
|
433 * Sets the default region.
|
Chris@0
|
434 *
|
Chris@0
|
435 * @param string $default_region
|
Chris@0
|
436 * The machine-readable name of the default region.
|
Chris@0
|
437 *
|
Chris@0
|
438 * @return $this
|
Chris@0
|
439 */
|
Chris@0
|
440 public function setDefaultRegion($default_region) {
|
Chris@0
|
441 $this->default_region = $default_region;
|
Chris@0
|
442 return $this;
|
Chris@0
|
443 }
|
Chris@0
|
444
|
Chris@0
|
445 /**
|
Chris@0
|
446 * {@inheritdoc}
|
Chris@0
|
447 */
|
Chris@0
|
448 public function getDeriver() {
|
Chris@0
|
449 return $this->deriver;
|
Chris@0
|
450 }
|
Chris@0
|
451
|
Chris@0
|
452 /**
|
Chris@0
|
453 * {@inheritdoc}
|
Chris@0
|
454 */
|
Chris@0
|
455 public function setDeriver($deriver) {
|
Chris@0
|
456 $this->deriver = $deriver;
|
Chris@0
|
457 return $this;
|
Chris@0
|
458 }
|
Chris@0
|
459
|
Chris@0
|
460 }
|