From 2061b8fb285a3892ebe8fc8c2fb059ee706682c9 Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Sun, 10 Mar 2024 18:02:56 +1000 Subject: [PATCH] Issue #3420982 by sorlov, alexpott: Convert EntityReferenceSelection plugin discovery to attributes (cherry picked from commit b4a9a825ea687b3f7315484c9cf432122d7aa672) --- .../Attribute/EntityReferenceSelection.php | 65 +++++++++++++++++++ .../SelectionPluginManager.php | 3 +- .../EntityReferenceSelection/Broken.php | 14 ++-- .../DefaultSelection.php | 18 ++--- .../CommentSelection.php | 17 ++--- .../FileSelection.php | 17 ++--- .../MediaSelection.php | 17 ++--- .../NodeSelection.php | 17 ++--- .../AllExceptHostEntity.php | 14 ++-- .../TermSelection.php | 17 ++--- .../UserSelection.php | 17 ++--- .../ViewsSelection.php | 14 ++-- .../WorkspaceSelection.php | 17 ++--- 13 files changed, 165 insertions(+), 82 deletions(-) create mode 100644 core/lib/Drupal/Core/Entity/Attribute/EntityReferenceSelection.php diff --git a/core/lib/Drupal/Core/Entity/Attribute/EntityReferenceSelection.php b/core/lib/Drupal/Core/Entity/Attribute/EntityReferenceSelection.php new file mode 100644 index 00000000000..3c20cc60d95 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Attribute/EntityReferenceSelection.php @@ -0,0 +1,65 @@ +alterInfo('entity_reference_selection'); $this->setCacheBackend($cache_backend, 'entity_reference_selection_plugins'); - parent::__construct('Plugin/EntityReferenceSelection', $namespaces, $module_handler, 'Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface', 'Drupal\Core\Entity\Annotation\EntityReferenceSelection'); + parent::__construct('Plugin/EntityReferenceSelection', $namespaces, $module_handler, 'Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface', EntityReferenceSelection::class, 'Drupal\Core\Entity\Annotation\EntityReferenceSelection'); } /** diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/Broken.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/Broken.php index aa53c397d30..ff39d50a8ee 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/Broken.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/Broken.php @@ -2,17 +2,23 @@ namespace Drupal\Core\Entity\Plugin\EntityReferenceSelection; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Defines a fallback plugin for missing entity_reference selection plugins. * - * @EntityReferenceSelection( - * id = "broken", - * label = @Translation("Broken/Missing") - * ) + * Note this plugin does not appear in the UI and is only used when a plugin can + * not found. */ +#[EntityReferenceSelection( + id: "broken", + label: new TranslatableMarkup("Broken/Missing"), + group: '', + weight: -100, +)] class Broken extends SelectionPluginBase { /** diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php index 9a8c1012c8d..4fd91204bff 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php @@ -4,6 +4,7 @@ namespace Drupal\Core\Entity\Plugin\EntityReferenceSelection; use Drupal\Component\Utility\Html; use Drupal\Core\Database\Query\AlterableInterface; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase; use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface; @@ -12,11 +13,13 @@ use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriver; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\user\EntityOwnerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -31,15 +34,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface * @see \Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriver * @see plugin_api - * - * @EntityReferenceSelection( - * id = "default", - * label = @Translation("Default"), - * group = "default", - * weight = 0, - * deriver = "Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriver" - * ) */ +#[EntityReferenceSelection( + id: "default", + label: new TranslatableMarkup("Default"), + group: "default", + weight: 0, + deriver: DefaultSelectionDeriver::class, +)] class DefaultSelection extends SelectionPluginBase implements ContainerFactoryPluginInterface, SelectionWithAutocreateInterface { /** diff --git a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php index e82e2ec3bb5..9cbf62a7f59 100644 --- a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php +++ b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php @@ -4,20 +4,21 @@ namespace Drupal\comment\Plugin\EntityReferenceSelection; use Drupal\Component\Utility\Html; use Drupal\Core\Database\Query\SelectInterface; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; use Drupal\comment\CommentInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Provides specific access control for the comment entity type. - * - * @EntityReferenceSelection( - * id = "default:comment", - * label = @Translation("Comment selection"), - * entity_types = {"comment"}, - * group = "default", - * weight = 1 - * ) */ +#[EntityReferenceSelection( + id: "default:comment", + label: new TranslatableMarkup("Comment selection"), + entity_types: ["comment"], + group: "default", + weight: 1 +)] class CommentSelection extends DefaultSelection { /** diff --git a/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php b/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php index 8c8623bc04c..29359dbb9f7 100644 --- a/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php +++ b/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php @@ -2,20 +2,21 @@ namespace Drupal\file\Plugin\EntityReferenceSelection; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\file\FileInterface; /** * Provides specific access control for the file entity type. - * - * @EntityReferenceSelection( - * id = "default:file", - * label = @Translation("File selection"), - * entity_types = {"file"}, - * group = "default", - * weight = 1 - * ) */ +#[EntityReferenceSelection( + id: "default:file", + label: new TranslatableMarkup("File selection"), + entity_types: ["file"], + group: "default", + weight: 1 +)] class FileSelection extends DefaultSelection { /** diff --git a/core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php b/core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php index 5bfe1ba611e..01b517c321c 100644 --- a/core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php +++ b/core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php @@ -2,19 +2,20 @@ namespace Drupal\media\Plugin\EntityReferenceSelection; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Provides specific access control for the media entity type. - * - * @EntityReferenceSelection( - * id = "default:media", - * label = @Translation("Media selection"), - * entity_types = {"media"}, - * group = "default", - * weight = 1 - * ) */ +#[EntityReferenceSelection( + id: "default:media", + label: new TranslatableMarkup("Media selection"), + entity_types: ["media"], + group: "default", + weight: 1 +)] class MediaSelection extends DefaultSelection { /** diff --git a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php index f40046c2931..a89e94f92e3 100644 --- a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php +++ b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php @@ -2,20 +2,21 @@ namespace Drupal\node\Plugin\EntityReferenceSelection; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\node\NodeInterface; /** * Provides specific access control for the node entity type. - * - * @EntityReferenceSelection( - * id = "default:node", - * label = @Translation("Node selection"), - * entity_types = {"node"}, - * group = "default", - * weight = 1 - * ) */ +#[EntityReferenceSelection( + id: "default:node", + label: new TranslatableMarkup("Node selection"), + entity_types: ["node"], + group: "default", + weight: 1 +)] class NodeSelection extends DefaultSelection { /** diff --git a/core/modules/system/tests/modules/entity_reference_test/src/Plugin/EntityReferenceSelection/AllExceptHostEntity.php b/core/modules/system/tests/modules/entity_reference_test/src/Plugin/EntityReferenceSelection/AllExceptHostEntity.php index 979bfe84c55..3f9a67688b5 100644 --- a/core/modules/system/tests/modules/entity_reference_test/src/Plugin/EntityReferenceSelection/AllExceptHostEntity.php +++ b/core/modules/system/tests/modules/entity_reference_test/src/Plugin/EntityReferenceSelection/AllExceptHostEntity.php @@ -2,17 +2,19 @@ namespace Drupal\entity_reference_test\Plugin\EntityReferenceSelection; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Allows access to all entities except for the host entity. - * - * @EntityReferenceSelection( - * id = "entity_test_all_except_host", - * label = @Translation("All except host entity."), - * group = "entity_test_all_except_host" - * ) */ +#[EntityReferenceSelection( + id: "entity_test_all_except_host", + label: new TranslatableMarkup("All except host entity."), + group: "entity_test_all_except_host", + weight: 0 +)] class AllExceptHostEntity extends DefaultSelection { /** diff --git a/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php b/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php index 295852c3fc1..12d5d02d100 100644 --- a/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php +++ b/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php @@ -3,21 +3,22 @@ namespace Drupal\taxonomy\Plugin\EntityReferenceSelection; use Drupal\Component\Utility\Html; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\taxonomy\Entity\Vocabulary; /** * Provides specific access control for the taxonomy_term entity type. - * - * @EntityReferenceSelection( - * id = "default:taxonomy_term", - * label = @Translation("Taxonomy Term selection"), - * entity_types = {"taxonomy_term"}, - * group = "default", - * weight = 1 - * ) */ +#[EntityReferenceSelection( + id: "default:taxonomy_term", + label: new TranslatableMarkup("Taxonomy Term selection"), + entity_types: ["taxonomy_term"], + group: "default", + weight: 1 +)] class TermSelection extends DefaultSelection { /** diff --git a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php index b3a616c3dc2..a29a573f300 100644 --- a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php +++ b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php @@ -4,6 +4,7 @@ namespace Drupal\user\Plugin\EntityReferenceSelection; use Drupal\Core\Database\Connection; use Drupal\Core\Database\Query\SelectInterface; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; @@ -12,21 +13,21 @@ use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides specific access control for the user entity type. - * - * @EntityReferenceSelection( - * id = "default:user", - * label = @Translation("User selection"), - * entity_types = {"user"}, - * group = "default", - * weight = 1 - * ) */ +#[EntityReferenceSelection( + id: "default:user", + label: new TranslatableMarkup("User selection"), + entity_types: ["user"], + group: "default", + weight: 1 +)] class UserSelection extends DefaultSelection { /** diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php index 80309f5b90f..15a4d7738ae 100644 --- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php +++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php @@ -3,6 +3,7 @@ namespace Drupal\views\Plugin\EntityReferenceSelection; use Drupal\Component\Utility\Xss; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -20,14 +21,13 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Plugin implementation of the 'selection' entity_reference. - * - * @EntityReferenceSelection( - * id = "views", - * label = @Translation("Views: Filter by an entity reference view"), - * group = "views", - * weight = 0 - * ) */ +#[EntityReferenceSelection( + id: "views", + label: new TranslatableMarkup("Views: Filter by an entity reference view"), + group: "views", + weight: 0 +)] class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPluginInterface { use StringTranslationTrait; diff --git a/core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php b/core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php index f76fc2f6f4c..ac57306f862 100644 --- a/core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php +++ b/core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php @@ -3,21 +3,22 @@ namespace Drupal\workspaces\Plugin\EntityReferenceSelection; use Drupal\Component\Utility\Html; +use Drupal\Core\Entity\Attribute\EntityReferenceSelection; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides specific access control for the workspace entity type. - * - * @EntityReferenceSelection( - * id = "default:workspace", - * label = @Translation("Workspace selection"), - * entity_types = {"workspace"}, - * group = "default", - * weight = 1 - * ) */ +#[EntityReferenceSelection( + id: "default:workspace", + label: new TranslatableMarkup("Workspace selection"), + entity_types: ["workspace"], + group: "default", + weight: 1 +)] class WorkspaceSelection extends DefaultSelection { /**