Skip to content

Commit e2ddb22

Browse files
authored
Use PHP attribute for FieldType plugin. (#178)
1 parent ec70391 commit e2ddb22

File tree

3 files changed

+31
-26
lines changed
  • templates/Plugin/Field/_type
  • tests/functional/Generator/Plugin/Field/_type

3 files changed

+31
-26
lines changed

templates/Plugin/Field/_type/type.twig

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,29 @@ declare(strict_types=1);
44
55
namespace Drupal\{{ machine_name }}\Plugin\Field\FieldType;
66
7+
{% apply sort_namespaces %}
78
use Drupal\Component\Utility\Random;
9+
use Drupal\Core\Field\Attribute\FieldType;
810
use Drupal\Core\Field\FieldDefinitionInterface;
911
use Drupal\Core\Field\FieldItemBase;
1012
use Drupal\Core\Field\FieldStorageDefinitionInterface;
11-
{% if configurable_storage or configurable_instance %}
13+
{% if configurable_storage or configurable_instance %}
1214
use Drupal\Core\Form\FormStateInterface;
13-
{% endif %}
15+
{% endif %}
16+
use Drupal\Core\StringTranslation\TranslatableMarkup;
1417
use Drupal\Core\TypedData\DataDefinition;
18+
{% endapply %}
1519
1620
/**
1721
* Defines the '{{ plugin_id }}' field type.
18-
*
19-
* @FieldType(
20-
* id = "{{ plugin_id }}",
21-
* label = @Translation("{{ plugin_label }}"),
22-
* description = @Translation("Some description."),
23-
* default_widget = "string_textfield",
24-
* default_formatter = "string",
25-
* )
2622
*/
23+
#[FieldType(
24+
id: '{{ plugin_id }}',
25+
label: new TranslatableMarkup('{{ plugin_label }}'),
26+
description: new TranslatableMarkup('Some description'),
27+
default_widget: 'string_textfield',
28+
default_formatter: 'string',
29+
)]
2730
final class {{ class }} extends FieldItemBase {
2831
2932
{% if configurable_storage %}

tests/functional/Generator/Plugin/Field/_type/_n_config/src/Plugin/Field/FieldType/FooItem.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
namespace Drupal\example\Plugin\Field\FieldType;
66

77
use Drupal\Component\Utility\Random;
8+
use Drupal\Core\Field\Attribute\FieldType;
89
use Drupal\Core\Field\FieldDefinitionInterface;
910
use Drupal\Core\Field\FieldItemBase;
1011
use Drupal\Core\Field\FieldStorageDefinitionInterface;
12+
use Drupal\Core\StringTranslation\TranslatableMarkup;
1113
use Drupal\Core\TypedData\DataDefinition;
1214

1315
/**
1416
* Defines the 'foo' field type.
15-
*
16-
* @FieldType(
17-
* id = "foo",
18-
* label = @Translation("Foo"),
19-
* description = @Translation("Some description."),
20-
* default_widget = "string_textfield",
21-
* default_formatter = "string",
22-
* )
2317
*/
18+
#[FieldType(
19+
id: 'foo',
20+
label: new TranslatableMarkup('Foo'),
21+
description: new TranslatableMarkup('Some description'),
22+
default_widget: 'string_textfield',
23+
default_formatter: 'string',
24+
)]
2425
final class FooItem extends FieldItemBase {
2526

2627
/**

tests/functional/Generator/Plugin/Field/_type/_w_config/src/Plugin/Field/FieldType/FooItem.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@
55
namespace Drupal\example\Plugin\Field\FieldType;
66

77
use Drupal\Component\Utility\Random;
8+
use Drupal\Core\Field\Attribute\FieldType;
89
use Drupal\Core\Field\FieldDefinitionInterface;
910
use Drupal\Core\Field\FieldItemBase;
1011
use Drupal\Core\Field\FieldStorageDefinitionInterface;
1112
use Drupal\Core\Form\FormStateInterface;
13+
use Drupal\Core\StringTranslation\TranslatableMarkup;
1214
use Drupal\Core\TypedData\DataDefinition;
1315

1416
/**
1517
* Defines the 'foo' field type.
16-
*
17-
* @FieldType(
18-
* id = "foo",
19-
* label = @Translation("Foo"),
20-
* description = @Translation("Some description."),
21-
* default_widget = "string_textfield",
22-
* default_formatter = "string",
23-
* )
2418
*/
19+
#[FieldType(
20+
id: 'foo',
21+
label: new TranslatableMarkup('Foo'),
22+
description: new TranslatableMarkup('Some description'),
23+
default_widget: 'string_textfield',
24+
default_formatter: 'string',
25+
)]
2526
final class FooItem extends FieldItemBase {
2627

2728
/**

0 commit comments

Comments
 (0)