From e5e52023d6b4c5d08d66d97425e9039df8d64839 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sat, 2 Mar 2024 10:59:12 +0000 Subject: [PATCH] Issue #3421018 by mohit_aghera, smustgrave: Convert WorkflowType plugin discovery to attributes (cherry picked from commit 60d9d6778e1be5d53467cf786e71b909855c1b16) --- .../Plugin/WorkflowType/ContentModeration.php | 27 +++---- .../workflows/src/Attribute/WorkflowType.php | 71 +++++++++++++++++++ .../workflows/src/WorkflowTypeManager.php | 4 +- .../Plugin/WorkflowType/ComplexTestType.php | 21 +++--- .../PredefinedStatesWorkflowTestType.php | 23 +++--- .../WorkflowType/RequiredStateTestType.php | 19 ++--- .../src/Plugin/WorkflowType/TestType.php | 11 +-- .../WorkflowType/WorkflowCustomAccessType.php | 11 +-- 8 files changed, 132 insertions(+), 55 deletions(-) create mode 100644 core/modules/workflows/src/Attribute/WorkflowType.php diff --git a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php index d967f717e4a..d05d34cb645 100644 --- a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php +++ b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php @@ -10,6 +10,8 @@ use Drupal\Core\Entity\EntityPublishedInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\content_moderation\ContentModerationState; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\workflows\Attribute\WorkflowType; use Drupal\workflows\Plugin\WorkflowTypeBase; use Drupal\workflows\StateInterface; use Drupal\workflows\WorkflowInterface; @@ -17,20 +19,19 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** * Attaches workflows to content entity types and their bundles. - * - * @WorkflowType( - * id = "content_moderation", - * label = @Translation("Content moderation"), - * required_states = { - * "draft", - * "published", - * }, - * forms = { - * "configure" = "\Drupal\content_moderation\Form\ContentModerationConfigureForm", - * "state" = "\Drupal\content_moderation\Form\ContentModerationStateForm" - * }, - * ) */ +#[WorkflowType( + id: 'content_moderation', + label: new TranslatableMarkup('Content moderation'), + forms: [ + 'configure' => '\Drupal\content_moderation\Form\ContentModerationConfigureForm', + 'state' => '\Drupal\content_moderation\Form\ContentModerationStateForm', + ], + required_states: [ + 'draft', + 'published', + ] +)] class ContentModeration extends WorkflowTypeBase implements ContentModerationInterface, ContainerFactoryPluginInterface { use StringTranslationTrait; diff --git a/core/modules/workflows/src/Attribute/WorkflowType.php b/core/modules/workflows/src/Attribute/WorkflowType.php new file mode 100644 index 00000000000..b60b80005f6 --- /dev/null +++ b/core/modules/workflows/src/Attribute/WorkflowType.php @@ -0,0 +1,71 @@ +forms = $forms; + $this->required_states = $required_states; + } + +} diff --git a/core/modules/workflows/src/WorkflowTypeManager.php b/core/modules/workflows/src/WorkflowTypeManager.php index 457b36d5da0..1252832e1fc 100644 --- a/core/modules/workflows/src/WorkflowTypeManager.php +++ b/core/modules/workflows/src/WorkflowTypeManager.php @@ -5,7 +5,7 @@ namespace Drupal\workflows; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; -use Drupal\workflows\Annotation\WorkflowType; +use Drupal\workflows\Attribute\WorkflowType; /** * Provides a Workflow type plugin manager. @@ -28,7 +28,7 @@ class WorkflowTypeManager extends DefaultPluginManager { * The module handler to invoke the alter hook with. */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { - parent::__construct('Plugin/WorkflowType', $namespaces, $module_handler, WorkflowTypeInterface::class, WorkflowType::class); + parent::__construct('Plugin/WorkflowType', $namespaces, $module_handler, WorkflowTypeInterface::class, WorkflowType::class, 'Drupal\workflows\Annotation\WorkflowType'); $this->alterInfo('workflow_type_info'); $this->setCacheBackend($cache_backend, 'workflow_type_info'); } diff --git a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/ComplexTestType.php b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/ComplexTestType.php index 30b175c32e1..71d67c620c7 100644 --- a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/ComplexTestType.php +++ b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/ComplexTestType.php @@ -3,21 +3,22 @@ namespace Drupal\workflow_type_test\Plugin\WorkflowType; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\workflows\Attribute\WorkflowType; use Drupal\workflows\Plugin\WorkflowTypeBase; /** * Test workflow type. - * - * @WorkflowType( - * id = "workflow_type_complex_test", - * label = @Translation("Workflow Type Complex Test"), - * forms = { - * "configure" = "\Drupal\workflow_type_test\Form\ComplexTestTypeConfigureForm", - * "state" = "\Drupal\workflow_type_test\Form\ComplexTestTypeStateForm", - * "transition" = "\Drupal\workflow_type_test\Form\ComplexTestTypeTransitionForm", - * } - * ) */ +#[WorkflowType( + id: 'workflow_type_complex_test', + label: new TranslatableMarkup('Workflow Type Complex Test'), + forms: [ + 'configure' => '\Drupal\workflow_type_test\Form\ComplexTestTypeConfigureForm', + 'state' => '\Drupal\workflow_type_test\Form\ComplexTestTypeStateForm', + 'transition' => '\Drupal\workflow_type_test\Form\ComplexTestTypeTransitionForm', + ] +)] class ComplexTestType extends WorkflowTypeBase { use StringTranslationTrait; diff --git a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/PredefinedStatesWorkflowTestType.php b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/PredefinedStatesWorkflowTestType.php index aab488aae00..3e209faa7d4 100644 --- a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/PredefinedStatesWorkflowTestType.php +++ b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/PredefinedStatesWorkflowTestType.php @@ -2,23 +2,24 @@ namespace Drupal\workflow_type_test\Plugin\WorkflowType; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\workflows\Attribute\WorkflowType; use Drupal\workflows\Plugin\WorkflowTypeBase; use Drupal\workflows\State; /** * Test workflow type. - * - * @WorkflowType( - * id = "predefined_states_workflow_test_type", - * label = @Translation("Predefined States Workflow Test Type"), - * required_states = { - * "pay_blinds", - * "bet", - * "raise", - * "fold", - * } - * ) */ +#[WorkflowType( + id: 'predefined_states_workflow_test_type', + label: new TranslatableMarkup('Predefined States Workflow Test Type'), + required_states: [ + 'pay_blinds', + 'bet', + 'raise', + 'fold', + ] +)] class PredefinedStatesWorkflowTestType extends WorkflowTypeBase { /** diff --git a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/RequiredStateTestType.php b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/RequiredStateTestType.php index c2c88ddcdae..daf2ccc01f4 100644 --- a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/RequiredStateTestType.php +++ b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/RequiredStateTestType.php @@ -3,20 +3,21 @@ namespace Drupal\workflow_type_test\Plugin\WorkflowType; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\workflows\Attribute\WorkflowType; use Drupal\workflows\Plugin\WorkflowTypeBase; /** * Test workflow type. - * - * @WorkflowType( - * id = "workflow_type_required_state_test", - * label = @Translation("Required State Type Test"), - * required_states = { - * "fresh", - * "rotten", - * } - * ) */ +#[WorkflowType( + id: 'workflow_type_required_state_test', + label: new TranslatableMarkup('Required State Type Test'), + required_states: [ + 'fresh', + 'rotten', + ] +)] class RequiredStateTestType extends WorkflowTypeBase { use StringTranslationTrait; diff --git a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/TestType.php b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/TestType.php index ddd074a019d..1a5ba4fd838 100644 --- a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/TestType.php +++ b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/TestType.php @@ -2,16 +2,17 @@ namespace Drupal\workflow_type_test\Plugin\WorkflowType; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\workflows\Attribute\WorkflowType; use Drupal\workflows\Plugin\WorkflowTypeBase; /** * Test workflow type. - * - * @WorkflowType( - * id = "workflow_type_test", - * label = @Translation("Workflow Type Test"), - * ) */ +#[WorkflowType( + id: 'workflow_type_test', + label: new TranslatableMarkup('Workflow Type Test') +)] class TestType extends WorkflowTypeBase { /** diff --git a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/WorkflowCustomAccessType.php b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/WorkflowCustomAccessType.php index 81c7d2e4ef7..f5d728dbb03 100644 --- a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/WorkflowCustomAccessType.php +++ b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/WorkflowCustomAccessType.php @@ -4,17 +4,18 @@ namespace Drupal\workflow_type_test\Plugin\WorkflowType; use Drupal\Core\Access\AccessResult; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\workflows\Attribute\WorkflowType; use Drupal\workflows\Plugin\WorkflowTypeBase; use Drupal\workflows\WorkflowInterface; /** * A test workflow with custom state/transition access rules applied. - * - * @WorkflowType( - * id = "workflow_custom_access_type", - * label = @Translation("Workflow Custom Access Type Test"), - * ) */ +#[WorkflowType( + id: 'workflow_custom_access_type', + label: new TranslatableMarkup('Workflow Custom Access Type Test') +)] class WorkflowCustomAccessType extends WorkflowTypeBase { /**