Verified Commit 17b3cf71 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420987 by kim.pepper, smustgrave: Convert DataType plugin discovery to attributes

(cherry picked from commit e32a8fd3)
parent 67c22284
Loading
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -4,7 +4,10 @@

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\Attribute\DataType;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\Core\TypedData\TypedData;
@@ -17,15 +20,14 @@
 *
 * In addition to the "entity" data type, this exposes derived
 * "entity:$entity_type" and "entity:$entity_type:$bundle" data types.
 *
 * @DataType(
 *   id = "entity",
 *   label = @Translation("Entity"),
 *   description = @Translation("All kind of entities, e.g. nodes, comments or users."),
 *   deriver = "\Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver",
 *   definition_class = "\Drupal\Core\Entity\TypedData\EntityDataDefinition"
 * )
 */
#[DataType(
  id: "entity",
  label: new TranslatableMarkup("Entity"),
  description: new TranslatableMarkup("All kind of entities, e.g. nodes, comments or users."),
  definition_class: EntityDataDefinition::class,
  deriver: EntityDeriver::class
)]
class EntityAdapter extends TypedData implements \IteratorAggregate, ComplexDataInterface {

  /**
+8 −6
Original line number Diff line number Diff line
@@ -3,7 +3,10 @@
namespace Drupal\Core\Entity\Plugin\DataType;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\Attribute\DataType;
use Drupal\Core\TypedData\DataReferenceBase;
use Drupal\Core\TypedData\DataReferenceDefinition;

/**
 * Defines an 'entity_reference' data type.
@@ -24,13 +27,12 @@
 * \Drupal\Core\TypedData\DataReferenceDefinition::create('entity')
 *   ->setTargetDefinition($definition);
 * @endcode
 *
 * @DataType(
 *   id = "entity_reference",
 *   label = @Translation("Entity reference"),
 *   definition_class = "\Drupal\Core\TypedData\DataReferenceDefinition"
 * )
 */
#[DataType(
  id: "entity_reference",
  label: new TranslatableMarkup("Entity reference"),
  definition_class: DataReferenceDefinition::class,
)]
class EntityReference extends DataReferenceBase {

  /**
+11 −7
Original line number Diff line number Diff line
@@ -2,19 +2,23 @@

namespace Drupal\Core\Field\Plugin\DataType;

use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Field\Plugin\DataType\Deriver\FieldItemDeriver;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\Attribute\DataType;

/**
 * Defines the base plugin for deriving data types for field types.
 *
 * Note that the class only register the plugin, and is actually never used.
 * \Drupal\Core\Field\FieldItemBase is available for use as base class.
 *
 * @DataType(
 *   id = "field_item",
 *   label = @Translation("Field item"),
 *   list_class = "\Drupal\Core\Field\FieldItemList",
 *   deriver = "Drupal\Core\Field\Plugin\DataType\Deriver\FieldItemDeriver"
 * )
 */
#[DataType(
  id: "field_item",
  label: new TranslatableMarkup("Field item"),
  list_class: FieldItemList::class,
  deriver: FieldItemDeriver::class
)]
abstract class FieldItem {

}
+80 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Core\TypedData\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\ListDataDefinition;
use Drupal\Core\TypedData\Plugin\DataType\ItemList;

/**
 * Defines a data type attribute.
 *
 * The typed data API allows modules to support any kind of data based upon
 * pre-defined primitive types and interfaces for complex data and lists.
 *
 * Defined data types may map to one of the pre-defined primitive types below
 * \Drupal\Core\TypedData\Type or may be complex data types, containing
 * one or more data properties. Typed data objects for complex data types have
 * to implement the \Drupal\Core\TypedData\ComplexDataInterface. Further
 * interfaces that may be implemented are:
 *  - \Drupal\Core\Access\AccessibleInterface
 *  - \Drupal\Core\TypedData\TranslatableInterface
 *
 * Furthermore, lists of data items are represented by objects implementing the
 * \Drupal\Core\TypedData\ListInterface. A list contains items of the same data
 * type, is ordered and may contain duplicates. The class used for a list of
 * items of a certain type may be specified using the 'list class' key.
 *
 * @see \Drupal::typedDataManager()
 * @see \Drupal\Core\TypedData\TypedDataManager::create()
 * @see hook_data_type_info_alter()
 *
 * @ingroup typed_data
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class DataType extends Plugin {

  /**
   * Constructs a new DataType attribute.
   *
   * @param string $id
   *   The data type plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
   *   The human-readable name of the data type.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
   *   (optional) The description of the data type.
   * @param string|null $definition_class
   *   (optional) The definition class to use for defining data of this type.
   * @param string|null $list_class
   *   (optional) The typed data class used for wrapping multiple data items of
   *   the type.
   * @param string|null $list_definition_class
   *   (optional) The definition class to use for defining a list of items of
   *   this type.
   * @param array $constraints
   *   (optional) An array of validation constraints for this type.
   * @param bool $unwrap_for_canonical_representation
   *   Whether the typed object wraps the canonical representation of the data.
   * @param string|null $deriver
   *   (optional) The deriver class for the data type.
   *
   * @see \Drupal\Core\TypedData\TypedDataManager::getConstraints()
   * @see \Drupal\Core\TypedData\TypedDataManager::getCanonicalRepresentation()
   */
  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $label,
    public readonly ?TranslatableMarkup $description = NULL,
    public readonly ?string $definition_class = DataDefinition::class,
    public readonly ?string $list_class = ItemList::class,
    public readonly ?string $list_definition_class = ListDataDefinition::class,
    public readonly array $constraints = [],
    public readonly bool $unwrap_for_canonical_representation = TRUE,
    public readonly ?string $deriver = NULL,
  ) {}

}
+6 −5
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\Core\TypedData\Plugin\DataType;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\Attribute\DataType;
use Drupal\Core\TypedData\TypedData;

/**
@@ -10,12 +12,11 @@
 * The "any" data type does not implement a list or complex data interface, nor
 * is it mappable to any primitive type. Thus, it may contain any PHP data for
 * which no further metadata is available.
 *
 * @DataType(
 *   id = "any",
 *   label = @Translation("Any data")
 * )
 */
#[DataType(
  id: "any",
  label: new TranslatableMarkup("Any data")
)]
class Any extends TypedData {

  /**
Loading