From a4c915aaff1bebd115d9c7b65b7b506dc167bfd7 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 22 Nov 2018 11:59:49 +0000 Subject: [PATCH] Issue #2936360 by tim.plunkett, samuel.mortenson: Remove duplicate references to the "layout_builder__layout" field from Layout Builder --- core/modules/layout_builder/layout_builder.module | 5 +++-- .../src/Entity/LayoutBuilderEntityViewDisplay.php | 9 +++++---- .../src/Form/LayoutBuilderEntityViewDisplayForm.php | 7 ++++--- .../layout_builder/src/LayoutEntityHelperTrait.php | 5 +++-- .../SectionStorage/OverridesSectionStorage.php | 11 +++++++++-- .../tests/src/Functional/LayoutSectionTest.php | 12 +++--------- .../LayoutBuilderFieldLayoutCompatibilityTest.php | 3 ++- .../tests/src/Kernel/LayoutBuilderInstallTest.php | 5 +++-- .../tests/src/Kernel/LayoutSectionItemListTest.php | 5 +++-- .../tests/src/Unit/OverridesSectionStorageTest.php | 8 ++++---- 10 files changed, 39 insertions(+), 31 deletions(-) diff --git a/core/modules/layout_builder/layout_builder.module b/core/modules/layout_builder/layout_builder.module index 48f62e98cf1..387c649f34b 100644 --- a/core/modules/layout_builder/layout_builder.module +++ b/core/modules/layout_builder/layout_builder.module @@ -19,6 +19,7 @@ use Drupal\layout_builder\Plugin\Block\ExtraFieldBlock; use Drupal\layout_builder\InlineBlockEntityOperations; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Access\AccessResult; +use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; /** * Implements hook_help(). @@ -62,8 +63,8 @@ function layout_builder_entity_type_alter(array &$entity_types) { function layout_builder_form_entity_form_display_edit_form_alter(&$form, FormStateInterface $form_state) { // Hides the Layout Builder field. It is rendered directly in // \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::buildMultiple(). - unset($form['fields']['layout_builder__layout']); - $key = array_search('layout_builder__layout', $form['#fields']); + unset($form['fields'][OverridesSectionStorage::FIELD_NAME]); + $key = array_search(OverridesSectionStorage::FIELD_NAME, $form['#fields']); if ($key !== FALSE) { unset($form['#fields'][$key]); } diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index 0118768f77b..7f10d6f51f9 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -9,6 +9,7 @@ use Drupal\Core\Plugin\Context\EntityContext; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; use Drupal\layout_builder\Section; use Drupal\layout_builder\SectionComponent; use Drupal\layout_builder\SectionStorage\SectionStorageTrait; @@ -110,10 +111,10 @@ class LayoutBuilderEntityViewDisplay extends BaseEntityViewDisplay implements La $bundle = $this->getTargetBundle(); if ($new_value) { - $this->addSectionField($entity_type_id, $bundle, 'layout_builder__layout'); + $this->addSectionField($entity_type_id, $bundle, OverridesSectionStorage::FIELD_NAME); } else { - $this->removeSectionField($entity_type_id, $bundle, 'layout_builder__layout'); + $this->removeSectionField($entity_type_id, $bundle, OverridesSectionStorage::FIELD_NAME); } } @@ -274,8 +275,8 @@ class LayoutBuilderEntityViewDisplay extends BaseEntityViewDisplay implements La * The sections. */ protected function getRuntimeSections(FieldableEntityInterface $entity) { - if ($this->isOverridable() && !$entity->get('layout_builder__layout')->isEmpty()) { - return $entity->get('layout_builder__layout')->getSections(); + if ($this->isOverridable() && !$entity->get(OverridesSectionStorage::FIELD_NAME)->isEmpty()) { + return $entity->get(OverridesSectionStorage::FIELD_NAME)->getSections(); } return $this->getSections(); diff --git a/core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php b/core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php index fea971bbcd6..72edc2f5b78 100644 --- a/core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php +++ b/core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php @@ -7,6 +7,7 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\field_ui\Form\EntityViewDisplayEditForm; use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface; +use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; use Drupal\layout_builder\SectionStorageInterface; /** @@ -48,8 +49,8 @@ class LayoutBuilderEntityViewDisplayForm extends EntityViewDisplayEditForm { $form = parent::form($form, $form_state); // Remove the Layout Builder field from the list. - $form['#fields'] = array_diff($form['#fields'], ['layout_builder__layout']); - unset($form['fields']['layout_builder__layout']); + $form['#fields'] = array_diff($form['#fields'], [OverridesSectionStorage::FIELD_NAME]); + unset($form['fields'][OverridesSectionStorage::FIELD_NAME]); $is_enabled = $this->entity->isLayoutBuilderEnabled(); if ($is_enabled) { @@ -133,7 +134,7 @@ class LayoutBuilderEntityViewDisplayForm extends EntityViewDisplayEditForm { $entity_type = $this->entityTypeManager->getDefinition($display->getTargetEntityTypeId()); $query = $this->entityTypeManager->getStorage($display->getTargetEntityTypeId())->getQuery() - ->exists('layout_builder__layout'); + ->exists(OverridesSectionStorage::FIELD_NAME); if ($bundle_key = $entity_type->getKey('bundle')) { $query->condition($bundle_key, $display->getTargetBundle()); } diff --git a/core/modules/layout_builder/src/LayoutEntityHelperTrait.php b/core/modules/layout_builder/src/LayoutEntityHelperTrait.php index 9124027542e..fc22168a99e 100644 --- a/core/modules/layout_builder/src/LayoutEntityHelperTrait.php +++ b/core/modules/layout_builder/src/LayoutEntityHelperTrait.php @@ -6,6 +6,7 @@ use Drupal\Component\Plugin\DerivativeInspectionInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface; +use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; /** * Methods to help with entities using the layout builder. @@ -65,7 +66,7 @@ trait LayoutEntityHelperTrait { return $entity->getSections(); } elseif ($this->isEntityUsingFieldOverride($entity)) { - return $entity->get('layout_builder__layout')->getSections(); + return $entity->get(OverridesSectionStorage::FIELD_NAME)->getSections(); } return NULL; } @@ -102,7 +103,7 @@ trait LayoutEntityHelperTrait { * TRUE if the entity is using a field for a layout override. */ protected function isEntityUsingFieldOverride(EntityInterface $entity) { - return $entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout'); + return $entity instanceof FieldableEntityInterface && $entity->hasField(OverridesSectionStorage::FIELD_NAME); } } diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php index c623c5ede37..bba10ef67f3 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -32,6 +32,13 @@ use Symfony\Component\Routing\RouteCollection; */ class OverridesSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface, OverridesSectionStorageInterface, SectionStorageLocalTaskProviderInterface { + /** + * The field name used by this storage. + * + * @var string + */ + const FIELD_NAME = 'layout_builder__layout'; + /** * The entity type manager. * @@ -127,8 +134,8 @@ class OverridesSectionStorage extends SectionStorageBase implements ContainerFac if (strpos($id, '.') !== FALSE) { list($entity_type_id, $entity_id) = explode('.', $id, 2); $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id); - if ($entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout')) { - return $entity->get('layout_builder__layout'); + if ($entity instanceof FieldableEntityInterface && $entity->hasField(static::FIELD_NAME)) { + return $entity->get(static::FIELD_NAME); } } throw new \InvalidArgumentException(sprintf('The "%s" ID for the "%s" section storage type is invalid', $id, $this->getStorageType())); diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php index e4af3290c46..e29609b30e7 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\layout_builder\Functional; use Drupal\language\Entity\ConfigurableLanguage; use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay; +use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; use Drupal\layout_builder\Section; use Drupal\layout_builder\SectionComponent; use Drupal\Tests\BrowserTestBase; @@ -20,13 +21,6 @@ class LayoutSectionTest extends BrowserTestBase { */ public static $modules = ['field_ui', 'layout_builder', 'node', 'block_test']; - /** - * The name of the layout section field. - * - * @var string - */ - protected $fieldName = 'layout_builder__layout'; - /** * {@inheritdoc} */ @@ -226,7 +220,7 @@ class LayoutSectionTest extends BrowserTestBase { ]); $entity->addTranslation('es', [ 'title' => 'Translated node title', - $this->fieldName => [ + OverridesSectionStorage::FIELD_NAME => [ [ 'section' => new Section('layout_twocol', [], [ 'foo' => new SectionComponent('foo', 'first', [ @@ -373,7 +367,7 @@ class LayoutSectionTest extends BrowserTestBase { 'value' => 'The node body', ], ], - $this->fieldName => $section_values, + OverridesSectionStorage::FIELD_NAME => $section_values, ]); } diff --git a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderFieldLayoutCompatibilityTest.php b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderFieldLayoutCompatibilityTest.php index 57dacb5b18c..12fd813f4fb 100644 --- a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderFieldLayoutCompatibilityTest.php +++ b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderFieldLayoutCompatibilityTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\layout_builder\Kernel; +use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; use Drupal\layout_builder\Section; /** @@ -56,7 +57,7 @@ class LayoutBuilderFieldLayoutCompatibilityTest extends LayoutBuilderCompatibili // Add a layout override. $this->enableOverrides(); /** @var \Drupal\layout_builder\SectionStorageInterface $field_list */ - $field_list = $this->entity->get('layout_builder__layout'); + $field_list = $this->entity->get(OverridesSectionStorage::FIELD_NAME); $field_list->appendSection(new Section('layout_onecol')); $this->entity->save(); diff --git a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderInstallTest.php b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderInstallTest.php index aa1b9942c85..a3ae736da1c 100644 --- a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderInstallTest.php +++ b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderInstallTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\layout_builder\Kernel; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; use Drupal\layout_builder\Section; /** @@ -35,7 +36,7 @@ class LayoutBuilderInstallTest extends LayoutBuilderCompatibilityTestBase { // Add a layout override. $this->enableOverrides(); $this->entity = $this->reloadEntity($this->entity); - $this->entity->get('layout_builder__layout')->appendSection(new Section('layout_onecol')); + $this->entity->get(OverridesSectionStorage::FIELD_NAME)->appendSection(new Section('layout_onecol')); $this->entity->save(); // The rendered entity has now changed. The non-configurable field is shown @@ -50,7 +51,7 @@ class LayoutBuilderInstallTest extends LayoutBuilderCompatibilityTestBase { $this->assertNotEmpty($this->cssSelect('.layout--onecol')); // Removing the layout restores the original rendering of the entity. - $this->entity->get('layout_builder__layout')->removeSection(0); + $this->entity->get(OverridesSectionStorage::FIELD_NAME)->removeSection(0); $this->entity->save(); $this->assertFieldAttributes($this->entity, $expected_fields); diff --git a/core/modules/layout_builder/tests/src/Kernel/LayoutSectionItemListTest.php b/core/modules/layout_builder/tests/src/Kernel/LayoutSectionItemListTest.php index 5bd354675cb..4231530fd7a 100644 --- a/core/modules/layout_builder/tests/src/Kernel/LayoutSectionItemListTest.php +++ b/core/modules/layout_builder/tests/src/Kernel/LayoutSectionItemListTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\layout_builder\Kernel; use Drupal\entity_test\Entity\EntityTestBaseFieldDisplay; use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay; +use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; /** * Tests the field type for Layout Sections. @@ -42,10 +43,10 @@ class LayoutSectionItemListTest extends SectionStorageTestBase { }, $section_data); $entity = EntityTestBaseFieldDisplay::create([ 'name' => 'The test entity', - 'layout_builder__layout' => $section_data, + OverridesSectionStorage::FIELD_NAME => $section_data, ]); $entity->save(); - return $entity->get('layout_builder__layout'); + return $entity->get(OverridesSectionStorage::FIELD_NAME); } } diff --git a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php index 110ec2021e6..3691fb6c711 100644 --- a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php @@ -112,13 +112,13 @@ class OverridesSectionStorageTest extends UnitTestCase { $entity_storage = $this->prophesize(EntityStorageInterface::class); $entity_without_layout = $this->prophesize(FieldableEntityInterface::class); - $entity_without_layout->hasField('layout_builder__layout')->willReturn(FALSE); - $entity_without_layout->get('layout_builder__layout')->shouldNotBeCalled(); + $entity_without_layout->hasField(OverridesSectionStorage::FIELD_NAME)->willReturn(FALSE); + $entity_without_layout->get(OverridesSectionStorage::FIELD_NAME)->shouldNotBeCalled(); $entity_storage->load('entity_without_layout')->willReturn($entity_without_layout->reveal()); $entity_with_layout = $this->prophesize(FieldableEntityInterface::class); - $entity_with_layout->hasField('layout_builder__layout')->willReturn(TRUE); - $entity_with_layout->get('layout_builder__layout')->willReturn('the_return_value'); + $entity_with_layout->hasField(OverridesSectionStorage::FIELD_NAME)->willReturn(TRUE); + $entity_with_layout->get(OverridesSectionStorage::FIELD_NAME)->willReturn('the_return_value'); $entity_storage->load('entity_with_layout')->willReturn($entity_with_layout->reveal()); $this->entityTypeManager->getStorage($expected_entity_type_id)->willReturn($entity_storage->reveal());