Skip to content

Commit

Permalink
Merge pull request #196 from Chi-teck/issue-156-entity-content
Browse files Browse the repository at this point in the history
Use PHP attributes for plugins (entity:content)
  • Loading branch information
Chi-teck authored Dec 30, 2024
2 parents 18b3648 + 02a9a7e commit 709863c
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 282 deletions.
197 changes: 106 additions & 91 deletions templates/Entity/_content-entity/src/Entity/Example.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Entity;

{% apply sort_namespaces %}
{% if not revisionable %}
use Drupal\Core\Entity\ContentEntityBase;
{% endif %}
use Drupal\Core\Entity\Attribute\ContentEntityType;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\views\EntityViewsData;
use Drupal\Core\Entity\Form\DeleteMultipleForm;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\{{ machine_name }}\{{ class }}ListBuilder;
use Drupal\{{ machine_name }}\Form\{{ class }}Form;
{% if author_base_field %}
use Drupal\Core\Entity\EntityStorageInterface;
{% endif %}
{% if has_base_fields %}
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
{% endif %}
{% if not revisionable %}
use Drupal\Core\Entity\ContentEntityBase;
{% endif %}
{% if revisionable %}
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Entity\Form\RevisionDeleteForm;
use Drupal\Core\Entity\Form\RevisionRevertForm;
use Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider;
{% endif %}
use Drupal\{{ machine_name }}\{{ class }}Interface;
{% if author_base_field %}
Expand All @@ -25,125 +35,130 @@ use Drupal\user\EntityOwnerTrait;
{% if changed_base_field %}
use Drupal\Core\Entity\EntityChangedTrait;
{% endif %}
{% if canonical %}
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
{% else %}
use Drupal\{{ machine_name }}\Routing\{{ class }}HtmlRouteProvider;
{% endif %}
{% if access_controller %}
use Drupal\{{ machine_name }}\{{ class }}AccessControlHandler;
{% endif %}
{% endapply %}

/**
* Defines the {{ entity_type_label|lower }} entity class.
*
* @ContentEntityType(
* id = "{{ entity_type_id }}",
* label = @Translation("{{ entity_type_label }}"),
* label_collection = @Translation("{{ entity_type_label|pluralize }}"),
* label_singular = @Translation("{{ entity_type_label|lower }}"),
* label_plural = @Translation("{{ entity_type_label|pluralize|lower }}"),
* label_count = @PluralTranslation(
* singular = "@count {{ entity_type_label|pluralize|lower }}",
* plural = "@count {{ entity_type_label|pluralize|lower }}",
* ),
*/
#[ContentEntityType(
id: '{{ entity_type_id }}',
label: new TranslatableMarkup('{{ entity_type_label }}'),
label_collection: new TranslatableMarkup('{{ entity_type_label|pluralize }}'),
label_singular: new TranslatableMarkup('{{ entity_type_label|lower }}'),
label_plural: new TranslatableMarkup('{{ entity_type_label|pluralize|lower }}'),
entity_keys: [
'id' => 'id',
{% if revisionable %}
'revision' => 'revision_id',
{% endif %}
{% if translatable %}
'langcode' => 'langcode',
{% endif %}
{% if bundle %}
* bundle_label = @Translation("{{ entity_type_label }} type"),
'bundle' => 'bundle',
{% endif %}
* handlers = {
* "list_builder" = "Drupal\{{ machine_name }}\{{ class }}ListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
'label' => '{{ label_base_field ? 'label' : 'id' }}',
{% if author_base_field %}
'owner' => 'uid',
{% endif %}
'uuid' => 'uuid',
],
handlers: [
'list_builder' => {{ class }}ListBuilder::class,
'views_data' => EntityViewsData::class,
{% if access_controller %}
* "access" = "Drupal\{{ machine_name }}\{{ class }}AccessControlHandler",
'access' => {{ class }}AccessControlHandler::class,
{% endif %}
* "form" = {
* "add" = "Drupal\{{ machine_name }}\Form\{{ class }}Form",
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class }}Form",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm",
'form' => [
'add' => {{ class }}Form::class,
'edit' => {{ class }}Form::class,
'delete' => ContentEntityDeleteForm::class,
'delete-multiple-confirm' => DeleteMultipleForm::class,
{% if revisionable %}
* "revision-delete" = \Drupal\Core\Entity\Form\RevisionDeleteForm::class,
* "revision-revert" = \Drupal\Core\Entity\Form\RevisionRevertForm::class,
'revision-delete' => RevisionDeleteForm::class,
'revision-revert' => RevisionRevertForm::class,
{% endif %}
* },
* "route_provider" = {
],
'route_provider' => [
{% if canonical %}
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
'html' => AdminHtmlRouteProvider::class,
{% else %}
* "html" = "Drupal\{{ machine_name }}\Routing\{{ class }}HtmlRouteProvider",
{% endif %}
{% if revisionable %}
* "revision" = \Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider::class,
{% endif %}
* },
* },
* base_table = "{{ entity_type_id }}",
{% if translatable %}
* data_table = "{{ entity_type_id }}_field_data",
'html' => {{ class }}HtmlRouteProvider::class,
{% endif %}
{% if revisionable %}
* revision_table = "{{ entity_type_id }}_revision",
{% endif %}
{% if revisionable and translatable %}
* revision_data_table = "{{ entity_type_id }}_field_revision",
'revision' => RevisionHtmlRouteProvider::class,
{% endif %}
{% if revisionable %}
* show_revision_ui = TRUE,
],
],
links: [
'collection' => '/admin/content/{{ entity_type_id_short|u2h }}',
{% if bundle %}
'add-form' => '{{ entity_base_path }}/add/{{ '{' }}{{ entity_type_id }}{{ '_type}' }}',
'add-page' => '{{ entity_base_path }}/add',
{% else %}
'add-form' => '{{ entity_base_path }}/add',
{% endif %}
{% if translatable %}
* translatable = TRUE,
'canonical' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}',
{% if canonical %}
'edit-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/edit',
{% else %}
'edit-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}',
{% endif %}
* admin_permission = "{{ permissions.administer }}",
* entity_keys = {
* "id" = "id",
'delete-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete',
'delete-multiple-form' => '/admin/content/{{ entity_type_id_short|u2h }}/delete-multiple',
{% if revisionable %}
* "revision" = "revision_id",
{% endif %}
{% if translatable %}
* "langcode" = "langcode",
'revision' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/view',
'revision-delete-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/delete',
'revision-revert-form' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/revert',
'version-history' => '{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revisions',
{% endif %}
],
admin_permission: '{{ permissions.administer }}',
{% if bundle %}
* "bundle" = "bundle",
bundle_entity_type: '{{ entity_type_id }}_type',
bundle_label: new TranslatableMarkup('{{ entity_type_label }} type'),
{% endif %}
* "label" = "{{ label_base_field ? 'label' : 'id' }}",
{% if author_base_field %}
* "uuid" = "uuid",
* "owner" = "uid",
{% else %}
* "uuid" = "uuid",
base_table: '{{ entity_type_id }}',
{% if translatable %}
data_table: '{{ entity_type_id }}_field_data',
{% endif %}
* },
{% if revisionable %}
* revision_metadata_keys = {
* "revision_user" = "revision_uid",
* "revision_created" = "revision_timestamp",
* "revision_log_message" = "revision_log",
* },
revision_table: '{{ entity_type_id }}_revision',
{% endif %}
* links = {
* "collection" = "/admin/content/{{ entity_type_id_short|u2h }}",
{% if bundle %}
* "add-form" = "{{ entity_base_path }}/add/{{ '{' }}{{ entity_type_id }}{{ '_type}' }}",
* "add-page" = "{{ entity_base_path }}/add",
{% else %}
* "add-form" = "{{ entity_base_path }}/add",
{% if revisionable and translatable %}
revision_data_table: '{{ entity_type_id }}_field_revision',
{% endif %}
* "canonical" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
{% if canonical %}
* "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/edit",
{% else %}
* "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
{% if translatable %}
translatable: TRUE,
{% endif %}
* "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
* "delete-multiple-form" = "/admin/content/{{ entity_type_id_short|u2h }}/delete-multiple",
{% if revisionable %}
* "revision" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/view",
* "revision-delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/delete",
* "revision-revert-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revision/{{ '{' }}{{ entity_type_id ~ '_revision' }}{{ '}' }}/revert",
* "version-history" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/revisions",
show_revision_ui: TRUE,
{% endif %}
* },
label_count: [
'singular' => '@count {{ entity_type_label|pluralize|lower }}',
'plural' => '@count {{ entity_type_label|pluralize|lower }}',
],
{% if bundle %}
* bundle_entity_type = "{{ entity_type_id }}_type",
* field_ui_base_route = "entity.{{ entity_type_id }}_type.edit_form",
field_ui_base_route: 'entity.{{ entity_type_id }}_type.edit_form',
{% elseif fieldable %}
* field_ui_base_route = "entity.{{ entity_type_id }}.settings",
field_ui_base_route: 'entity.{{ entity_type_id }}.settings',
{% endif %}
* )
*/
{% if revisionable %}
revision_metadata_keys: [
'revision_user' => 'revision_uid',
'revision_created' => 'revision_timestamp',
'revision_log_message' => 'revision_log',
],
{% endif %}
)]
final class {{ class }} extends {% if revisionable %}Revisionable{% endif %}ContentEntityBase implements {{ class }}Interface {

{% if changed_base_field or author_base_field %}
Expand Down
91 changes: 49 additions & 42 deletions templates/Entity/_content-entity/src/Entity/ExampleType.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,60 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Entity;

{% apply sort_namespaces %}
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\Attribute\ConfigEntityType;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\{{ machine_name }}\Form\{{ class }}TypeForm;
use Drupal\{{ machine_name }}\{{ class }}TypeListBuilder;
{% endapply %}

/**
* Defines the {{ entity_type_label }} type configuration entity.
*
* @ConfigEntityType(
* id = "{{ entity_type_id }}_type",
* label = @Translation("{{ entity_type_label }} type"),
* label_collection = @Translation("{{ entity_type_label }} types"),
* label_singular = @Translation("{{ entity_type_label|lower }} type"),
* label_plural = @Translation("{{ entity_type_label|pluralize|lower }} types"),
* label_count = @PluralTranslation(
* singular = "@count {{ entity_type_label|pluralize|lower }} type",
* plural = "@count {{ entity_type_label|pluralize|lower }} types",
* ),
* handlers = {
* "form" = {
* "add" = "Drupal\{{ machine_name }}\Form\{{ class }}TypeForm",
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class }}TypeForm",
* "delete" = "Drupal\Core\Entity\EntityDeleteForm",
* },
* "list_builder" = "Drupal\{{ machine_name }}\{{ class }}TypeListBuilder",
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* },
* },
* admin_permission = "{{ permissions.administer }}",
* bundle_of = "{{ entity_type_id }}",
* config_prefix = "{{ entity_type_id }}_type",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid",
* },
* links = {
* "add-form" = "/admin/structure/{{ entity_type_id }}_types/add",
* "edit-form" = "/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}",
* "delete-form" = "/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}/delete",
* "collection" = "/admin/structure/{{ entity_type_id }}_types",
* },
* config_export = {
* "id",
* "label",
* "uuid",
* },
* )
*/
#[ConfigEntityType(
id: '{{ entity_type_id }}_type',
label: new TranslatableMarkup('{{ entity_type_label }} type'),
label_collection: new TranslatableMarkup('{{ entity_type_label }} types'),
label_singular: new TranslatableMarkup('{{ entity_type_label|lower }} type'),
label_plural: new TranslatableMarkup('{{ entity_type_label|pluralize|lower }} types'),
config_prefix: '{{ entity_type_id }}_type',
entity_keys: [
'id' => 'id',
'label' => 'label',
'uuid' => 'uuid',
],
handlers: [
'list_builder' => {{ class }}TypeListBuilder::class,
'route_provider' => [
'html' => AdminHtmlRouteProvider::class,
],
'form' => [
'add' => {{ class }}TypeForm::class,
'edit' => {{ class }}TypeForm::class,
'delete' => EntityDeleteForm::class,
],
],
links: [
'add-form' => '/admin/structure/{{ entity_type_id }}_types/add',
'edit-form' => '/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}',
'delete-form' => '/admin/structure/{{ entity_type_id }}_types/manage/{{ '{' ~ entity_type_id ~ '_type}' }}/delete',
'collection' => '/admin/structure/{{ entity_type_id }}_types',
],
admin_permission: '{{ permissions.administer }}',
bundle_of: '{{ entity_type_id }}',
label_count: [
'singular' => '@count {{ entity_type_label|lower }} type',
'plural' => '@count {{ entity_type_label|pluralize|lower }} types',
],
config_export: [
'id',
'label',
'uuid',
],
)]
final class {{ class }}Type extends ConfigEntityBundleBase {

/**
Expand Down
Loading

0 comments on commit 709863c

Please sign in to comment.