Section.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace Drupal\layout_builder;
  3. /**
  4. * Provides a domain object for layout sections.
  5. *
  6. * A section consists of three parts:
  7. * - The layout plugin ID for the layout applied to the section (for example,
  8. * 'layout_onecol').
  9. * - An array of settings for the layout plugin.
  10. * - An array of components that can be rendered in the section.
  11. *
  12. * @internal
  13. * Layout Builder is currently experimental and should only be leveraged by
  14. * experimental modules and development releases of contributed modules.
  15. * See https://www.drupal.org/core/experimental for more information.
  16. *
  17. * @see \Drupal\Core\Layout\LayoutDefinition
  18. * @see \Drupal\layout_builder\SectionComponent
  19. *
  20. * @todo Determine whether an interface will be provided for this in
  21. * https://www.drupal.org/project/drupal/issues/2930334.
  22. */
  23. class Section {
  24. /**
  25. * The layout plugin ID.
  26. *
  27. * @var string
  28. */
  29. protected $layoutId;
  30. /**
  31. * The layout plugin settings.
  32. *
  33. * @var array
  34. */
  35. protected $layoutSettings = [];
  36. /**
  37. * An array of components, keyed by UUID.
  38. *
  39. * @var \Drupal\layout_builder\SectionComponent[]
  40. */
  41. protected $components = [];
  42. /**
  43. * Constructs a new Section.
  44. *
  45. * @param string $layout_id
  46. * The layout plugin ID.
  47. * @param array $layout_settings
  48. * (optional) The layout plugin settings.
  49. * @param \Drupal\layout_builder\SectionComponent[] $components
  50. * (optional) The components.
  51. */
  52. public function __construct($layout_id, array $layout_settings = [], array $components = []) {
  53. $this->layoutId = $layout_id;
  54. $this->layoutSettings = $layout_settings;
  55. foreach ($components as $component) {
  56. $this->setComponent($component);
  57. }
  58. }
  59. /**
  60. * Returns the renderable array for this section.
  61. *
  62. * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
  63. * An array of available contexts.
  64. * @param bool $in_preview
  65. * TRUE if the section is being previewed, FALSE otherwise.
  66. *
  67. * @return array
  68. * A renderable array representing the content of the section.
  69. */
  70. public function toRenderArray(array $contexts = [], $in_preview = FALSE) {
  71. $regions = [];
  72. foreach ($this->getComponents() as $component) {
  73. if ($output = $component->toRenderArray($contexts, $in_preview)) {
  74. $regions[$component->getRegion()][$component->getUuid()] = $output;
  75. }
  76. }
  77. return $this->getLayout()->build($regions);
  78. }
  79. /**
  80. * Gets the layout plugin for this section.
  81. *
  82. * @return \Drupal\Core\Layout\LayoutInterface
  83. * The layout plugin.
  84. */
  85. public function getLayout() {
  86. return $this->layoutPluginManager()->createInstance($this->getLayoutId(), $this->getLayoutSettings());
  87. }
  88. /**
  89. * Gets the layout plugin ID for this section.
  90. *
  91. * @return string
  92. * The layout plugin ID.
  93. *
  94. * @internal
  95. * This method should only be used by code responsible for storing the data.
  96. */
  97. public function getLayoutId() {
  98. return $this->layoutId;
  99. }
  100. /**
  101. * Gets the layout plugin settings for this section.
  102. *
  103. * @return mixed[]
  104. * The layout plugin settings.
  105. *
  106. * @internal
  107. * This method should only be used by code responsible for storing the data.
  108. */
  109. public function getLayoutSettings() {
  110. return $this->layoutSettings;
  111. }
  112. /**
  113. * Sets the layout plugin settings for this section.
  114. *
  115. * @param mixed[] $layout_settings
  116. * The layout plugin settings.
  117. *
  118. * @return $this
  119. */
  120. public function setLayoutSettings(array $layout_settings) {
  121. $this->layoutSettings = $layout_settings;
  122. return $this;
  123. }
  124. /**
  125. * Gets the default region.
  126. *
  127. * @return string
  128. * The machine-readable name of the default region.
  129. */
  130. public function getDefaultRegion() {
  131. return $this->layoutPluginManager()->getDefinition($this->getLayoutId())->getDefaultRegion();
  132. }
  133. /**
  134. * Returns the components of the section.
  135. *
  136. * @return \Drupal\layout_builder\SectionComponent[]
  137. * The components.
  138. */
  139. public function getComponents() {
  140. return $this->components;
  141. }
  142. /**
  143. * Gets the component for a given UUID.
  144. *
  145. * @param string $uuid
  146. * The UUID of the component to retrieve.
  147. *
  148. * @return \Drupal\layout_builder\SectionComponent
  149. * The component.
  150. *
  151. * @throws \InvalidArgumentException
  152. * Thrown when the expected UUID does not exist.
  153. */
  154. public function getComponent($uuid) {
  155. if (!isset($this->components[$uuid])) {
  156. throw new \InvalidArgumentException(sprintf('Invalid UUID "%s"', $uuid));
  157. }
  158. return $this->components[$uuid];
  159. }
  160. /**
  161. * Helper method to set a component.
  162. *
  163. * @param \Drupal\layout_builder\SectionComponent $component
  164. * The component.
  165. *
  166. * @return $this
  167. */
  168. protected function setComponent(SectionComponent $component) {
  169. $this->components[$component->getUuid()] = $component;
  170. return $this;
  171. }
  172. /**
  173. * Removes a given component from a region.
  174. *
  175. * @param string $uuid
  176. * The UUID of the component to remove.
  177. *
  178. * @return $this
  179. */
  180. public function removeComponent($uuid) {
  181. unset($this->components[$uuid]);
  182. return $this;
  183. }
  184. /**
  185. * Appends a component to the end of a region.
  186. *
  187. * @param \Drupal\layout_builder\SectionComponent $component
  188. * The component being appended.
  189. *
  190. * @return $this
  191. */
  192. public function appendComponent(SectionComponent $component) {
  193. $component->setWeight($this->getNextHighestWeight($component->getRegion()));
  194. $this->setComponent($component);
  195. return $this;
  196. }
  197. /**
  198. * Returns the next highest weight of the component in a region.
  199. *
  200. * @param string $region
  201. * The region name.
  202. *
  203. * @return int
  204. * A number higher than the highest weight of the component in the region.
  205. */
  206. protected function getNextHighestWeight($region) {
  207. $components = $this->getComponentsByRegion($region);
  208. $weights = array_map(function (SectionComponent $component) {
  209. return $component->getWeight();
  210. }, $components);
  211. return $weights ? max($weights) + 1 : 0;
  212. }
  213. /**
  214. * Gets the components for a specific region.
  215. *
  216. * @param string $region
  217. * The region name.
  218. *
  219. * @return \Drupal\layout_builder\SectionComponent[]
  220. * An array of components in the specified region, sorted by weight.
  221. */
  222. protected function getComponentsByRegion($region) {
  223. $components = array_filter($this->getComponents(), function (SectionComponent $component) use ($region) {
  224. return $component->getRegion() === $region;
  225. });
  226. uasort($components, function (SectionComponent $a, SectionComponent $b) {
  227. return $a->getWeight() > $b->getWeight() ? 1 : -1;
  228. });
  229. return $components;
  230. }
  231. /**
  232. * Inserts a component after a specified existing component.
  233. *
  234. * @param string $preceding_uuid
  235. * The UUID of the existing component to insert after.
  236. * @param \Drupal\layout_builder\SectionComponent $component
  237. * The component being inserted.
  238. *
  239. * @return $this
  240. *
  241. * @throws \InvalidArgumentException
  242. * Thrown when the expected UUID does not exist.
  243. */
  244. public function insertAfterComponent($preceding_uuid, SectionComponent $component) {
  245. // Find the delta of the specified UUID.
  246. $uuids = array_keys($this->getComponentsByRegion($component->getRegion()));
  247. $delta = array_search($preceding_uuid, $uuids, TRUE);
  248. if ($delta === FALSE) {
  249. throw new \InvalidArgumentException(sprintf('Invalid preceding UUID "%s"', $preceding_uuid));
  250. }
  251. return $this->insertComponent($delta + 1, $component);
  252. }
  253. /**
  254. * Inserts a component at a specified delta.
  255. *
  256. * @param int $delta
  257. * The zero-based delta in which to insert the component.
  258. * @param \Drupal\layout_builder\SectionComponent $new_component
  259. * The component being inserted.
  260. *
  261. * @return $this
  262. *
  263. * @throws \OutOfBoundsException
  264. * Thrown when the specified delta is invalid.
  265. */
  266. public function insertComponent($delta, SectionComponent $new_component) {
  267. $components = $this->getComponentsByRegion($new_component->getRegion());
  268. $count = count($components);
  269. if ($delta > $count) {
  270. throw new \OutOfBoundsException(sprintf('Invalid delta "%s" for the "%s" component', $delta, $new_component->getUuid()));
  271. }
  272. // If the delta is the end of the list, append the component instead.
  273. if ($delta === $count) {
  274. return $this->appendComponent($new_component);
  275. }
  276. // Find the weight of the component that exists at the specified delta.
  277. $weight = array_values($components)[$delta]->getWeight();
  278. $this->setComponent($new_component->setWeight($weight++));
  279. // Increase the weight of every subsequent component.
  280. foreach (array_slice($components, $delta) as $component) {
  281. $component->setWeight($weight++);
  282. }
  283. return $this;
  284. }
  285. /**
  286. * Wraps the layout plugin manager.
  287. *
  288. * @return \Drupal\Core\Layout\LayoutPluginManagerInterface
  289. * The layout plugin manager.
  290. */
  291. protected function layoutPluginManager() {
  292. return \Drupal::service('plugin.manager.core.layout');
  293. }
  294. /**
  295. * Returns an array representation of the section.
  296. *
  297. * Only use this method if you are implementing custom storage for sections.
  298. *
  299. * @return array
  300. * An array representation of the section component.
  301. */
  302. public function toArray() {
  303. return [
  304. 'layout_id' => $this->getLayoutId(),
  305. 'layout_settings' => $this->getLayoutSettings(),
  306. 'components' => array_map(function (SectionComponent $component) {
  307. return $component->toArray();
  308. }, $this->getComponents()),
  309. ];
  310. }
  311. /**
  312. * Creates an object from an array representation of the section.
  313. *
  314. * Only use this method if you are implementing custom storage for sections.
  315. *
  316. * @param array $section
  317. * An array of section data in the format returned by ::toArray().
  318. *
  319. * @return static
  320. * The section object.
  321. */
  322. public static function fromArray(array $section) {
  323. return new static(
  324. $section['layout_id'],
  325. $section['layout_settings'],
  326. array_map([SectionComponent::class, 'fromArray'], $section['components'])
  327. );
  328. }
  329. }