LayoutDefault.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Drupal\Core\Layout;
  3. use Drupal\Component\Utility\NestedArray;
  4. use Drupal\Core\Plugin\PluginBase;
  5. /**
  6. * Provides a default class for Layout plugins.
  7. */
  8. class LayoutDefault extends PluginBase implements LayoutInterface {
  9. /**
  10. * The layout definition.
  11. *
  12. * @var \Drupal\Core\Layout\LayoutDefinition
  13. */
  14. protected $pluginDefinition;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  19. parent::__construct($configuration, $plugin_id, $plugin_definition);
  20. $this->setConfiguration($configuration);
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function build(array $regions) {
  26. // Ensure $build only contains defined regions and in the order defined.
  27. $build = [];
  28. foreach ($this->getPluginDefinition()->getRegionNames() as $region_name) {
  29. if (array_key_exists($region_name, $regions)) {
  30. $build[$region_name] = $regions[$region_name];
  31. }
  32. }
  33. $build['#settings'] = $this->getConfiguration();
  34. $build['#layout'] = $this->pluginDefinition;
  35. $build['#theme'] = $this->pluginDefinition->getThemeHook();
  36. if ($library = $this->pluginDefinition->getLibrary()) {
  37. $build['#attached']['library'][] = $library;
  38. }
  39. return $build;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getConfiguration() {
  45. return $this->configuration;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function setConfiguration(array $configuration) {
  51. $this->configuration = NestedArray::mergeDeep($this->defaultConfiguration(), $configuration);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function defaultConfiguration() {
  57. return [];
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function calculateDependencies() {
  63. return [];
  64. }
  65. /**
  66. * {@inheritdoc}
  67. *
  68. * @return \Drupal\Core\Layout\LayoutDefinition
  69. */
  70. public function getPluginDefinition() {
  71. return parent::getPluginDefinition();
  72. }
  73. }