Skip to content

Commit 709863c

Browse files
authored
Merge pull request #196 from Chi-teck/issue-156-entity-content
Use PHP attributes for plugins (entity:content)
2 parents 18b3648 + 02a9a7e commit 709863c

File tree

6 files changed

+327
-282
lines changed

6 files changed

+327
-282
lines changed

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

+106-91
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ declare(strict_types=1);
55
namespace Drupal\{{ machine_name }}\Entity;
66
77
{% apply sort_namespaces %}
8-
{% if not revisionable %}
9-
use Drupal\Core\Entity\ContentEntityBase;
10-
{% endif %}
8+
use Drupal\Core\Entity\Attribute\ContentEntityType;
9+
use Drupal\Core\Entity\ContentEntityDeleteForm;
10+
use Drupal\views\EntityViewsData;
11+
use Drupal\Core\Entity\Form\DeleteMultipleForm;
12+
use Drupal\Core\StringTranslation\TranslatableMarkup;
13+
use Drupal\{{ machine_name }}\{{ class }}ListBuilder;
14+
use Drupal\{{ machine_name }}\Form\{{ class }}Form;
1115
{% if author_base_field %}
1216
use Drupal\Core\Entity\EntityStorageInterface;
1317
{% endif %}
1418
{% if has_base_fields %}
1519
use Drupal\Core\Entity\EntityTypeInterface;
1620
use Drupal\Core\Field\BaseFieldDefinition;
21+
{% endif %}
22+
{% if not revisionable %}
23+
use Drupal\Core\Entity\ContentEntityBase;
1724
{% endif %}
1825
{% if revisionable %}
1926
use Drupal\Core\Entity\RevisionableContentEntityBase;
27+
use Drupal\Core\Entity\Form\RevisionDeleteForm;
28+
use Drupal\Core\Entity\Form\RevisionRevertForm;
29+
use Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider;
2030
{% endif %}
2131
use Drupal\{{ machine_name }}\{{ class }}Interface;
2232
{% if author_base_field %}
@@ -25,125 +35,130 @@ use Drupal\user\EntityOwnerTrait;
2535
{% if changed_base_field %}
2636
use Drupal\Core\Entity\EntityChangedTrait;
2737
{% endif %}
38+
{% if canonical %}
39+
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
40+
{% else %}
41+
use Drupal\{{ machine_name }}\Routing\{{ class }}HtmlRouteProvider;
42+
{% endif %}
43+
{% if access_controller %}
44+
use Drupal\{{ machine_name }}\{{ class }}AccessControlHandler;
45+
{% endif %}
2846
{% endapply %}
2947
3048
/**
3149
* Defines the {{ entity_type_label|lower }} entity class.
32-
*
33-
* @ContentEntityType(
34-
* id = "{{ entity_type_id }}",
35-
* label = @Translation("{{ entity_type_label }}"),
36-
* label_collection = @Translation("{{ entity_type_label|pluralize }}"),
37-
* label_singular = @Translation("{{ entity_type_label|lower }}"),
38-
* label_plural = @Translation("{{ entity_type_label|pluralize|lower }}"),
39-
* label_count = @PluralTranslation(
40-
* singular = "@count {{ entity_type_label|pluralize|lower }}",
41-
* plural = "@count {{ entity_type_label|pluralize|lower }}",
42-
* ),
50+
*/
51+
#[ContentEntityType(
52+
id: '{{ entity_type_id }}',
53+
label: new TranslatableMarkup('{{ entity_type_label }}'),
54+
label_collection: new TranslatableMarkup('{{ entity_type_label|pluralize }}'),
55+
label_singular: new TranslatableMarkup('{{ entity_type_label|lower }}'),
56+
label_plural: new TranslatableMarkup('{{ entity_type_label|pluralize|lower }}'),
57+
entity_keys: [
58+
'id' => 'id',
59+
{% if revisionable %}
60+
'revision' => 'revision_id',
61+
{% endif %}
62+
{% if translatable %}
63+
'langcode' => 'langcode',
64+
{% endif %}
4365
{% if bundle %}
44-
* bundle_label = @Translation("{{ entity_type_label }} type"),
66+
'bundle' => 'bundle',
4567
{% endif %}
46-
* handlers = {
47-
* "list_builder" = "Drupal\{{ machine_name }}\{{ class }}ListBuilder",
48-
* "views_data" = "Drupal\views\EntityViewsData",
68+
'label' => '{{ label_base_field ? 'label' : 'id' }}',
69+
{% if author_base_field %}
70+
'owner' => 'uid',
71+
{% endif %}
72+
'uuid' => 'uuid',
73+
],
74+
handlers: [
75+
'list_builder' => {{ class }}ListBuilder::class,
76+
'views_data' => EntityViewsData::class,
4977
{% if access_controller %}
50-
* "access" = "Drupal\{{ machine_name }}\{{ class }}AccessControlHandler",
78+
'access' => {{ class }}AccessControlHandler::class,
5179
{% endif %}
52-
* "form" = {
53-
* "add" = "Drupal\{{ machine_name }}\Form\{{ class }}Form",
54-
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class }}Form",
55-
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
56-
* "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm",
80+
'form' => [
81+
'add' => {{ class }}Form::class,
82+
'edit' => {{ class }}Form::class,
83+
'delete' => ContentEntityDeleteForm::class,
84+
'delete-multiple-confirm' => DeleteMultipleForm::class,
5785
{% if revisionable %}
58-
* "revision-delete" = \Drupal\Core\Entity\Form\RevisionDeleteForm::class,
59-
* "revision-revert" = \Drupal\Core\Entity\Form\RevisionRevertForm::class,
86+
'revision-delete' => RevisionDeleteForm::class,
87+
'revision-revert' => RevisionRevertForm::class,
6088
{% endif %}
61-
* },
62-
* "route_provider" = {
89+
],
90+
'route_provider' => [
6391
{% if canonical %}
64-
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
92+
'html' => AdminHtmlRouteProvider::class,
6593
{% else %}
66-
* "html" = "Drupal\{{ machine_name }}\Routing\{{ class }}HtmlRouteProvider",
67-
{% endif %}
68-
{% if revisionable %}
69-
* "revision" = \Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider::class,
70-
{% endif %}
71-
* },
72-
* },
73-
* base_table = "{{ entity_type_id }}",
74-
{% if translatable %}
75-
* data_table = "{{ entity_type_id }}_field_data",
94+
'html' => {{ class }}HtmlRouteProvider::class,
7695
{% endif %}
7796
{% if revisionable %}
78-
* revision_table = "{{ entity_type_id }}_revision",
79-
{% endif %}
80-
{% if revisionable and translatable %}
81-
* revision_data_table = "{{ entity_type_id }}_field_revision",
97+
'revision' => RevisionHtmlRouteProvider::class,
8298
{% endif %}
83-
{% if revisionable %}
84-
* show_revision_ui = TRUE,
99+
],
100+
],
101+
links: [
102+
'collection' => '/admin/content/{{ entity_type_id_short|u2h }}',
103+
{% if bundle %}
104+
'add-form' => '{{ entity_base_path }}/add/{{ '{' }}{{ entity_type_id }}{{ '_type}' }}',
105+
'add-page' => '{{ entity_base_path }}/add',
106+
{% else %}
107+
'add-form' => '{{ entity_base_path }}/add',
85108
{% endif %}
86-
{% if translatable %}
87-
* translatable = TRUE,
109+
'canonical' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}',
110+
{% if canonical %}
111+
'edit-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/edit',
112+
{% else %}
113+
'edit-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}',
88114
{% endif %}
89-
* admin_permission = "{{ permissions.administer }}",
90-
* entity_keys = {
91-
* "id" = "id",
115+
'delete-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete',
116+
'delete-multiple-form' => '/admin/content/{{ entity_type_id_short|u2h }}/delete-multiple',
92117
{% if revisionable %}
93-
* "revision" = "revision_id",
94-
{% endif %}
95-
{% if translatable %}
96-
* "langcode" = "langcode",
118+
'revision' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/view',
119+
'revision-delete-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/delete',
120+
'revision-revert-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/revert',
121+
'version-history' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revisions',
97122
{% endif %}
123+
],
124+
admin_permission: '{{ permissions.administer }}',
98125
{% if bundle %}
99-
* "bundle" = "bundle",
126+
bundle_entity_type: '{{ entity_type_id }}_type',
127+
bundle_label: new TranslatableMarkup('{{ entity_type_label }} type'),
100128
{% endif %}
101-
* "label" = "{{ label_base_field ? 'label' : 'id' }}",
102-
{% if author_base_field %}
103-
* "uuid" = "uuid",
104-
* "owner" = "uid",
105-
{% else %}
106-
* "uuid" = "uuid",
129+
base_table: '{{ entity_type_id }}',
130+
{% if translatable %}
131+
data_table: '{{ entity_type_id }}_field_data',
107132
{% endif %}
108-
* },
109133
{% if revisionable %}
110-
* revision_metadata_keys = {
111-
* "revision_user" = "revision_uid",
112-
* "revision_created" = "revision_timestamp",
113-
* "revision_log_message" = "revision_log",
114-
* },
134+
revision_table: '{{ entity_type_id }}_revision',
115135
{% endif %}
116-
* links = {
117-
* "collection" = "/admin/content/{{ entity_type_id_short|u2h }}",
118-
{% if bundle %}
119-
* "add-form" = "{{ entity_base_path }}/add/{{ '{' }}{{ entity_type_id }}{{ '_type}' }}",
120-
* "add-page" = "{{ entity_base_path }}/add",
121-
{% else %}
122-
* "add-form" = "{{ entity_base_path }}/add",
136+
{% if revisionable and translatable %}
137+
revision_data_table: '{{ entity_type_id }}_field_revision',
123138
{% endif %}
124-
* "canonical" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
125-
{% if canonical %}
126-
* "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/edit",
127-
{% else %}
128-
* "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
139+
{% if translatable %}
140+
translatable: TRUE,
129141
{% endif %}
130-
* "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
131-
* "delete-multiple-form" = "/admin/content/{{ entity_type_id_short|u2h }}/delete-multiple",
132142
{% if revisionable %}
133-
* "revision" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/view",
134-
* "revision-delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/delete",
135-
* "revision-revert-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/revert",
136-
* "version-history" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revisions",
143+
show_revision_ui: TRUE,
137144
{% endif %}
138-
* },
145+
label_count: [
146+
'singular' => '@count {{ entity_type_label|pluralize|lower }}',
147+
'plural' => '@count {{ entity_type_label|pluralize|lower }}',
148+
],
139149
{% if bundle %}
140-
* bundle_entity_type = "{{ entity_type_id }}_type",
141-
* field_ui_base_route = "entity.{{ entity_type_id }}_type.edit_form",
150+
field_ui_base_route: 'entity.{{ entity_type_id }}_type.edit_form',
142151
{% elseif fieldable %}
143-
* field_ui_base_route = "entity.{{ entity_type_id }}.settings",
152+
field_ui_base_route: 'entity.{{ entity_type_id }}.settings',
144153
{% endif %}
145-
* )
146-
*/
154+
{% if revisionable %}
155+
revision_metadata_keys: [
156+
'revision_user' => 'revision_uid',
157+
'revision_created' => 'revision_timestamp',
158+
'revision_log_message' => 'revision_log',
159+
],
160+
{% endif %}
161+
)]
147162
final class {{ class }} extends {% if revisionable %}Revisionable{% endif %}ContentEntityBase implements {{ class }}Interface {
148163
149164
{% if changed_base_field or author_base_field %}

templates/Entity/_content-entity/src/Entity/ExampleType.php.twig

+49-42
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,60 @@ declare(strict_types=1);
44
55
namespace Drupal\{{ machine_name }}\Entity;
66
7+
{% apply sort_namespaces %}
78
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
9+
use Drupal\Core\Entity\Attribute\ConfigEntityType;
10+
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
11+
use Drupal\Core\Entity\EntityDeleteForm;
12+
use Drupal\Core\StringTranslation\TranslatableMarkup;
13+
use Drupal\{{ machine_name }}\Form\{{ class }}TypeForm;
14+
use Drupal\{{ machine_name }}\{{ class }}TypeListBuilder;
15+
{% endapply %}
816
917
/**
1018
* Defines the {{ entity_type_label }} type configuration entity.
11-
*
12-
* @ConfigEntityType(
13-
* id = "{{ entity_type_id }}_type",
14-
* label = @Translation("{{ entity_type_label }} type"),
15-
* label_collection = @Translation("{{ entity_type_label }} types"),
16-
* label_singular = @Translation("{{ entity_type_label|lower }} type"),
17-
* label_plural = @Translation("{{ entity_type_label|pluralize|lower }} types"),
18-
* label_count = @PluralTranslation(
19-
* singular = "@count {{ entity_type_label|pluralize|lower }} type",
20-
* plural = "@count {{ entity_type_label|pluralize|lower }} types",
21-
* ),
22-
* handlers = {
23-
* "form" = {
24-
* "add" = "Drupal\{{ machine_name }}\Form\{{ class }}TypeForm",
25-
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class }}TypeForm",
26-
* "delete" = "Drupal\Core\Entity\EntityDeleteForm",
27-
* },
28-
* "list_builder" = "Drupal\{{ machine_name }}\{{ class }}TypeListBuilder",
29-
* "route_provider" = {
30-
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
31-
* },
32-
* },
33-
* admin_permission = "{{ permissions.administer }}",
34-
* bundle_of = "{{ entity_type_id }}",
35-
* config_prefix = "{{ entity_type_id }}_type",
36-
* entity_keys = {
37-
* "id" = "id",
38-
* "label" = "label",
39-
* "uuid" = "uuid",
40-
* },
41-
* links = {
42-
* "add-form" = "/admin/structure/{{ entity_type_id }}_types/add",
43-
* "edit-form" = "/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}",
44-
* "delete-form" = "/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}/delete",
45-
* "collection" = "/admin/structure/{{ entity_type_id }}_types",
46-
* },
47-
* config_export = {
48-
* "id",
49-
* "label",
50-
* "uuid",
51-
* },
52-
* )
5319
*/
20+
#[ConfigEntityType(
21+
id: '{{ entity_type_id }}_type',
22+
label: new TranslatableMarkup('{{ entity_type_label }} type'),
23+
label_collection: new TranslatableMarkup('{{ entity_type_label }} types'),
24+
label_singular: new TranslatableMarkup('{{ entity_type_label|lower }} type'),
25+
label_plural: new TranslatableMarkup('{{ entity_type_label|pluralize|lower }} types'),
26+
config_prefix: '{{ entity_type_id }}_type',
27+
entity_keys: [
28+
'id' => 'id',
29+
'label' => 'label',
30+
'uuid' => 'uuid',
31+
],
32+
handlers: [
33+
'list_builder' => {{ class }}TypeListBuilder::class,
34+
'route_provider' => [
35+
'html' => AdminHtmlRouteProvider::class,
36+
],
37+
'form' => [
38+
'add' => {{ class }}TypeForm::class,
39+
'edit' => {{ class }}TypeForm::class,
40+
'delete' => EntityDeleteForm::class,
41+
],
42+
],
43+
links: [
44+
'add-form' => '/admin/structure/{{ entity_type_id }}_types/add',
45+
'edit-form' => '/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}',
46+
'delete-form' => '/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}/delete',
47+
'collection' => '/admin/structure/{{ entity_type_id }}_types',
48+
],
49+
admin_permission: '{{ permissions.administer }}',
50+
bundle_of: '{{ entity_type_id }}',
51+
label_count: [
52+
'singular' => '@count {{ entity_type_label|lower }} type',
53+
'plural' => '@count {{ entity_type_label|pluralize|lower }} types',
54+
],
55+
config_export: [
56+
'id',
57+
'label',
58+
'uuid',
59+
],
60+
)]
5461
final class {{ class }}Type extends ConfigEntityBundleBase {
5562
5663
/**

0 commit comments

Comments
 (0)