Skip to content

Commit 18b3648

Browse files
authored
Merge pull request #197 from Chi-teck/issue-156-entity-configuration
Use PHP attributes for plugins (entity:configuration)
2 parents cec4e2d + 96c27d6 commit 18b3648

File tree

2 files changed

+84
-76
lines changed
  • templates/Entity/_configuration-entity/src/Entity
  • tests/functional/Generator/Entity/_configuration_entity/src/Entity

2 files changed

+84
-76
lines changed

templates/Entity/_configuration-entity/src/Entity/Example.php.twig

+42-38
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,54 @@ namespace Drupal\{{ machine_name }}\Entity;
66
77
{% apply sort_namespaces %}
88
use Drupal\Core\Config\Entity\ConfigEntityBase;
9+
use Drupal\Core\Entity\Attribute\ConfigEntityType;
10+
use Drupal\Core\Entity\EntityDeleteForm;
11+
use Drupal\Core\StringTranslation\TranslatableMarkup;
912
use Drupal\{{ machine_name }}\{{ class_prefix }}Interface;
13+
use Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder;
14+
use Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form;
1015
{% endapply %}
1116
1217
/**
1318
* Defines the {{ entity_type_label|lower }} entity type.
14-
*
15-
* @ConfigEntityType(
16-
* id = "{{ entity_type_id }}",
17-
* label = @Translation("{{ entity_type_label }}"),
18-
* label_collection = @Translation("{{ entity_type_label|pluralize }}"),
19-
* label_singular = @Translation("{{ entity_type_label|lower }}"),
20-
* label_plural = @Translation("{{ entity_type_label|pluralize|lower }}"),
21-
* label_count = @PluralTranslation(
22-
* singular = "@count {{ entity_type_label|lower }}",
23-
* plural = "@count {{ entity_type_label|pluralize|lower }}",
24-
* ),
25-
* handlers = {
26-
* "list_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder",
27-
* "form" = {
28-
* "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
29-
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
30-
* "delete" = "Drupal\Core\Entity\EntityDeleteForm",
31-
* },
32-
* },
33-
* config_prefix = "{{ entity_type_id }}",
34-
* admin_permission = "administer {{ entity_type_id }}",
35-
* links = {
36-
* "collection" = "/admin/structure/{{ entity_type_id|u2h }}",
37-
* "add-form" = "/admin/structure/{{ entity_type_id|u2h }}/add",
38-
* "edit-form" = "/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
39-
* "delete-form" = "/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
40-
* },
41-
* entity_keys = {
42-
* "id" = "id",
43-
* "label" = "label",
44-
* "uuid" = "uuid",
45-
* },
46-
* config_export = {
47-
* "id",
48-
* "label",
49-
* "description",
50-
* },
51-
* )
5219
*/
20+
#[ConfigEntityType(
21+
id: '{{ entity_type_id }}',
22+
label: new TranslatableMarkup('{{ entity_type_label }}'),
23+
label_collection: new TranslatableMarkup('{{ entity_type_label|pluralize }}'),
24+
label_singular: new TranslatableMarkup('{{ entity_type_label|lower }}'),
25+
label_plural: new TranslatableMarkup('{{ entity_type_label|pluralize|lower }}'),
26+
config_prefix: '{{ entity_type_id }}',
27+
entity_keys: [
28+
'id' => 'id',
29+
'label' => 'label',
30+
'uuid' => 'uuid',
31+
],
32+
handlers: [
33+
'list_builder' => {{ class_prefix }}ListBuilder::class,
34+
'form' => [
35+
'add' => {{ class_prefix }}Form::class,
36+
'edit' => {{ class_prefix }}Form::class,
37+
'delete' => EntityDeleteForm::class,
38+
],
39+
],
40+
links: [
41+
'collection' => '/admin/structure/{{ entity_type_id|u2h }}',
42+
'add-form' => '/admin/structure/{{ entity_type_id|u2h }}/add',
43+
'edit-form' => '/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}',
44+
'delete-form' => '/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete',
45+
],
46+
admin_permission: 'administer {{ entity_type_id }}',
47+
label_count: [
48+
'singular' => '@count {{ entity_type_label|lower }}',
49+
'plural' => '@count {{ entity_type_label|pluralize|lower }}',
50+
],
51+
config_export: [
52+
'id',
53+
'label',
54+
'description',
55+
],
56+
)]
5357
final class {{ class_prefix }} extends ConfigEntityBase implements {{ class_prefix }}Interface {
5458
5559
/**

tests/functional/Generator/Entity/_configuration_entity/src/Entity/Example.php

+42-38
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,53 @@
55
namespace Drupal\foo\Entity;
66

77
use Drupal\Core\Config\Entity\ConfigEntityBase;
8+
use Drupal\Core\Entity\Attribute\ConfigEntityType;
9+
use Drupal\Core\Entity\EntityDeleteForm;
10+
use Drupal\Core\StringTranslation\TranslatableMarkup;
811
use Drupal\foo\ExampleInterface;
12+
use Drupal\foo\ExampleListBuilder;
13+
use Drupal\foo\Form\ExampleForm;
914

1015
/**
1116
* Defines the example entity type.
12-
*
13-
* @ConfigEntityType(
14-
* id = "example",
15-
* label = @Translation("Example"),
16-
* label_collection = @Translation("Examples"),
17-
* label_singular = @Translation("example"),
18-
* label_plural = @Translation("examples"),
19-
* label_count = @PluralTranslation(
20-
* singular = "@count example",
21-
* plural = "@count examples",
22-
* ),
23-
* handlers = {
24-
* "list_builder" = "Drupal\foo\ExampleListBuilder",
25-
* "form" = {
26-
* "add" = "Drupal\foo\Form\ExampleForm",
27-
* "edit" = "Drupal\foo\Form\ExampleForm",
28-
* "delete" = "Drupal\Core\Entity\EntityDeleteForm",
29-
* },
30-
* },
31-
* config_prefix = "example",
32-
* admin_permission = "administer example",
33-
* links = {
34-
* "collection" = "/admin/structure/example",
35-
* "add-form" = "/admin/structure/example/add",
36-
* "edit-form" = "/admin/structure/example/{example}",
37-
* "delete-form" = "/admin/structure/example/{example}/delete",
38-
* },
39-
* entity_keys = {
40-
* "id" = "id",
41-
* "label" = "label",
42-
* "uuid" = "uuid",
43-
* },
44-
* config_export = {
45-
* "id",
46-
* "label",
47-
* "description",
48-
* },
49-
* )
5017
*/
18+
#[ConfigEntityType(
19+
id: 'example',
20+
label: new TranslatableMarkup('Example'),
21+
label_collection: new TranslatableMarkup('Examples'),
22+
label_singular: new TranslatableMarkup('example'),
23+
label_plural: new TranslatableMarkup('examples'),
24+
config_prefix: 'example',
25+
entity_keys: [
26+
'id' => 'id',
27+
'label' => 'label',
28+
'uuid' => 'uuid',
29+
],
30+
handlers: [
31+
'list_builder' => ExampleListBuilder::class,
32+
'form' => [
33+
'add' => ExampleForm::class,
34+
'edit' => ExampleForm::class,
35+
'delete' => EntityDeleteForm::class,
36+
],
37+
],
38+
links: [
39+
'collection' => '/admin/structure/example',
40+
'add-form' => '/admin/structure/example/add',
41+
'edit-form' => '/admin/structure/example/{example}',
42+
'delete-form' => '/admin/structure/example/{example}/delete',
43+
],
44+
admin_permission: 'administer example',
45+
label_count: [
46+
'singular' => '@count example',
47+
'plural' => '@count examples',
48+
],
49+
config_export: [
50+
'id',
51+
'label',
52+
'description',
53+
],
54+
)]
5155
final class Example extends ConfigEntityBase implements ExampleInterface {
5256

5357
/**

0 commit comments

Comments
 (0)