LayoutDefault.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Drupal\Core\Layout;
  3. use Drupal\Component\Utility\NestedArray;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Plugin\PluginBase;
  6. use Drupal\Core\Plugin\PluginFormInterface;
  7. /**
  8. * Provides a default class for Layout plugins.
  9. */
  10. class LayoutDefault extends PluginBase implements LayoutInterface, PluginFormInterface {
  11. /**
  12. * The layout definition.
  13. *
  14. * @var \Drupal\Core\Layout\LayoutDefinition
  15. */
  16. protected $pluginDefinition;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  21. parent::__construct($configuration, $plugin_id, $plugin_definition);
  22. $this->setConfiguration($configuration);
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function build(array $regions) {
  28. // Ensure $build only contains defined regions and in the order defined.
  29. $build = [];
  30. foreach ($this->getPluginDefinition()->getRegionNames() as $region_name) {
  31. if (array_key_exists($region_name, $regions)) {
  32. $build[$region_name] = $regions[$region_name];
  33. }
  34. }
  35. $build['#settings'] = $this->getConfiguration();
  36. $build['#layout'] = $this->pluginDefinition;
  37. $build['#theme'] = $this->pluginDefinition->getThemeHook();
  38. if ($library = $this->pluginDefinition->getLibrary()) {
  39. $build['#attached']['library'][] = $library;
  40. }
  41. return $build;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getConfiguration() {
  47. return $this->configuration;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function setConfiguration(array $configuration) {
  53. $this->configuration = NestedArray::mergeDeep($this->defaultConfiguration(), $configuration);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function defaultConfiguration() {
  59. return [
  60. 'label' => '',
  61. ];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function calculateDependencies() {
  67. return [];
  68. }
  69. /**
  70. * {@inheritdoc}
  71. *
  72. * @return \Drupal\Core\Layout\LayoutDefinition
  73. */
  74. public function getPluginDefinition() {
  75. return parent::getPluginDefinition();
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  81. $form['label'] = [
  82. '#type' => 'textfield',
  83. '#title' => $this->t('Administrative label'),
  84. '#default_value' => $this->configuration['label'],
  85. ];
  86. return $form;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  97. $this->configuration['label'] = $form_state->getValue('label');
  98. }
  99. }