Create a custom attribute class

Last updated on
5 March 2024

New plugin types should use custom attribute class. This allows for documentation, and a consistent developer experience.

Following is an example taken from the core text_trimmed field formatter.

It uses its own attribute class, FieldFormatter, which extends \Drupal\Component\Plugin\Attribute\Plugin.

<?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,
  ) {}

}

You can see that the field_types and weight keys have a default value assigned. In the plugins' annotation, using these keys is optional. The deriver key is required, even if the plugin type will not be using a dervier class.

And this is the relevant sections from the text_trimmed plugin. It show the the use of attribute FieldFormatter class and the id, label and field_types keys.

/**
 * Plugin implementation of the 'text_trimmed' formatter.
 *
 * Note: This class also contains the implementations used by the
 * 'text_summary_or_trimmed' formatter.
 *
 * @see \Drupal\text\Field\Formatter\TextSummaryOrTrimmedFormatter
 */
#[FieldFormatter(
  id: 'text_trimmed',
  label: new TranslatableMarkup('Trimmed'),
  field_types: [
    'text',
    'text_long',
    'text_with_summary',
  ],
)]
class TextTrimmedFormatter extends FormatterBase implements TrustedCallbackInterface {

Help improve this page

Page status: No known problems

You can: