3 namespace Drupal\Core\Layout;
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;
12 * Provides an implementation of a layout definition and its metadata.
14 class LayoutDefinition extends PluginDefinition implements PluginDefinitionInterface, DerivablePluginDefinitionInterface, DependentPluginDefinitionInterface {
16 use DependentPluginDefinitionTrait;
19 * The name of the deriver of this layout definition, if any.
26 * The human-readable name.
33 * An optional description for advanced layouts.
37 protected $description;
40 * The human-readable category.
47 * The template file to render this layout (relative to the 'path' given).
54 * The path to the template.
58 protected $templatePath;
61 * The theme hook used to render this layout.
65 protected $theme_hook;
68 * Path (relative to the module or theme) to resources like icon or template.
82 * The path to the preview image.
89 * An array defining the regions of a layout.
91 * @var string[][]|null
93 * @see \Drupal\Core\Layout\Icon\IconBuilderInterface::build()
98 * An associative array of regions in this layout.
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.
104 * Any remaining keys may have special meaning for the given layout plugin,
105 * but are undefined here.
109 protected $regions = [];
112 * The default region.
116 protected $default_region;
119 * Any additional properties and values.
123 protected $additional = [];
126 * LayoutDefinition constructor.
128 * @param array $definition
129 * An array of values from the annotation.
131 public function __construct(array $definition) {
132 foreach ($definition as $property => $value) {
133 $this->set($property, $value);
138 * Gets any arbitrary property.
140 * @param string $property
141 * The property to retrieve.
144 * The value for that property, or NULL if the property does not exist.
146 public function get($property) {
147 if (property_exists($this, $property)) {
148 $value = isset($this->{$property}) ? $this->{$property} : NULL;
151 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
157 * Sets a value to an arbitrary property.
159 * @param string $property
160 * The property to use for the value.
161 * @param mixed $value
166 public function set($property, $value) {
167 if (property_exists($this, $property)) {
168 $this->{$property} = $value;
171 $this->additional[$property] = $value;
177 * Gets the human-readable name of the layout definition.
179 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
180 * The human-readable name of the layout definition.
182 public function getLabel() {
187 * Sets the human-readable name of the layout definition.
189 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $label
190 * The human-readable name of the layout definition.
194 public function setLabel($label) {
195 $this->label = $label;
200 * Gets the description of the layout definition.
202 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
203 * The description of the layout definition.
205 public function getDescription() {
206 return $this->description;
210 * Sets the description of the layout definition.
212 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $description
213 * The description of the layout definition.
217 public function setDescription($description) {
218 $this->description = $description;
223 * Gets the human-readable category of the layout definition.
225 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
226 * The human-readable category of the layout definition.
228 public function getCategory() {
229 return $this->category;
233 * Sets the human-readable category of the layout definition.
235 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $category
236 * The human-readable category of the layout definition.
240 public function setCategory($category) {
241 $this->category = $category;
246 * Gets the template name.
248 * @return string|null
249 * The template name, if it exists.
251 public function getTemplate() {
252 return $this->template;
256 * Sets the template name.
258 * @param string|null $template
263 public function setTemplate($template) {
264 $this->template = $template;
269 * Gets the template path.
274 public function getTemplatePath() {
275 return $this->templatePath;
279 * Sets the template path.
281 * @param string $template_path
286 public function setTemplatePath($template_path) {
287 $this->templatePath = $template_path;
292 * Gets the theme hook.
294 * @return string|null
295 * The theme hook, if it exists.
297 public function getThemeHook() {
298 return $this->theme_hook;
302 * Sets the theme hook.
304 * @param string $theme_hook
309 public function setThemeHook($theme_hook) {
310 $this->theme_hook = $theme_hook;
315 * Gets the base path for this layout definition.
320 public function getPath() {
325 * Sets the base path for this layout definition.
327 * @param string $path
332 public function setPath($path) {
338 * Gets the asset library for this layout definition.
340 * @return string|null
341 * The asset library, if it exists.
343 public function getLibrary() {
344 return $this->library;
348 * Sets the asset library for this layout definition.
350 * @param string|null $library
355 public function setLibrary($library) {
356 $this->library = $library;
361 * Gets the icon path for this layout definition.
363 * @return string|null
364 * The icon path, if it exists.
366 public function getIconPath() {
371 * Sets the icon path for this layout definition.
373 * @param string|null $icon
378 public function setIconPath($icon) {
384 * Gets the icon map for this layout definition.
386 * This should not be used if an icon path is specified. See ::getIcon().
388 * @return string[][]|null
389 * The icon map, if it exists.
391 public function getIconMap() {
392 return $this->icon_map;
396 * Sets the icon map for this layout definition.
398 * @param string[][]|null $icon_map
403 public function setIconMap($icon_map) {
404 $this->icon_map = $icon_map;
409 * Builds a render array for an icon representing the layout.
412 * (optional) The width of the icon. Defaults to 125.
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.
422 * A render array for the icon.
424 public function getIcon($width = 125, $height = 150, $stroke_width = NULL, $padding = NULL) {
426 if ($icon_path = $this->getIconPath()) {
429 '#uri' => $icon_path,
431 '#height' => $height,
432 '#alt' => $this->getLabel(),
435 elseif ($icon_map = $this->getIconMap()) {
436 $icon_builder = $this->getIconBuilder()
438 ->setLabel($this->getLabel())
440 ->setHeight($height);
442 $icon_builder->setPadding($padding);
445 $icon_builder->setStrokeWidth($stroke_width);
447 $icon = $icon_builder->build($icon_map);
453 * Wraps the icon builder.
455 * @return \Drupal\Core\Layout\Icon\IconBuilderInterface
458 protected function getIconBuilder() {
459 return \Drupal::service('layout.icon_builder');
463 * Gets the regions for this layout definition.
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.
472 public function getRegions() {
473 return $this->regions;
477 * Sets the regions for this layout definition.
479 * @param array[] $regions
480 * An array of regions, see ::getRegions() for the format.
484 public function setRegions(array $regions) {
485 $this->regions = $regions;
490 * Gets the machine-readable region names.
493 * An array of machine-readable region names.
495 public function getRegionNames() {
496 return array_keys($this->getRegions());
500 * Gets the human-readable region labels.
503 * An array of human-readable region labels.
505 public function getRegionLabels() {
506 $regions = $this->getRegions();
507 return array_combine(array_keys($regions), array_column($regions, 'label'));
511 * Gets the default region.
514 * The machine-readable name of the default region.
516 public function getDefaultRegion() {
517 return $this->default_region;
521 * Sets the default region.
523 * @param string $default_region
524 * The machine-readable name of the default region.
528 public function setDefaultRegion($default_region) {
529 $this->default_region = $default_region;
536 public function getDeriver() {
537 return $this->deriver;
543 public function setDeriver($deriver) {
544 $this->deriver = $deriver;