Verified Commit dcecab3d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420980 by mstrelan, smustgrave: Convert FieldFormatter plugin discovery to attributes

parent 60d9d677
Loading
Loading
Loading
Loading
Loading
+54 −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 FieldFormatter attribute for plugin discovery.
 *
 * Formatters handle the display of field values. They are typically
 * instantiated and invoked by an EntityDisplay object.
 *
 * Additional attribute keys for formatters can be defined in
 * hook_field_formatter_info_alter().
 *
 * @see \Drupal\Core\Field\FormatterPluginManager
 * @see \Drupal\Core\Field\FormatterInterface
 *
 * @ingroup field_formatter
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class FieldFormatter extends Plugin {

  /**
   * Constructs a FieldFormatter attribute.
   *
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $label
   *   (optional) The human-readable name of the formatter type.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
   *   (optional) A short description of the formatter type.
   * @param string[] $field_types
   *   (optional) An array of field types the formatter supports.
   * @param int|null $weight
   *   (optional) An integer to determine the weight of this formatter.
   *   Weight is relative to other formatters in the Field UI when selecting a
   *   formatter for a given field instance.
   * @param class-string|null $deriver
   *   (optional) The deriver class.
   */
  public function __construct(
    public readonly string $id,
    public readonly ?TranslatableMarkup $label = NULL,
    public readonly ?TranslatableMarkup $description = NULL,
    public readonly array $field_types = [],
    public readonly ?int $weight = NULL,
    public readonly ?string $deriver = NULL,
  ) {}

}
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Plugin\DefaultPluginManager;

/**
@@ -42,7 +43,7 @@ class FormatterPluginManager extends DefaultPluginManager {
   *   The 'field type' plugin manager.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager) {
    parent::__construct('Plugin/Field/FieldFormatter', $namespaces, $module_handler, 'Drupal\Core\Field\FormatterInterface', 'Drupal\Core\Field\Annotation\FieldFormatter');
    parent::__construct('Plugin/Field/FieldFormatter', $namespaces, $module_handler, 'Drupal\Core\Field\FormatterInterface', FieldFormatter::class, 'Drupal\Core\Field\Annotation\FieldFormatter');

    $this->setCacheBackend($cache_backend, 'field_formatter_types_plugins');
    $this->alterInfo('field_formatter_info');
+10 −9
Original line number Diff line number Diff line
@@ -2,21 +2,22 @@

namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'basic_string' formatter.
 *
 * @FieldFormatter(
 *   id = "basic_string",
 *   label = @Translation("Plain text"),
 *   field_types = {
 *     "string_long",
 *     "email"
 *   }
 * )
 */
#[FieldFormatter(
  id: 'basic_string',
  label: new TranslatableMarkup('Plain text'),
  field_types: [
    'string_long',
    'email',
  ],
)]
class BasicStringFormatter extends FormatterBase {

  /**
+9 −8
Original line number Diff line number Diff line
@@ -2,21 +2,22 @@

namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'boolean' formatter.
 *
 * @FieldFormatter(
 *   id = "boolean",
 *   label = @Translation("Boolean"),
 *   field_types = {
 *     "boolean",
 *   }
 * )
 */
#[FieldFormatter(
  id: 'boolean',
  label: new TranslatableMarkup('Boolean'),
  field_types: [
    'boolean',
  ],
)]
class BooleanFormatter extends FormatterBase {

  /**
+10 −9
Original line number Diff line number Diff line
@@ -2,7 +2,9 @@

namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'number_decimal' formatter.
@@ -10,16 +12,15 @@
 * The 'Default' formatter is different for integer fields on the one hand, and
 * for decimal and float fields on the other hand, in order to be able to use
 * different settings.
 *
 * @FieldFormatter(
 *   id = "number_decimal",
 *   label = @Translation("Default"),
 *   field_types = {
 *     "decimal",
 *     "float"
 *   }
 * )
 */
#[FieldFormatter(
  id: 'number_decimal',
  label: new TranslatableMarkup('Default'),
  field_types: [
    'decimal',
    'float',
  ],
)]
class DecimalFormatter extends NumericFormatterBase {

  /**
Loading