Skip to content

Commit 9641c75

Browse files
authored
Merge pull request #167 from Chi-teck/php-attribute-field-widget
Use PHP attribute for FieldWidget plugin
2 parents b81e31c + e1bfc1d commit 9641c75

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

templates/Plugin/Field/_widget/widget.twig

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ declare(strict_types=1);
66
namespace Drupal\{{ machine_name }}\Plugin\Field\FieldWidget;
77
88
{% apply sort_namespaces %}
9+
use Drupal\Core\Field\Attribute\FieldWidget;
910
use Drupal\Core\Field\FieldItemListInterface;
1011
use Drupal\Core\Field\WidgetBase;
1112
use Drupal\Core\Form\FormStateInterface;
13+
use Drupal\Core\StringTranslation\TranslatableMarkup;
1214
{% if services %}
1315
{{ di.use(services) }}
1416
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@@ -18,13 +20,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
1820
1921
/**
2022
* Defines the '{{ plugin_id }}' field widget.
21-
*
22-
* @FieldWidget(
23-
* id = "{{ plugin_id }}",
24-
* label = @Translation("{{ plugin_label }}"),
25-
* field_types = {"string"},
26-
* )
2723
*/
24+
#[FieldWidget(
25+
id: '{{ plugin_id }}',
26+
label: new TranslatableMarkup('{{ plugin_label }}'),
27+
field_types: ['string'],
28+
)]
2829
final class {{ class }} extends WidgetBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
2930
3031
{% if services %}

tests/functional/Generator/Plugin/Field/_widget/_n_deps/src/Plugin/Field/FieldWidget/ExampleWidget.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\FieldWidget;
66

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

1113
/**
1214
* Defines the 'foo_example' field widget.
13-
*
14-
* @FieldWidget(
15-
* id = "foo_example",
16-
* label = @Translation("Example"),
17-
* field_types = {"string"},
18-
* )
1915
*/
16+
#[FieldWidget(
17+
id: 'foo_example',
18+
label: new TranslatableMarkup('Example'),
19+
field_types: ['string'],
20+
)]
2021
final class ExampleWidget extends WidgetBase {
2122

2223
/**

tests/functional/Generator/Plugin/Field/_widget/_w_deps/src/Plugin/Field/FieldWidget/ExampleWidget.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66

77
use Drupal\Core\CronInterface;
88
use Drupal\Core\Database\Connection;
9+
use Drupal\Core\Field\Attribute\FieldWidget;
910
use Drupal\Core\Field\FieldItemListInterface;
1011
use Drupal\Core\Field\WidgetBase;
1112
use Drupal\Core\Form\FormStateInterface;
1213
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14+
use Drupal\Core\StringTranslation\TranslatableMarkup;
1315
use Symfony\Component\DependencyInjection\ContainerInterface;
1416

1517
/**
1618
* Defines the 'foo_example' field widget.
17-
*
18-
* @FieldWidget(
19-
* id = "foo_example",
20-
* label = @Translation("Example"),
21-
* field_types = {"string"},
22-
* )
2319
*/
20+
#[FieldWidget(
21+
id: 'foo_example',
22+
label: new TranslatableMarkup('Example'),
23+
field_types: ['string'],
24+
)]
2425
final class ExampleWidget extends WidgetBase implements ContainerFactoryPluginInterface {
2526

2627
/**

0 commit comments

Comments
 (0)