.../src/Entity/LayoutBuilderEntityViewDisplay.php | 17 ++++---- .../layout_builder/src/QuickEditIntegration.php | 44 ++++++++++--------- .../LayoutBuilderQuickEditTest.php | 51 +++------------------- .../QuickEditIntegrationTest.php | 4 +- 4 files changed, 40 insertions(+), 76 deletions(-) diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index d74f6648c5..73e24b1fd4 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -492,7 +492,7 @@ public function getComponent($name) { } /** - * Returns the QuickEdit formatter settings. + * Returns the Quick Edit formatter settings. * * @return \Drupal\layout_builder\SectionComponent|null * The section component if it is available. @@ -501,16 +501,14 @@ public function getComponent($name) { * @see \Drupal\quickedit\MetadataGenerator::generateFieldMetadata() */ private function getQuickEditSectionComponent() { - // To determine the view_mode used by QuickEdit we need an originalMode set. + // To determine the Quick Edit view_mode ID we need an originalMode set. if ($original_mode = $this->getOriginalMode()) { $parts = explode('-', $original_mode); - // The QuickEdit view_mode is created by + // The Quick Edit view mode ID is created by // \Drupal\layout_builder\QuickEditIntegration::entityViewAlter() - // by concatenating together the information we need to retrieve the - // Layout Builder component. The created view_mode starts with - // 'layout_builder' to distinguish it from other view_modes. If the third - // part of the is 'component' then this is a field that is rendered by - // Layout Builder. + // concatenating together the information we need to retrieve the Layout + // Builder component. It follows the structure prescribed by the + // documentation of hook_quickedit_render_field(). if (count($parts) === 4 && $parts[0] === 'layout_builder' && $parts[2] === 'component') { list($delta, $component_uuid, $entity_id) = explode('-', $parts[3]); // @todo Explicitly cast delta to an integer, remove this in @@ -523,7 +521,8 @@ private function getQuickEditSectionComponent() { $component = $sections[$delta]->getComponent(str_replace('_', '-', $component_uuid)); $plugin = $component->getPlugin(); // We only care about FieldBlock because these are only components - // that provide QuickEdit integration. + // that provide Quick Edit integration: Quick Edit enables in-place + // editing of fields of entities, not of anything else. if ($plugin instanceof DerivativeInspectionInterface && $plugin->getBaseId() === 'field_block') { return $component; } diff --git a/core/modules/layout_builder/src/QuickEditIntegration.php b/core/modules/layout_builder/src/QuickEditIntegration.php index 11a5379e59..ce69c4c83c 100644 --- a/core/modules/layout_builder/src/QuickEditIntegration.php +++ b/core/modules/layout_builder/src/QuickEditIntegration.php @@ -19,7 +19,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Helper methods for QuickEdit module integration. + * Helper methods for Quick Edit module integration. * * @internal */ @@ -79,8 +79,9 @@ public static function create(ContainerInterface $container) { * Alters the entity view build for Quick Edit compatibility. * * When rendering fields outside of normal view modes, Quick Edit requires - * that modules identify themselves with a view mode in the format - * [module_name]-[id]. + * that modules identify themselves with a view mode ID in the format + * [module_name]-[information the module needs to rerender], as prescribed by + * hook_quickedit_render_field(). * * @param array $build * The built entity render array. @@ -114,9 +115,10 @@ public function entityViewAlter(array &$build, EntityInterface $entity, EntityVi return; } - // Create a hash of the sections and use it in the unique Quick Edit ID. - // Any changes to the sections will result in a different hash, forcing - // Quick Edit to recognize any changes. + // Create a hash of the sections and use it in the unique Quick Edit view + // mode ID. Any changes to the sections will result in a different hash, + // forcing Quick Edit's JavaScript to recognize any changes and retrieve + // up-to-date metadata. $sections_hash = hash('sha256', serialize($section_list->getSections())); // Track each component by their plugin ID, delta, region, and UUID. @@ -155,7 +157,7 @@ public function entityViewAlter(array &$build, EntityInterface $entity, EntityVi } /** - * Generate a unique view mode ID. + * Generates a Quick Edit view mode ID. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. @@ -166,10 +168,10 @@ public function entityViewAlter(array &$build, EntityInterface $entity, EntityVi * @param string $component_uuid * The component UUID. * @param string $sections_hash - * The hash of the sections. + * The hash of the sections; must change whenever the sections change. * * @return string - * The unique view mode ID. + * The Quick Edit view mode ID. * * @see \Drupal\layout_builder\QuickEditIntegration::deconstructViewModeId() */ @@ -187,10 +189,10 @@ private static function getViewModeId(EntityInterface $entity, EntityViewDisplay } /** - * Deconstructs the view mode ID into its constituent parts. + * Deconstructs the Quick Edit view mode ID into its constituent parts. * - * @param string $view_mode_id - * The view mode ID. + * @param string $quick_edit_view_mode_id + * The Quick Edit view mode ID. * * @return array * An array containing the entity view mode ID, the delta, the component @@ -198,10 +200,10 @@ private static function getViewModeId(EntityInterface $entity, EntityViewDisplay * * @see \Drupal\layout_builder\QuickEditIntegration::getViewModeId() */ - private static function deconstructViewModeId($view_mode_id) { - list(, $entity_view_mode, $delta, $component_uuid, $entity_id) = explode('-', $view_mode_id, 7); + private static function deconstructViewModeId($quick_edit_view_mode_id) { + list(, $entity_view_mode_id, $delta, $component_uuid, $entity_id) = explode('-', $quick_edit_view_mode_id, 7); return [ - $entity_view_mode, + $entity_view_mode_id, // @todo Explicitly cast delta to an integer, remove this in // https://www.drupal.org/project/drupal/issues/2984509. (int) $delta, @@ -218,8 +220,8 @@ private static function deconstructViewModeId($view_mode_id) { * The entity. * @param string $field_name * The field name. - * @param string $view_mode_id - * The view mode ID. + * @param string $quick_edit_view_mode_id + * The Quick Edit view mode ID. * @param string $langcode * The language code. * @@ -228,8 +230,8 @@ private static function deconstructViewModeId($view_mode_id) { * * @internal */ - public function quickEditRenderField(FieldableEntityInterface $entity, $field_name, $view_mode_id, $langcode) { - list($entity_view_mode, $delta, $component_uuid) = static::deconstructViewModeId($view_mode_id); + public function quickEditRenderField(FieldableEntityInterface $entity, $field_name, $quick_edit_view_mode_id, $langcode) { + list($entity_view_mode, $delta, $component_uuid) = static::deconstructViewModeId($quick_edit_view_mode_id); $entity_build = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId())->view($entity, $entity_view_mode, $langcode); $this->buildEntityView($entity_build); @@ -278,7 +280,7 @@ private function buildEntityView(array &$elements) { } /** - * Determines whether a component has QuickEdit support. + * Determines whether a component has Quick Edit support. * * Only field_block components for view configurable fields should be * supported. @@ -289,7 +291,7 @@ private function buildEntityView(array &$elements) { * The entity being displayed. * * @return bool - * Whether QuickEdit is supported on the component. + * Whether Quick Edit is supported on the component. * * @see \Drupal\layout_builder\Plugin\Block\FieldBlock */ diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderQuickEditTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderQuickEditTest.php index 84f50ece44..f82120a160 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderQuickEditTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderQuickEditTest.php @@ -56,12 +56,11 @@ protected function setUp() { 'type' => 'datetime', ]); $field_storage->save(); - $field = FieldConfig::create([ + FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => 'article', 'label' => 'Date field', - ]); - $field->save(); + ])->save(); // Save the current user to re-login after Layout Builder changes. $user = $this->loggedInUser; @@ -218,42 +217,6 @@ protected function replaceLayoutBuilderFieldIdKeys(array $array) { return $replacement; } - /** - * Gets view mode that Layout Builder used for a field. - * - * @param string $entity_type - * The entity type. - * @param int $entity_id - * The entity ID. - * @param string $field_name - * The field name. - * @param string $view_mode - * The view mode. - * - * @return string - * The view mode used by Layout Builder. - */ - protected function getLayoutBuilderViewMode($entity_type, $entity_id, $field_name, $view_mode) { - // If the field is one that is not rendered by Layout Builder do not change - // $view_mode. - if (in_array($field_name, ['title', 'uid', 'created'])) { - return $view_mode; - } - /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - $entity = $this->container->get('entity_type.manager')->getStorage($entity_type)->load($entity_id); - $view_display = EntityViewDisplay::collectRenderDisplay($entity, 'default'); - $sections = $view_display->getSections(); - // Find the component with the plugin ID in the field_block format that - // matches the entity type, bundle, and field name. - foreach (reset($sections)->getComponents() as $component) { - $component_in_view_mode = str_replace('-', '_', $component->getUuid()); - if ($component->getPlugin()->getPluginId() === "field_block:$entity_type:{$entity->bundle()}:$field_name") { - return 'layout_builder-0-' . $component_in_view_mode . '-' . $entity->id(); - } - } - $this->fail("Component not found for: $entity_type, $entity_id, $field_name"); - } - /** * Login the Layout admin user for the test. */ @@ -330,7 +293,7 @@ protected function revertLayoutToDefaults($layout_url) { } /** - * Disable Layout Builder. + * Disables Layout Builder. * * @param string $path * The path to the manage display page. @@ -349,7 +312,7 @@ protected function disableLayoutBuilder($path) { } /** - * Asserts that QuickEdit is initialized on the node view correctly. + * Asserts that Quick Edit is initialized on the node view correctly. * * @todo Replace calls to this method with calls to ::doTestArticle() in * https://www.drupal.org/node/3037436. @@ -380,14 +343,14 @@ private function assertQuickEditInit(NodeInterface $node) { protected function getQuickEditFieldId($original_field_id) { $page = $this->getSession()->getPage(); $parts = explode('/', $original_field_id); - // Remove the last part of the field id which will contain the QuickEdit - // view_mode. When using the Layout Builder the view_mode will contain a + // Removes the last part of the field id which will contain the Quick Edit + // view mode ID. When using the Layout Builder the view_mode will contain a // hash of the layout sections and will be different each time the layout // changes. array_pop($parts); $field_key_without_view_mode = implode('/', $parts); $element = $page->find('css', "[data-quickedit-field-id^=\"$field_key_without_view_mode\"]"); - $this->assertNotEmpty($element, "Found QuickEdit element starting with: $field_key_without_view_mode"); + $this->assertNotEmpty($element, "Found Quick Edit-enabled field whose data-quickedit-field attribute starts with: $field_key_without_view_mode"); return $element->getAttribute('data-quickedit-field-id'); } diff --git a/core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditIntegrationTest.php b/core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditIntegrationTest.php index b617167740..e212857d7c 100644 --- a/core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditIntegrationTest.php +++ b/core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditIntegrationTest.php @@ -128,7 +128,7 @@ public function testArticleNode() { } /** - * Tests an article with QuickEdit. + * Tests an article with Quick Edit. * * @param \Drupal\node\NodeInterface $node * The node. @@ -362,7 +362,7 @@ protected function createNodeWithTerm() { } /** - * Gets the QuickEdit field ID attribute value. + * Gets the Quick Edit field ID attribute value. * * @param string $original_field_id * The original field ID.