Verified Commit 4afc682d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420981 by kim.pepper, quietone, mstrelan, Berdir, alexpott: Convert...

Issue #3420981 by kim.pepper, quietone, mstrelan, Berdir, alexpott: Convert FieldType plugin discovery to attributes

(cherry picked from commit cf095965)
parent dbffe6f4
Loading
Loading
Loading
Loading
Loading
+87 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Core\Field\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a FieldType attribute.
 *
 * Additional attribute keys for field types can be defined in
 * hook_field_info_alter().
 *
 * @ingroup field_types
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class FieldType extends Plugin {

  /**
   * Constructs a FieldType attribute.
   *
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
   *   The human-readable name of the field type.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|array|null $description
   *   (optional) A short human-readable description for the field type.
   * @param string $category
   *   (optional) The category under which the field type should be listed in
   *   the UI.
   * @param int $weight
   *   (optional) The weight of the field type.
   * @param string|null $default_widget
   *   (optional) The plugin_id of the default widget for this field type.
   *   This widget must be available whenever the field type is available (i.e.
   *   provided by the field type module, or by a module the field type module
   *   depends on).
   * @param string|null $default_formatter
   *   (optional) The plugin_id of the default formatter for this field type.
   *   This formatter must be available whenever the field type is available
   *   (i.e. provided by the field type module, or by a module the field type
   *   module depends on).
   * @param bool $no_ui
   *   (optional) A boolean stating that fields of this type cannot be created
   *   through the UI.
   * @param string|null $list_class
   *   (optional) The typed data class used for wrapping multiple data items of
   *   the type. Must implement the \Drupal\Core\TypedData\ListInterface.
   * @param int|null $cardinality
   *   (optional) An integer defining a fixed cardinality for this field type.
   *   If this value is not set, cardinality can be configured in the field UI.
   * @param array $constraints
   *   (optional) An array of validation constraints for this type.
   * @param array $config_dependencies
   *   (optional) An array of configuration dependencies.
   * @param array $column_groups
   *   (optional) An array of column groups for the field type.
   * @param array $serialized_property_names
   *   (optional) An array of property names that should be serialized.
   * @param string|null $deriver
   *   (optional) The deriver class for the data type.
   * @param string|null $module
   *   The name of the module providing the field type plugin.
   */
  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $label,
    public readonly TranslatableMarkup|array|null $description = NULL,
    public readonly string $category = '',
    public readonly int $weight = 0,
    public readonly ?string $default_widget = NULL,
    public readonly ?string $default_formatter = NULL,
    public readonly bool $no_ui = FALSE,
    public readonly ?string $list_class = NULL,
    public readonly ?int $cardinality = NULL,
    public readonly array $constraints = [],
    public readonly array $config_dependencies = [],
    public readonly array $column_groups = [],
    public readonly array $serialized_property_names = [],
    public readonly ?string $deriver = NULL,
    public readonly ?string $module = NULL,
  ) {
  }

}
+10 −1
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\StringTranslation\TranslatableMarkup;
@@ -45,7 +46,15 @@ class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePl
   *   The field type category plugin manager.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, TypedDataManagerInterface $typed_data_manager, protected ?FieldTypeCategoryManagerInterface $fieldTypeCategoryManager = NULL) {
    parent::__construct('Plugin/Field/FieldType', $namespaces, $module_handler, 'Drupal\Core\Field\FieldItemInterface', 'Drupal\Core\Field\Annotation\FieldType');
    parent::__construct(
      'Plugin/Field/FieldType',
      $namespaces,
      $module_handler,
      FieldItemInterface::class,
      FieldType::class,
      'Drupal\Core\Field\Annotation\FieldType',
    );

    $this->alterInfo('field_info');
    $this->setCacheBackend($cache_backend, 'field_types_plugins');
    $this->typedDataManager = $typed_data_manager;
+9 −9
Original line number Diff line number Diff line
@@ -2,26 +2,26 @@

namespace Drupal\Core\Field\Plugin\Field\FieldType;

use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\OptionsProviderInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\OptionsProviderInterface;

/**
 * Defines the 'boolean' entity field type.
 *
 * @FieldType(
 *   id = "boolean",
 *   label = @Translation("Boolean"),
 *   description = @Translation("Field to store a true or false value."),
 *   default_widget = "boolean_checkbox",
 *   default_formatter = "boolean",
 * )
 */
#[FieldType(
  id: "boolean",
  label: new TranslatableMarkup("Boolean"),
  description: new TranslatableMarkup("Field to store a true or false value."),
  default_widget: "boolean_checkbox",
  default_formatter: "boolean",
)]
class BooleanItem extends FieldItemBase implements OptionsProviderInterface {

  /**
+13 −10
Original line number Diff line number Diff line
@@ -2,24 +2,27 @@

namespace Drupal\Core\Field\Plugin\Field\FieldType;

use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\ChangedFieldItemList;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines the 'changed' entity field type.
 *
 * Based on a field of this type, entity types can easily implement the
 * EntityChangedInterface.
 *
 * @FieldType(
 *   id = "changed",
 *   label = @Translation("Last changed"),
 *   description = @Translation("An entity field containing a UNIX timestamp of when the entity has been last updated."),
 *   no_ui = TRUE,
 *   default_widget = "datetime_timestamp",
 *   default_formatter = "timestamp",
 *   list_class = "\Drupal\Core\Field\ChangedFieldItemList"
 * )
 *
 * @see \Drupal\Core\Entity\EntityChangedInterface
 */
#[FieldType(
  id: "changed",
  label: new TranslatableMarkup("Last changed"),
  description: new TranslatableMarkup("An entity field containing a UNIX timestamp of when the entity has been last updated."),
  default_widget: "datetime_timestamp",
  default_formatter: "timestamp",
  no_ui: TRUE,
  list_class: ChangedFieldItemList::class,
)]
class ChangedItem extends CreatedItem {

  /**
+11 −9
Original line number Diff line number Diff line
@@ -2,18 +2,20 @@

namespace Drupal\Core\Field\Plugin\Field\FieldType;

use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines the 'created' entity field type.
 *
 * @FieldType(
 *   id = "created",
 *   label = @Translation("Created"),
 *   description = @Translation("An entity field containing a UNIX timestamp of when the entity has been created."),
 *   no_ui = TRUE,
 *   default_widget = "datetime_timestamp",
 *   default_formatter = "timestamp"
 * )
 */
#[FieldType(
  id: "created",
  label: new TranslatableMarkup("Created"),
  description: new TranslatableMarkup("An entity field containing a UNIX timestamp of when the entity has been created."),
  default_widget: "datetime_timestamp",
  default_formatter: "timestamp",
  no_ui: TRUE,
)]
class CreatedItem extends TimestampItem {

  /**
Loading