Commit fe516c1a authored by catch's avatar catch
Browse files

Issue #3264633 by mstrelan, Spokje, dww, larowlan, catch, tim.plunkett,...

Issue #3264633 by mstrelan, Spokje, dww, larowlan, catch, tim.plunkett, bbrala, xjm: Remove \Drupal\layout_builder\QuickEditIntegration and refactor it so that quickedit contrib provides the integration with layout builder
parent 386d79e2
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -192,7 +192,7 @@ protected function serializeField($field, array $context, $format) {
      // @todo Replace this workaround after https://www.drupal.org/node/3043245
      //   or remove the need for this in https://www.drupal.org/node/2942975.
      //   See \Drupal\layout_builder\Normalizer\LayoutEntityDisplayNormalizer.
      if ($context['resource_object']->getResourceType()->getDeserializationTargetClass() === 'Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay' && $context['resource_object']->getField('third_party_settings') === $field) {
      if (is_a($context['resource_object']->getResourceType()->getDeserializationTargetClass(), 'Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay', TRUE) && $context['resource_object']->getField('third_party_settings') === $field) {
        unset($field['layout_builder']['sections']);
      }

+0 −16
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\QuickEditIntegration;

/**
 * Implements hook_help().
@@ -162,12 +161,6 @@ function layout_builder_entity_view_alter(array &$build, EntityInterface $entity
  if ($display instanceof LayoutBuilderEntityViewDisplay && strpos($route_name, 'layout_builder.') === 0) {
    unset($build['#contextual_links']);
  }

  if (\Drupal::moduleHandler()->moduleExists('quickedit')) {
    /** @var \Drupal\layout_builder\QuickEditIntegration $quick_edit_integration */
    $quick_edit_integration = \Drupal::classResolver(QuickEditIntegration::class);
    $quick_edit_integration->entityViewAlter($build, $entity, $display);
  }
}

/**
@@ -351,15 +344,6 @@ function layout_builder_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMa
  }
}

/**
 * Implements hook_quickedit_render_field().
 */
function layout_builder_quickedit_render_field(EntityInterface $entity, $field_name, $view_mode_id, $langcode) {
  /** @var \Drupal\layout_builder\QuickEditIntegration $quick_edit_integration */
  $quick_edit_integration = \Drupal::classResolver(QuickEditIntegration::class);
  return $quick_edit_integration->quickEditRenderField($entity, $field_name, $view_mode_id, $langcode);
}

/**
 * Implements hook_entity_translation_create().
 */
+1 −39
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\layout_builder\LayoutEntityHelperTrait;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\QuickEditIntegration;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
use Drupal\layout_builder\SectionListTrait;
@@ -473,7 +472,7 @@ private function sectionStorageManager() {
   * {@inheritdoc}
   */
  public function getComponent($name) {
    if ($this->isLayoutBuilderEnabled() && $section_component = $this->getQuickEditSectionComponent() ?: $this->getSectionComponentForFieldName($name)) {
    if ($this->isLayoutBuilderEnabled() && $section_component = $this->getSectionComponentForFieldName($name)) {
      $plugin = $section_component->getPlugin();
      if ($plugin instanceof ConfigurableInterface) {
        $configuration = $plugin->getConfiguration();
@@ -485,43 +484,6 @@ public function getComponent($name) {
    return parent::getComponent($name);
  }

  /**
   * Returns the Quick Edit formatter settings.
   *
   * @return \Drupal\layout_builder\SectionComponent|null
   *   The section component if it is available.
   *
   * @see \Drupal\layout_builder\QuickEditIntegration::entityViewAlter()
   * @see \Drupal\quickedit\MetadataGenerator::generateFieldMetadata()
   */
  private function getQuickEditSectionComponent() {
    // To determine the Quick Edit view_mode ID we need an originalMode set.
    if ($original_mode = $this->getOriginalMode()) {
      $parts = explode('-', $original_mode);
      // The Quick Edit view mode ID is created by
      // \Drupal\layout_builder\QuickEditIntegration::entityViewAlter()
      // 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) === 6 && $parts[0] === 'layout_builder') {
        [, $delta, $component_uuid, $entity_id] = QuickEditIntegration::deconstructViewModeId($original_mode);
        $entity = $this->entityTypeManager()->getStorage($this->getTargetEntityTypeId())->load($entity_id);
        $sections = $this->getEntitySections($entity);
        if (isset($sections[$delta])) {
          $component = $sections[$delta]->getComponent($component_uuid);
          $plugin = $component->getPlugin();
          // We only care about FieldBlock because these are only components
          // 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;
          }
        }
      }
    }
    return NULL;
  }

  /**
   * Gets the component for a given field name if any.
   *
+30 −0
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\quickedit\Entity\QuickEditLayoutBuilderEntityViewDisplay;
use Drupal\quickedit\LayoutBuilderIntegration;

/**
 * Implements hook_help().
@@ -174,10 +177,28 @@ function quickedit_preprocess_field(&$variables) {
  }
}

/**
 * Implements hook_entity_type_alter().
 */
function quickedit_entity_type_alter(array &$entity_types) {
  if (\Drupal::moduleHandler()->moduleExists('layout_builder')) {
    /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
    if ($entity_types['entity_view_display']->getClass() === LayoutBuilderEntityViewDisplay::class) {
      $entity_types['entity_view_display']->setClass(QuickEditLayoutBuilderEntityViewDisplay::class);
    }
  }
}

/**
 * Implements hook_entity_view_alter().
 */
function quickedit_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  if (\Drupal::moduleHandler()->moduleExists('layout_builder')) {
    /** @var \Drupal\quickedit\LayoutBuilderIntegration $layout_builder_integration */
    $layout_builder_integration = \Drupal::classResolver(LayoutBuilderIntegration::class);
    $layout_builder_integration->entityViewAlter($build, $entity, $display);
  }

  if (isset($build['#embed'])) {
    return;
  }
@@ -189,3 +210,12 @@ function quickedit_entity_view_alter(&$build, EntityInterface $entity, EntityVie

  $build['#attributes']['data-quickedit-entity-id'] = $entity->getEntityTypeId() . '/' . $entity->id();
}

/**
 * Implements hook_quickedit_render_field().
 */
function layout_builder_quickedit_render_field(EntityInterface $entity, $field_name, $view_mode_id, $langcode) {
  /** @var \Drupal\quickedit\LayoutBuilderIntegration $layout_builder_integration */
  $layout_builder_integration = \Drupal::classResolver(LayoutBuilderIntegration::class);
  return $layout_builder_integration->quickEditRenderField($entity, $field_name, $view_mode_id, $langcode);
}
+68 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\quickedit\Entity;

use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DerivativeInspectionInterface;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\quickedit\LayoutBuilderIntegration;

/**
 * Provides an entity view display entity that has a layout with quickedit.
 */
class QuickEditLayoutBuilderEntityViewDisplay extends LayoutBuilderEntityViewDisplay {

  /**
   * {@inheritdoc}
   */
  public function getComponent($name) {
    if ($this->isLayoutBuilderEnabled() && $section_component = $this->getQuickEditSectionComponent()) {
      $plugin = $section_component->getPlugin();
      if ($plugin instanceof ConfigurableInterface) {
        $configuration = $plugin->getConfiguration();
        if (isset($configuration['formatter'])) {
          return $configuration['formatter'];
        }
      }
    }
    return parent::getComponent($name);
  }

  /**
   * Returns the Quick Edit formatter settings.
   *
   * @return \Drupal\layout_builder\SectionComponent|null
   *   The section component if it is available.
   *
   * @see \Drupal\quickedit\LayoutBuilderIntegration::entityViewAlter()
   * @see \Drupal\quickedit\MetadataGenerator::generateFieldMetadata()
   */
  private function getQuickEditSectionComponent() {
    // To determine the Quick Edit view_mode ID we need an originalMode set.
    if ($original_mode = $this->getOriginalMode()) {
      $parts = explode('-', $original_mode);
      // The Quick Edit view mode ID is created by
      // \Drupal\quickedit\LayoutBuilderIntegration::entityViewAlter()
      // 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) === 6 && $parts[0] === 'layout_builder') {
        [, $delta, $component_uuid, $entity_id] = LayoutBuilderIntegration::deconstructViewModeId($original_mode);
        $entity = $this->entityTypeManager()->getStorage($this->getTargetEntityTypeId())->load($entity_id);
        $sections = $this->getEntitySections($entity);
        if (isset($sections[$delta])) {
          $component = $sections[$delta]->getComponent($component_uuid);
          $plugin = $component->getPlugin();
          // We only care about FieldBlock because these are only components
          // 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;
          }
        }
      }
    }
    return NULL;
  }

}
Loading