Skip to content

Commit 6aa6d76

Browse files
committed
Use PHP attribute for FieldFormatter plugin.
1 parent 32beb00 commit 6aa6d76

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

templates/Plugin/Field/_formatter/formatter.twig

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ declare(strict_types=1);
44
55
namespace Drupal\{{ machine_name }}\Plugin\Field\FieldFormatter;
66
7+
{% apply sort_namespaces %}
8+
use Drupal\Core\Field\Attribute\FieldFormatter;
79
use Drupal\Core\Field\FieldItemListInterface;
810
use Drupal\Core\Field\FormatterBase;
9-
{% if configurable %}
11+
use Drupal\Core\StringTranslation\TranslatableMarkup;
12+
{% if configurable %}
1013
use Drupal\Core\Form\FormStateInterface;
11-
{% endif %}
14+
{% endif %}
15+
{% endapply %}
1216
1317
/**
1418
* Plugin implementation of the '{{ plugin_label }}' formatter.
15-
*
16-
* @FieldFormatter(
17-
* id = "{{ plugin_id }}",
18-
* label = @Translation("{{ plugin_label }}"),
19-
* field_types = {"string"},
20-
* )
2119
*/
20+
#[FieldFormatter(
21+
id: '{{ plugin_id }}',
22+
label: new TranslatableMarkup('{{ plugin_label }}'),
23+
field_types: ['string'],
24+
)]
2225
final class {{ class }} extends FormatterBase {
2326
2427
{% if configurable %}

tests/functional/Generator/Plugin/Field/_formatter/_n_config/src/Plugin/Field/FieldFormatter/ZooFormatter.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
namespace Drupal\foo\Plugin\Field\FieldFormatter;
66

7+
use Drupal\Core\Field\Attribute\FieldFormatter;
78
use Drupal\Core\Field\FieldItemListInterface;
89
use Drupal\Core\Field\FormatterBase;
10+
use Drupal\Core\StringTranslation\TranslatableMarkup;
911

1012
/**
1113
* Plugin implementation of the 'Zoo' formatter.
12-
*
13-
* @FieldFormatter(
14-
* id = "foo_zoo",
15-
* label = @Translation("Zoo"),
16-
* field_types = {"string"},
17-
* )
1814
*/
15+
#[FieldFormatter(
16+
id: 'foo_zoo',
17+
label: new TranslatableMarkup('Zoo'),
18+
field_types: ['string'],
19+
)]
1920
final class ZooFormatter extends FormatterBase {
2021

2122
/**

tests/functional/Generator/Plugin/Field/_formatter/_w_config/src/Plugin/Field/FieldFormatter/ZooFormatter.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44

55
namespace Drupal\foo\Plugin\Field\FieldFormatter;
66

7+
use Drupal\Core\Field\Attribute\FieldFormatter;
78
use Drupal\Core\Field\FieldItemListInterface;
89
use Drupal\Core\Field\FormatterBase;
910
use Drupal\Core\Form\FormStateInterface;
11+
use Drupal\Core\StringTranslation\TranslatableMarkup;
1012

1113
/**
1214
* Plugin implementation of the 'Zoo' formatter.
13-
*
14-
* @FieldFormatter(
15-
* id = "foo_zoo",
16-
* label = @Translation("Zoo"),
17-
* field_types = {"string"},
18-
* )
1915
*/
16+
#[FieldFormatter(
17+
id: 'foo_zoo',
18+
label: new TranslatableMarkup('Zoo'),
19+
field_types: ['string'],
20+
)]
2021
final class ZooFormatter extends FormatterBase {
2122

2223
/**

0 commit comments

Comments
 (0)