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