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 associative array of regions in this layout.
91 * The key of the array is the machine name of the region, and the value is
92 * an associative array with the following keys:
93 * - label: (string) The human-readable name of the region.
95 * Any remaining keys may have special meaning for the given layout plugin,
96 * but are undefined here.
100 protected $regions = [];
103 * The default region.
107 protected $default_region;
110 * Any additional properties and values.
114 protected $additional = [];
117 * LayoutDefinition constructor.
119 * @param array $definition
120 * An array of values from the annotation.
122 public function __construct(array $definition) {
123 foreach ($definition as $property => $value) {
124 $this->set($property, $value);
129 * Gets any arbitrary property.
131 * @param string $property
132 * The property to retrieve.
135 * The value for that property, or NULL if the property does not exist.
137 public function get($property) {
138 if (property_exists($this, $property)) {
139 $value = isset($this->{$property}) ? $this->{$property} : NULL;
142 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
148 * Sets a value to an arbitrary property.
150 * @param string $property
151 * The property to use for the value.
152 * @param mixed $value
157 public function set($property, $value) {
158 if (property_exists($this, $property)) {
159 $this->{$property} = $value;
162 $this->additional[$property] = $value;
168 * Gets the human-readable name of the layout definition.
170 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
171 * The human-readable name of the layout definition.
173 public function getLabel() {
178 * Sets the human-readable name of the layout definition.
180 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $label
181 * The human-readable name of the layout definition.
185 public function setLabel($label) {
186 $this->label = $label;
191 * Gets the description of the layout definition.
193 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
194 * The description of the layout definition.
196 public function getDescription() {
197 return $this->description;
201 * Sets the description of the layout definition.
203 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $description
204 * The description of the layout definition.
208 public function setDescription($description) {
209 $this->description = $description;
214 * Gets the human-readable category of the layout definition.
216 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
217 * The human-readable category of the layout definition.
219 public function getCategory() {
220 return $this->category;
224 * Sets the human-readable category of the layout definition.
226 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $category
227 * The human-readable category of the layout definition.
231 public function setCategory($category) {
232 $this->category = $category;
237 * Gets the template name.
239 * @return string|null
240 * The template name, if it exists.
242 public function getTemplate() {
243 return $this->template;
247 * Sets the template name.
249 * @param string|null $template
254 public function setTemplate($template) {
255 $this->template = $template;
260 * Gets the template path.
265 public function getTemplatePath() {
266 return $this->templatePath;
270 * Sets the template path.
272 * @param string $template_path
277 public function setTemplatePath($template_path) {
278 $this->templatePath = $template_path;
283 * Gets the theme hook.
285 * @return string|null
286 * The theme hook, if it exists.
288 public function getThemeHook() {
289 return $this->theme_hook;
293 * Sets the theme hook.
295 * @param string $theme_hook
300 public function setThemeHook($theme_hook) {
301 $this->theme_hook = $theme_hook;
306 * Gets the base path for this layout definition.
311 public function getPath() {
316 * Sets the base path for this layout definition.
318 * @param string $path
323 public function setPath($path) {
329 * Gets the asset library for this layout definition.
331 * @return string|null
332 * The asset library, if it exists.
334 public function getLibrary() {
335 return $this->library;
339 * Sets the asset library for this layout definition.
341 * @param string|null $library
346 public function setLibrary($library) {
347 $this->library = $library;
352 * Gets the icon path for this layout definition.
354 * @return string|null
355 * The icon path, if it exists.
357 public function getIconPath() {
362 * Sets the icon path for this layout definition.
364 * @param string|null $icon
369 public function setIconPath($icon) {
375 * Gets the regions for this layout definition.
378 * The layout regions. The keys of the array are the machine names of the
379 * regions, and the values are an associative array with the following keys:
380 * - label: (string) The human-readable name of the region.
381 * Any remaining keys may have special meaning for the given layout plugin,
382 * but are undefined here.
384 public function getRegions() {
385 return $this->regions;
389 * Sets the regions for this layout definition.
391 * @param array[] $regions
392 * An array of regions, see ::getRegions() for the format.
396 public function setRegions(array $regions) {
397 $this->regions = $regions;
402 * Gets the machine-readable region names.
405 * An array of machine-readable region names.
407 public function getRegionNames() {
408 return array_keys($this->getRegions());
412 * Gets the human-readable region labels.
415 * An array of human-readable region labels.
417 public function getRegionLabels() {
418 $regions = $this->getRegions();
419 return array_combine(array_keys($regions), array_column($regions, 'label'));
423 * Gets the default region.
426 * The machine-readable name of the default region.
428 public function getDefaultRegion() {
429 return $this->default_region;
433 * Sets the default region.
435 * @param string $default_region
436 * The machine-readable name of the default region.
440 public function setDefaultRegion($default_region) {
441 $this->default_region = $default_region;
448 public function getDeriver() {
449 return $this->deriver;
455 public function setDeriver($deriver) {
456 $this->deriver = $deriver;