Verified Commit 36c73975 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420983 by sorlov, godotislate, alexpott, quietone, kim.pepper,...

Issue #3420983 by sorlov, godotislate, alexpott, quietone, kim.pepper, larowlan, mstrelan: Convert Layout plugin discovery to attributes

(cherry picked from commit 091483c1)
parent f2fb38ac
Loading
Loading
Loading
Loading
Loading
+111 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Layout\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Layout\LayoutDefault;
use Drupal\Core\Layout\LayoutDefinition;

/**
 * Defines a Layout attribute object.
 *
 * Layouts are used to define a list of regions and then output render arrays
 * in each of the regions, usually using a template.
 *
 * Plugin Namespace: Plugin\Layout
 *
 * @see \Drupal\Core\Layout\LayoutInterface
 * @see \Drupal\Core\Layout\LayoutDefault
 * @see \Drupal\Core\Layout\LayoutPluginManager
 * @see plugin_api
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class Layout extends Plugin {

  /**
   * Any additional properties and values.
   *
   * @see \Drupal\Core\Layout\LayoutDefinition::$additional
   *
   * @var array
   */
  public readonly array $additional;

  /**
   * Constructs a Layout attribute.
   *
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $label
   *   (optional) The human-readable name. @todo Deprecate optional label in
   *   https://www.drupal.org/project/drupal/issues/3392572.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $category
   *   (optional) The human-readable category. @todo Deprecate optional category
   *   in https://www.drupal.org/project/drupal/issues/3392572.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
   *   (optional) The description for advanced layouts.
   * @param string|null $template
   *   (optional) The template file to render the layout.
   * @param string $theme_hook
   *   (optional) The template hook to render the layout.
   * @param string|null $path
   *   (optional) Path (relative to the module or theme) to resources like icon or template.
   * @param string|null $library
   *   (optional) The asset library.
   * @param string|null $icon
   *   (optional) The path to the preview image (relative to the 'path' given).
   * @param string[][]|null $icon_map
   *   (optional) The icon map.
   * @param array $regions
   *   (optional) An associative array of regions in this layout.
   * @param string|null $default_region
   *   (optional) The default region.
   * @param class-string $class
   *   (optional) The layout plugin class.
   * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface[] $context_definitions
   *   (optional) The context definition.
   * @param array $config_dependencies
   *   (optional) The config dependencies.
   * @param class-string|null $deriver
   *   (optional) The deriver class.
   * @param mixed $additional
   *   (optional) Additional properties passed in that can be used by a deriver.
   */
  public function __construct(
    public readonly string $id,
    public readonly ?TranslatableMarkup $label = NULL,
    public readonly ?TranslatableMarkup $category = NULL,
    public readonly ?TranslatableMarkup $description = NULL,
    public readonly ?string $template = NULL,
    public readonly string $theme_hook = 'layout',
    public readonly ?string $path = NULL,
    public readonly ?string $library = NULL,
    public readonly ?string $icon = NULL,
    public readonly ?array $icon_map = NULL,
    public readonly array $regions = [],
    public readonly ?string $default_region = NULL,
    public string $class = LayoutDefault::class,
    public readonly array $context_definitions = [],
    public readonly array $config_dependencies = [],
    public readonly ?string $deriver = NULL,
    ...$additional,
  ) {
    // Layout definitions support arbitrary properties being passed in, which
    // are stored in the 'additional' property in LayoutDefinition. The variadic
    // 'additional' parameter here saves arbitrary parameters passed into the
    // 'additional' property in this attribute class. The 'additional' property
    // gets passed to the LayoutDefinition constructor in ::get().
    // @see \Drupal\Core\Layout\LayoutDefinition::$additional
    // @see \Drupal\Core\Layout\LayoutDefinition::get()
    $this->additional = $additional;
  }

  /**
   * {@inheritdoc}
   */
  public function get(): LayoutDefinition {
    return new LayoutDefinition(parent::get());
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ class LayoutDefinition extends PluginDefinition implements PluginDefinitionInter
   * LayoutDefinition constructor.
   *
   * @param array $definition
   *   An array of values from the annotation.
   *   An array of values from the attribute.
   */
  public function __construct(array $definition) {
    // If there are context definitions in the plugin definition, they should
+6 −6
Original line number Diff line number Diff line
@@ -2,16 +2,16 @@

namespace Drupal\Core\Layout;

use Drupal\Component\Annotation\Plugin\Discovery\AnnotationBridgeDecorator;
use Drupal\Component\Plugin\Discovery\AttributeBridgeDecorator;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\AttributeDiscoveryWithAnnotations;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator;
use Drupal\Core\Layout\Annotation\Layout;
use Drupal\Core\Layout\Attribute\Layout;
use Drupal\Core\Plugin\FilteredPluginManagerTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;

@@ -43,7 +43,7 @@ class LayoutPluginManager extends DefaultPluginManager implements LayoutPluginMa
   *   The theme handler to invoke the alter hook with.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
    parent::__construct('Plugin/Layout', $namespaces, $module_handler, LayoutInterface::class, Layout::class);
    parent::__construct('Plugin/Layout', $namespaces, $module_handler, LayoutInterface::class, Layout::class, 'Drupal\Core\Layout\Annotation\Layout');
    $this->themeHandler = $theme_handler;

    $type = $this->getType();
@@ -70,13 +70,13 @@ protected function providerExists($provider) {
   */
  protected function getDiscovery() {
    if (!$this->discovery) {
      $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
      $discovery = new AttributeDiscoveryWithAnnotations($this->subdir, $this->namespaces, $this->pluginDefinitionAttributeName, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
      $discovery = new YamlDiscoveryDecorator($discovery, 'layouts', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories());
      $discovery
        ->addTranslatableProperty('label')
        ->addTranslatableProperty('description')
        ->addTranslatableProperty('category');
      $discovery = new AnnotationBridgeDecorator($discovery, $this->pluginDefinitionAnnotationName);
      $discovery = new AttributeBridgeDecorator($discovery, $this->pluginDefinitionAttributeName);
      $discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
      $this->discovery = $discovery;
    }
+17 −16
Original line number Diff line number Diff line
@@ -2,26 +2,27 @@

namespace Drupal\field_layout_test\Plugin\Layout;

use Drupal\Core\Layout\Attribute\Layout;
use Drupal\Core\Layout\LayoutDefault;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Provides an annotated layout plugin for field_layout tests.
 *
 * @Layout(
 *   id = "test_layout_content_and_footer",
 *   label = @Translation("Test plugin: Content and Footer"),
 *   category = @Translation("Layout test"),
 *   description = @Translation("Test layout"),
 *   regions = {
 *     "content" = {
 *       "label" = @Translation("Content Region")
 *     },
 *     "footer" = {
 *       "label" = @Translation("Footer Region")
 *     }
 *   },
 * )
 * Provides a Layout plugin for field_layout tests.
 */
#[Layout(
  id: 'test_layout_content_and_footer',
  label: new TranslatableMarkup('Test plugin: Content and Footer'),
  category: new TranslatableMarkup('Layout test'),
  description: new TranslatableMarkup('Test layout'),
  regions: [
    "content" => [
      "label" => new TranslatableMarkup("Content Region"),
    ],
    "footer" => [
      "label" => new TranslatableMarkup("Footer Region"),
    ],
  ],
)]
class TestLayoutContentFooter extends LayoutDefault {

}
+22 −21
Original line number Diff line number Diff line
@@ -2,31 +2,32 @@

namespace Drupal\field_layout_test\Plugin\Layout;

use Drupal\Core\Layout\Attribute\Layout;
use Drupal\Core\Layout\LayoutDefault;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Provides an annotated layout plugin for field_layout tests.
 *
 * @Layout(
 *   id = "test_layout_main_and_footer",
 *   label = @Translation("Test plugin: Main and Footer"),
 *   category = @Translation("Layout test"),
 *   description = @Translation("Test layout"),
 *   regions = {
 *     "main" = {
 *       "label" = @Translation("Main Region")
 *     },
 *     "footer" = {
 *       "label" = @Translation("Footer Region")
 *     }
 *   },
 *   config_dependencies = {
 *     "module" = {
 *       "layout_discovery",
 *     },
 *   },
 * )
 * Provides an attributed layout plugin for field_layout tests.
 */
#[Layout(
  id: 'test_layout_main_and_footer',
  label: new TranslatableMarkup('Test plugin: Main and Footer'),
  category: new TranslatableMarkup('Layout test'),
  description: new TranslatableMarkup('Test layout'),
  regions: [
    "main" => [
      "label" => new TranslatableMarkup("Main Region"),
    ],
    "footer" => [
      "label" => new TranslatableMarkup("Footer Region"),
    ],
  ],
  config_dependencies: [
    "module" => [
      "layout_discovery",
    ],
  ],
)]
class TestLayoutMainFooter extends LayoutDefault {

  /**
Loading