3 namespace Drupal\layout_plugin\Plugin\Layout;
5 use Drupal\Component\Plugin\ConfigurablePluginInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Plugin\PluginBase;
8 use Drupal\Core\Plugin\PluginFormInterface;
11 * Provides a base class for Layout plugins.
13 abstract class LayoutBase extends PluginBase implements LayoutInterface, ConfigurablePluginInterface, PluginFormInterface {
16 * The layout configuration.
20 protected $configuration = [];
23 * Gets the human-readable name.
25 * @return \Drupal\Core\Annotation\Translation|NULL
26 * The human-readable name.
28 public function getLabel() {
29 return $this->pluginDefinition['label'];
33 * Gets the optional description for advanced layouts.
35 * @return \Drupal\Core\Annotation\Translation|NULL
36 * The layout description.
38 public function getDescription() {
39 return isset($this->pluginDefinition['description']) ? $this->pluginDefinition['description'] : NULL;
43 * Gets the human-readable category.
45 * @return \Drupal\Core\Annotation\Translation
46 * The human-readable category.
48 public function getCategory() {
49 return $this->pluginDefinition['category'];
53 * Gets human-readable list of regions keyed by machine name.
55 * @return \Drupal\Core\Annotation\Translation[]
56 * An array of human-readable region names keyed by machine name.
58 public function getRegionNames() {
59 return $this->pluginDefinition['region_names'];
63 * Gets information on regions keyed by machine name.
66 * An array of information on regions keyed by machine name.
68 public function getRegionDefinitions() {
69 return $this->pluginDefinition['regions'];
73 * Gets the path to resources like icon or template.
76 * The path relative to the Drupal root.
78 public function getBasePath() {
79 return isset($this->pluginDefinition['path']) ? $this->pluginDefinition['path'] : NULL;
83 * Gets the path to the preview image.
85 * This can optionally be used in the user interface to show the layout of
89 * The path to preview image file.
91 public function getIconFilename() {
92 return isset($this->pluginDefinition['icon']) ? $this->pluginDefinition['icon'] : NULL;
96 * Get the asset library.
101 public function getLibrary() {
102 return isset($this->pluginDefinition['library']) ? $this->pluginDefinition['library'] : NULL;
106 * Gets the theme hook used to render this layout.
108 * @return string|NULL
111 public function getThemeHook() {
112 return isset($this->pluginDefinition['theme']) ? $this->pluginDefinition['theme'] : NULL;
118 public function build(array $regions) {
119 $build = array_intersect_key($regions, $this->getRegionDefinitions());
120 $build['#layout'] = $this->getPluginDefinition();
121 $build['#settings'] = $this->getConfiguration();
122 if ($theme = $this->getThemeHook()) {
123 $build['#theme'] = $theme;
125 if ($library = $this->getLibrary()) {
126 $build['#attached']['library'][] = $library;
134 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
141 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
147 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
148 $this->configuration = $form_state->getValues();
154 public function getConfiguration() {
155 return array_merge($this->defaultConfiguration(), $this->configuration);
161 public function setConfiguration(array $configuration) {
162 $this->configuration = $configuration;
168 public function defaultConfiguration() {
175 public function calculateDependencies() {
176 return isset($this->configuration['dependencies']) ? $this->configuration['dependencies'] : [];