Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/layout_builder/src/Section.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | a9cd425dd02b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\layout_builder; | 3 namespace Drupal\layout_builder; |
4 | |
5 use Drupal\Core\Config\Entity\ThirdPartySettingsInterface; | |
4 | 6 |
5 /** | 7 /** |
6 * Provides a domain object for layout sections. | 8 * Provides a domain object for layout sections. |
7 * | 9 * |
8 * A section consists of three parts: | 10 * A section consists of three parts: |
9 * - The layout plugin ID for the layout applied to the section (for example, | 11 * - The layout plugin ID for the layout applied to the section (for example, |
10 * 'layout_onecol'). | 12 * 'layout_onecol'). |
11 * - An array of settings for the layout plugin. | 13 * - An array of settings for the layout plugin. |
12 * - An array of components that can be rendered in the section. | 14 * - An array of components that can be rendered in the section. |
13 * | 15 * |
14 * @internal | |
15 * Layout Builder is currently experimental and should only be leveraged by | |
16 * experimental modules and development releases of contributed modules. | |
17 * See https://www.drupal.org/core/experimental for more information. | |
18 * | |
19 * @see \Drupal\Core\Layout\LayoutDefinition | 16 * @see \Drupal\Core\Layout\LayoutDefinition |
20 * @see \Drupal\layout_builder\SectionComponent | 17 * @see \Drupal\layout_builder\SectionComponent |
21 * | |
22 * @todo Determine whether an interface will be provided for this in | |
23 * https://www.drupal.org/project/drupal/issues/2930334. | |
24 */ | 18 */ |
25 class Section { | 19 class Section implements ThirdPartySettingsInterface { |
26 | 20 |
27 /** | 21 /** |
28 * The layout plugin ID. | 22 * The layout plugin ID. |
29 * | 23 * |
30 * @var string | 24 * @var string |
42 * An array of components, keyed by UUID. | 36 * An array of components, keyed by UUID. |
43 * | 37 * |
44 * @var \Drupal\layout_builder\SectionComponent[] | 38 * @var \Drupal\layout_builder\SectionComponent[] |
45 */ | 39 */ |
46 protected $components = []; | 40 protected $components = []; |
41 | |
42 /** | |
43 * Third party settings. | |
44 * | |
45 * An array of key/value pairs keyed by provider. | |
46 * | |
47 * @var array[] | |
48 */ | |
49 protected $thirdPartySettings = []; | |
47 | 50 |
48 /** | 51 /** |
49 * Constructs a new Section. | 52 * Constructs a new Section. |
50 * | 53 * |
51 * @param string $layout_id | 54 * @param string $layout_id |
52 * The layout plugin ID. | 55 * The layout plugin ID. |
53 * @param array $layout_settings | 56 * @param array $layout_settings |
54 * (optional) The layout plugin settings. | 57 * (optional) The layout plugin settings. |
55 * @param \Drupal\layout_builder\SectionComponent[] $components | 58 * @param \Drupal\layout_builder\SectionComponent[] $components |
56 * (optional) The components. | 59 * (optional) The components. |
57 */ | 60 * @param array[] $third_party_settings |
58 public function __construct($layout_id, array $layout_settings = [], array $components = []) { | 61 * (optional) Any third party settings. |
62 */ | |
63 public function __construct($layout_id, array $layout_settings = [], array $components = [], array $third_party_settings = []) { | |
59 $this->layoutId = $layout_id; | 64 $this->layoutId = $layout_id; |
60 $this->layoutSettings = $layout_settings; | 65 $this->layoutSettings = $layout_settings; |
61 foreach ($components as $component) { | 66 foreach ($components as $component) { |
62 $this->setComponent($component); | 67 $this->setComponent($component); |
63 } | 68 } |
69 $this->thirdPartySettings = $third_party_settings; | |
64 } | 70 } |
65 | 71 |
66 /** | 72 /** |
67 * Returns the renderable array for this section. | 73 * Returns the renderable array for this section. |
68 * | 74 * |
238 * The region name. | 244 * The region name. |
239 * | 245 * |
240 * @return \Drupal\layout_builder\SectionComponent[] | 246 * @return \Drupal\layout_builder\SectionComponent[] |
241 * An array of components in the specified region, sorted by weight. | 247 * An array of components in the specified region, sorted by weight. |
242 */ | 248 */ |
243 protected function getComponentsByRegion($region) { | 249 public function getComponentsByRegion($region) { |
244 $components = array_filter($this->getComponents(), function (SectionComponent $component) use ($region) { | 250 $components = array_filter($this->getComponents(), function (SectionComponent $component) use ($region) { |
245 return $component->getRegion() === $region; | 251 return $component->getRegion() === $region; |
246 }); | 252 }); |
247 uasort($components, function (SectionComponent $a, SectionComponent $b) { | 253 uasort($components, function (SectionComponent $a, SectionComponent $b) { |
248 return $a->getWeight() > $b->getWeight() ? 1 : -1; | 254 return $a->getWeight() > $b->getWeight() ? 1 : -1; |
332 'layout_id' => $this->getLayoutId(), | 338 'layout_id' => $this->getLayoutId(), |
333 'layout_settings' => $this->getLayoutSettings(), | 339 'layout_settings' => $this->getLayoutSettings(), |
334 'components' => array_map(function (SectionComponent $component) { | 340 'components' => array_map(function (SectionComponent $component) { |
335 return $component->toArray(); | 341 return $component->toArray(); |
336 }, $this->getComponents()), | 342 }, $this->getComponents()), |
343 'third_party_settings' => $this->thirdPartySettings, | |
337 ]; | 344 ]; |
338 } | 345 } |
339 | 346 |
340 /** | 347 /** |
341 * Creates an object from an array representation of the section. | 348 * Creates an object from an array representation of the section. |
347 * | 354 * |
348 * @return static | 355 * @return static |
349 * The section object. | 356 * The section object. |
350 */ | 357 */ |
351 public static function fromArray(array $section) { | 358 public static function fromArray(array $section) { |
359 // Ensure expected array keys are present. | |
360 $section += [ | |
361 'layout_id' => '', | |
362 'layout_settings' => [], | |
363 'components' => [], | |
364 'third_party_settings' => [], | |
365 ]; | |
352 return new static( | 366 return new static( |
353 $section['layout_id'], | 367 $section['layout_id'], |
354 $section['layout_settings'], | 368 $section['layout_settings'], |
355 array_map([SectionComponent::class, 'fromArray'], $section['components']) | 369 array_map([SectionComponent::class, 'fromArray'], $section['components']), |
370 $section['third_party_settings'] | |
356 ); | 371 ); |
357 } | 372 } |
358 | 373 |
359 /** | 374 /** |
360 * Magic method: Implements a deep clone. | 375 * Magic method: Implements a deep clone. |
363 foreach ($this->components as $uuid => $component) { | 378 foreach ($this->components as $uuid => $component) { |
364 $this->components[$uuid] = clone $component; | 379 $this->components[$uuid] = clone $component; |
365 } | 380 } |
366 } | 381 } |
367 | 382 |
383 /** | |
384 * {@inheritdoc} | |
385 */ | |
386 public function getThirdPartySetting($provider, $key, $default = NULL) { | |
387 return isset($this->thirdPartySettings[$provider][$key]) ? $this->thirdPartySettings[$provider][$key] : $default; | |
388 } | |
389 | |
390 /** | |
391 * {@inheritdoc} | |
392 */ | |
393 public function getThirdPartySettings($provider) { | |
394 return isset($this->thirdPartySettings[$provider]) ? $this->thirdPartySettings[$provider] : []; | |
395 } | |
396 | |
397 /** | |
398 * {@inheritdoc} | |
399 */ | |
400 public function setThirdPartySetting($provider, $key, $value) { | |
401 $this->thirdPartySettings[$provider][$key] = $value; | |
402 return $this; | |
403 } | |
404 | |
405 /** | |
406 * {@inheritdoc} | |
407 */ | |
408 public function unsetThirdPartySetting($provider, $key) { | |
409 unset($this->thirdPartySettings[$provider][$key]); | |
410 // If the third party is no longer storing any information, completely | |
411 // remove the array holding the settings for this provider. | |
412 if (empty($this->thirdPartySettings[$provider])) { | |
413 unset($this->thirdPartySettings[$provider]); | |
414 } | |
415 return $this; | |
416 } | |
417 | |
418 /** | |
419 * {@inheritdoc} | |
420 */ | |
421 public function getThirdPartyProviders() { | |
422 return array_keys($this->thirdPartySettings); | |
423 } | |
424 | |
368 } | 425 } |