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