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.
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.
19 class LayoutDefinition extends PluginDefinition implements PluginDefinitionInterface, DerivablePluginDefinitionInterface, DependentPluginDefinitionInterface {
21 use DependentPluginDefinitionTrait;
24 * The name of the deriver of this layout definition, if any.
31 * The human-readable name.
38 * An optional description for advanced layouts.
42 protected $description;
45 * The human-readable category.
52 * The template file to render this layout (relative to the 'path' given).
59 * The path to the template.
63 protected $templatePath;
66 * The theme hook used to render this layout.
70 protected $theme_hook;
73 * Path (relative to the module or theme) to resources like icon or template.
87 * The path to the preview image.
94 * An associative array of regions in this layout.
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.
100 * Any remaining keys may have special meaning for the given layout plugin,
101 * but are undefined here.
105 protected $regions = [];
108 * The default region.
112 protected $default_region;
115 * Any additional properties and values.
119 protected $additional = [];
122 * LayoutDefinition constructor.
124 * @param array $definition
125 * An array of values from the annotation.
127 public function __construct(array $definition) {
128 foreach ($definition as $property => $value) {
129 $this->set($property, $value);
134 * Gets any arbitrary property.
136 * @param string $property
137 * The property to retrieve.
140 * The value for that property, or NULL if the property does not exist.
142 public function get($property) {
143 if (property_exists($this, $property)) {
144 $value = isset($this->{$property}) ? $this->{$property} : NULL;
147 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
153 * Sets a value to an arbitrary property.
155 * @param string $property
156 * The property to use for the value.
157 * @param mixed $value
162 public function set($property, $value) {
163 if (property_exists($this, $property)) {
164 $this->{$property} = $value;
167 $this->additional[$property] = $value;
173 * Gets the human-readable name of the layout definition.
175 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
176 * The human-readable name of the layout definition.
178 public function getLabel() {
183 * Sets the human-readable name of the layout definition.
185 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $label
186 * The human-readable name of the layout definition.
190 public function setLabel($label) {
191 $this->label = $label;
196 * Gets the description of the layout definition.
198 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
199 * The description of the layout definition.
201 public function getDescription() {
202 return $this->description;
206 * Sets the description of the layout definition.
208 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $description
209 * The description of the layout definition.
213 public function setDescription($description) {
214 $this->description = $description;
219 * Gets the human-readable category of the layout definition.
221 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
222 * The human-readable category of the layout definition.
224 public function getCategory() {
225 return $this->category;
229 * Sets the human-readable category of the layout definition.
231 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $category
232 * The human-readable category of the layout definition.
236 public function setCategory($category) {
237 $this->category = $category;
242 * Gets the template name.
244 * @return string|null
245 * The template name, if it exists.
247 public function getTemplate() {
248 return $this->template;
252 * Sets the template name.
254 * @param string|null $template
259 public function setTemplate($template) {
260 $this->template = $template;
265 * Gets the template path.
270 public function getTemplatePath() {
271 return $this->templatePath;
275 * Sets the template path.
277 * @param string $template_path
282 public function setTemplatePath($template_path) {
283 $this->templatePath = $template_path;
288 * Gets the theme hook.
290 * @return string|null
291 * The theme hook, if it exists.
293 public function getThemeHook() {
294 return $this->theme_hook;
298 * Sets the theme hook.
300 * @param string $theme_hook
305 public function setThemeHook($theme_hook) {
306 $this->theme_hook = $theme_hook;
311 * Gets the base path for this layout definition.
316 public function getPath() {
321 * Sets the base path for this layout definition.
323 * @param string $path
328 public function setPath($path) {
334 * Gets the asset library for this layout definition.
336 * @return string|null
337 * The asset library, if it exists.
339 public function getLibrary() {
340 return $this->library;
344 * Sets the asset library for this layout definition.
346 * @param string|null $library
351 public function setLibrary($library) {
352 $this->library = $library;
357 * Gets the icon path for this layout definition.
359 * @return string|null
360 * The icon path, if it exists.
362 public function getIconPath() {
367 * Sets the icon path for this layout definition.
369 * @param string|null $icon
374 public function setIconPath($icon) {
380 * Gets the regions for this layout definition.
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
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.
390 public function getRegions() {
391 return $this->regions;
395 * Sets the regions for this layout definition.
397 * @param array[] $regions
398 * An array of regions, see ::getRegions() for the format.
402 public function setRegions(array $regions) {
403 $this->regions = $regions;
408 * Gets the machine-readable region names.
411 * An array of machine-readable region names.
413 public function getRegionNames() {
414 return array_keys($this->getRegions());
418 * Gets the human-readable region labels.
421 * An array of human-readable region labels.
423 public function getRegionLabels() {
424 $regions = $this->getRegions();
425 return array_combine(array_keys($regions), array_column($regions, 'label'));
429 * Gets the default region.
432 * The machine-readable name of the default region.
434 public function getDefaultRegion() {
435 return $this->default_region;
439 * Sets the default region.
441 * @param string $default_region
442 * The machine-readable name of the default region.
446 public function setDefaultRegion($default_region) {
447 $this->default_region = $default_region;
454 public function getDeriver() {
455 return $this->deriver;
461 public function setDeriver($deriver) {
462 $this->deriver = $deriver;