Verified Commit 31ec7d64 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3421019 by DanielVeza, sorlov, andypost, smustgrave, larowlan,...

Issue #3421019 by DanielVeza, sorlov, andypost, smustgrave, larowlan, mstrelan, alexpott: Convert SectionStorage plugin discovery to attributes

(cherry picked from commit 4fcae3ca)
parent f3938d38
Loading
Loading
Loading
Loading
+65 −0
Changes for core/modules/layout_builder/src/Attribute/SectionStorage.php: 65 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\layout_builder\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\layout_builder\SectionStorage\SectionStorageDefinition;

/**
 * Defines a SectionStorage attribute.
 *
 * Plugin Namespace: Plugin\SectionStorage
 *
 * @see \Drupal\layout_builder\SectionStorage\SectionStorageManager
 * @see plugin_api
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class SectionStorage extends Plugin {

  /**
   * Constructs a SectionStorage attribute.
   *
   * @param string $id
   *   The plugin ID.
   * @param int $weight
   *   (optional) The plugin weight.
   *   When an entity with layout is rendered, section storage plugins are
   *   checked, in order of their weight, to determine which one should be used
   *   to render the layout.
   * @param \Drupal\Component\Plugin\Context\ContextDefinitionInterface[] $context_definitions
   *   (optional) Any required context definitions.
   *   When an entity with layout is rendered, all section storage plugins which
   *   match a particular set of contexts are checked, in order of their weight,
   *   to determine which plugin should be used to render the layout.
   *   @see \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface::findByContext()
   * @param bool $handles_permission_check
   *   (optional) Indicates that this section storage handles its own
   *   permission checking. If FALSE, the 'configure any layout' permission
   *   will be required during routing access. If TRUE, Layout Builder will
   *   not enforce any access restrictions for the storage, so the section
   *   storage's implementation of access() must perform the access checking itself.
   * @param string|null $deriver
   *   (optional) The deriver class.
   */
  public function __construct(
    public readonly string $id,
    public readonly int $weight = 0,
    public readonly array $context_definitions = [],
    public readonly bool $handles_permission_check = FALSE,
    public readonly ?string $deriver = NULL,
  ) {}

  /**
   * {@inheritdoc}
   */
  public function get(): SectionStorageDefinition {
    return new SectionStorageDefinition([
      'id' => $this->id,
      'class' => $this->class,
      'weight' => $this->weight,
      'context_definitions' => $this->context_definitions,
      'handles_permission_check' => $this->handles_permission_check,
    ]);
  }

}
+1 −1
Changes for core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -331,7 +331,7 @@ protected function buildSections(FieldableEntityInterface $entity) {
        $build[$delta] = $section->toRenderArray($contexts);
      }
    }
    // The render array is built based on decisions made by @SectionStorage
    // The render array is built based on decisions made by SectionStorage
    // plugins and therefore it needs to depend on the accumulated
    // cacheability of those decisions.
    $cacheability->applyTo($build);
+15 −9
Changes for core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php: 15 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -11,9 +11,13 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\layout_builder\Attribute\SectionStorage;
use Drupal\layout_builder\DefaultsSectionStorageInterface;
use Drupal\layout_builder\Entity\SampleEntityGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -28,18 +32,20 @@
 * - The default weight is 0, so other custom implementations will also take
 *   precedence unless otherwise specified.
 *
 * @SectionStorage(
 *   id = "defaults",
 *   weight = 20,
 *   context_definitions = {
 *     "display" = @ContextDefinition("entity:entity_view_display"),
 *     "view_mode" = @ContextDefinition("string", default_value = "default"),
 *   },
 * )
 *
 * @internal
 *   Plugin classes are internal.
 */
#[SectionStorage(id: "defaults", weight: 20, context_definitions: [
  "display" => new EntityContextDefinition(
    data_type: "entity_view_display",
    label: new TranslatableMarkup("Entity view display"),
  ),
  'view_mode' => new ContextDefinition(
    data_type: 'string',
    label: new TranslatableMarkup("View mode"),
    default_value: "default",
  ),
])]
class DefaultsSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface, DefaultsSectionStorageInterface {

  /**
+21 −12
Changes for core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php: 21 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -15,7 +15,9 @@
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\layout_builder\Attribute\SectionStorage;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\OverridesSectionStorageInterface;
use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface;
@@ -31,21 +33,28 @@
 * - The default weight is 0, so custom implementations will not take
 *   precedence unless otherwise specified.
 *
 * @SectionStorage(
 *   id = "overrides",
 *   weight = -20,
 *   handles_permission_check = TRUE,
 *   context_definitions = {
 *     "entity" = @ContextDefinition("entity", constraints = {
 *       "EntityHasField" = \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::FIELD_NAME,
 *     }),
 *     "view_mode" = @ContextDefinition("string", default_value = "default"),
 *   }
 * )
 *
 * @internal
 *   Plugin classes are internal.
 */
#[SectionStorage(
  id: "overrides",
  weight: -20,
  context_definitions: [
    'entity' => new ContextDefinition(
      data_type: 'entity',
      label: new TranslatableMarkup("Entity"),
      constraints: [
        "EntityHasField" => OverridesSectionStorage::FIELD_NAME,
      ],
    ),
    'view_mode' => new ContextDefinition(
      data_type: 'string',
      label: new TranslatableMarkup("View mode"),
      default_value: "default",
    ),
  ],
  handles_permission_check: TRUE,
)]
class OverridesSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface, OverridesSectionStorageInterface, SectionStorageLocalTaskProviderInterface {

  /**
+2 −2
Changes for core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\layout_builder\Annotation\SectionStorage;
use Drupal\layout_builder\Attribute\SectionStorage;
use Drupal\layout_builder\SectionStorageInterface;

/**
@@ -43,7 +43,7 @@ class SectionStorageManager extends DefaultPluginManager implements SectionStora
   *   The context handler.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ContextHandlerInterface $context_handler) {
    parent::__construct('Plugin/SectionStorage', $namespaces, $module_handler, SectionStorageInterface::class, SectionStorage::class);
    parent::__construct('Plugin/SectionStorage', $namespaces, $module_handler, SectionStorageInterface::class, SectionStorage::class, '\Drupal\layout_builder\Annotation\SectionStorage');

    $this->contextHandler = $context_handler;

Loading