Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Layout / LayoutDefault.php
1 <?php
2
3 namespace Drupal\Core\Layout;
4
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Plugin\PluginBase;
7
8 /**
9  * Provides a default class for Layout plugins.
10  *
11  * @internal
12  *   The layout system is currently experimental and should only be leveraged by
13  *   experimental modules and development releases of contributed modules.
14  *   See https://www.drupal.org/core/experimental for more information.
15  */
16 class LayoutDefault extends PluginBase implements LayoutInterface {
17
18   /**
19    * The layout definition.
20    *
21    * @var \Drupal\Core\Layout\LayoutDefinition
22    */
23   protected $pluginDefinition;
24
25   /**
26    * {@inheritdoc}
27    */
28   public function __construct(array $configuration, $plugin_id, $plugin_definition) {
29     parent::__construct($configuration, $plugin_id, $plugin_definition);
30     $this->setConfiguration($configuration);
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function build(array $regions) {
37     // Ensure $build only contains defined regions and in the order defined.
38     $build = [];
39     foreach ($this->getPluginDefinition()->getRegionNames() as $region_name) {
40       if (array_key_exists($region_name, $regions)) {
41         $build[$region_name] = $regions[$region_name];
42       }
43     }
44     $build['#settings'] = $this->getConfiguration();
45     $build['#layout'] = $this->pluginDefinition;
46     $build['#theme'] = $this->pluginDefinition->getThemeHook();
47     if ($library = $this->pluginDefinition->getLibrary()) {
48       $build['#attached']['library'][] = $library;
49     }
50     return $build;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getConfiguration() {
57     return $this->configuration;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function setConfiguration(array $configuration) {
64     $this->configuration = NestedArray::mergeDeep($this->defaultConfiguration(), $configuration);
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function defaultConfiguration() {
71     return [];
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function calculateDependencies() {
78     return [];
79   }
80
81   /**
82    * {@inheritdoc}
83    *
84    * @return \Drupal\Core\Layout\LayoutDefinition
85    */
86   public function getPluginDefinition() {
87     return parent::getPluginDefinition();
88   }
89
90 }