Skip to content

Commit

Permalink
Merge pull request #166 from Chi-teck/php-attribute-field-formatter
Browse files Browse the repository at this point in the history
Use PHP attribute for FieldFormatter plugin
  • Loading branch information
Chi-teck committed May 24, 2024
2 parents 9efe42a + 6aa6d76 commit a4b5581
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
19 changes: 11 additions & 8 deletions templates/Plugin/Field/_formatter/formatter.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\Field\FieldFormatter;

{% apply sort_namespaces %}
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
{% if configurable %}
use Drupal\Core\StringTranslation\TranslatableMarkup;
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
{% endif %}
{% endapply %}

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

{% if configurable %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

namespace Drupal\foo\Plugin\Field\FieldFormatter;

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

/**
* Plugin implementation of the 'Zoo' formatter.
*
* @FieldFormatter(
* id = "foo_zoo",
* label = @Translation("Zoo"),
* field_types = {"string"},
* )
*/
#[FieldFormatter(
id: 'foo_zoo',
label: new TranslatableMarkup('Zoo'),
field_types: ['string'],
)]
final class ZooFormatter extends FormatterBase {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

namespace Drupal\foo\Plugin\Field\FieldFormatter;

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

/**
* Plugin implementation of the 'Zoo' formatter.
*
* @FieldFormatter(
* id = "foo_zoo",
* label = @Translation("Zoo"),
* field_types = {"string"},
* )
*/
#[FieldFormatter(
id: 'foo_zoo',
label: new TranslatableMarkup('Zoo'),
field_types: ['string'],
)]
final class ZooFormatter extends FormatterBase {

/**
Expand Down

0 comments on commit a4b5581

Please sign in to comment.