Skip to content

Commit

Permalink
Merge pull request #197 from Chi-teck/issue-156-entity-configuration
Browse files Browse the repository at this point in the history
Use PHP attributes for plugins (entity:configuration)
  • Loading branch information
Chi-teck authored Dec 30, 2024
2 parents cec4e2d + 96c27d6 commit 18b3648
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 76 deletions.
80 changes: 42 additions & 38 deletions templates/Entity/_configuration-entity/src/Entity/Example.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,54 @@ namespace Drupal\{{ machine_name }}\Entity;

{% apply sort_namespaces %}
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\Attribute\ConfigEntityType;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\{{ machine_name }}\{{ class_prefix }}Interface;
use Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder;
use Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form;
{% endapply %}

/**
* Defines the {{ entity_type_label|lower }} entity type.
*
* @ConfigEntityType(
* 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|lower }}",
* plural = "@count {{ entity_type_label|pluralize|lower }}",
* ),
* handlers = {
* "list_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder",
* "form" = {
* "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
* "delete" = "Drupal\Core\Entity\EntityDeleteForm",
* },
* },
* config_prefix = "{{ entity_type_id }}",
* admin_permission = "administer {{ entity_type_id }}",
* links = {
* "collection" = "/admin/structure/{{ entity_type_id|u2h }}",
* "add-form" = "/admin/structure/{{ entity_type_id|u2h }}/add",
* "edit-form" = "/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
* "delete-form" = "/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
* },
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid",
* },
* config_export = {
* "id",
* "label",
* "description",
* },
* )
*/
#[ConfigEntityType(
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 }}'),
config_prefix: '{{ entity_type_id }}',
entity_keys: [
'id' => 'id',
'label' => 'label',
'uuid' => 'uuid',
],
handlers: [
'list_builder' => {{ class_prefix }}ListBuilder::class,
'form' => [
'add' => {{ class_prefix }}Form::class,
'edit' => {{ class_prefix }}Form::class,
'delete' => EntityDeleteForm::class,
],
],
links: [
'collection' => '/admin/structure/{{ entity_type_id|u2h }}',
'add-form' => '/admin/structure/{{ entity_type_id|u2h }}/add',
'edit-form' => '/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}',
'delete-form' => '/admin/structure/{{ entity_type_id|u2h }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete',
],
admin_permission: 'administer {{ entity_type_id }}',
label_count: [
'singular' => '@count {{ entity_type_label|lower }}',
'plural' => '@count {{ entity_type_label|pluralize|lower }}',
],
config_export: [
'id',
'label',
'description',
],
)]
final class {{ class_prefix }} extends ConfigEntityBase implements {{ class_prefix }}Interface {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,53 @@
namespace Drupal\foo\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\Attribute\ConfigEntityType;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\foo\ExampleInterface;
use Drupal\foo\ExampleListBuilder;
use Drupal\foo\Form\ExampleForm;

/**
* Defines the example entity type.
*
* @ConfigEntityType(
* id = "example",
* label = @Translation("Example"),
* label_collection = @Translation("Examples"),
* label_singular = @Translation("example"),
* label_plural = @Translation("examples"),
* label_count = @PluralTranslation(
* singular = "@count example",
* plural = "@count examples",
* ),
* handlers = {
* "list_builder" = "Drupal\foo\ExampleListBuilder",
* "form" = {
* "add" = "Drupal\foo\Form\ExampleForm",
* "edit" = "Drupal\foo\Form\ExampleForm",
* "delete" = "Drupal\Core\Entity\EntityDeleteForm",
* },
* },
* config_prefix = "example",
* admin_permission = "administer example",
* links = {
* "collection" = "/admin/structure/example",
* "add-form" = "/admin/structure/example/add",
* "edit-form" = "/admin/structure/example/{example}",
* "delete-form" = "/admin/structure/example/{example}/delete",
* },
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid",
* },
* config_export = {
* "id",
* "label",
* "description",
* },
* )
*/
#[ConfigEntityType(
id: 'example',
label: new TranslatableMarkup('Example'),
label_collection: new TranslatableMarkup('Examples'),
label_singular: new TranslatableMarkup('example'),
label_plural: new TranslatableMarkup('examples'),
config_prefix: 'example',
entity_keys: [
'id' => 'id',
'label' => 'label',
'uuid' => 'uuid',
],
handlers: [
'list_builder' => ExampleListBuilder::class,
'form' => [
'add' => ExampleForm::class,
'edit' => ExampleForm::class,
'delete' => EntityDeleteForm::class,
],
],
links: [
'collection' => '/admin/structure/example',
'add-form' => '/admin/structure/example/add',
'edit-form' => '/admin/structure/example/{example}',
'delete-form' => '/admin/structure/example/{example}/delete',
],
admin_permission: 'administer example',
label_count: [
'singular' => '@count example',
'plural' => '@count examples',
],
config_export: [
'id',
'label',
'description',
],
)]
final class Example extends ConfigEntityBase implements ExampleInterface {

/**
Expand Down

0 comments on commit 18b3648

Please sign in to comment.