Verified Commit 60d9d677 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3421018 by mohit_aghera, smustgrave: Convert WorkflowType plugin discovery to attributes

parent 2001751f
Loading
Loading
Loading
Loading
Loading
+14 −13
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@
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 @@

/**
 * 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;
+71 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\workflows\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a Workflow type attribute object.
 *
 * Plugin Namespace: Plugin\WorkflowType
 *
 * For a working example, see \Drupal\content_moderation\Plugin\Workflow\ContentModerate
 *
 * @see \Drupal\workflows\WorkflowTypeInterface
 * @see \Drupal\workflows\WorkflowTypeManager
 * @see workflow_type_info_alter()
 * @see plugin_api
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class WorkflowType extends Plugin {

  /**
   * States required to exist.
   *
   * Normally supplied by WorkflowType::defaultConfiguration().
   */
  public array $required_states = [];

  /**
   * A list of optional form classes implementing PluginFormInterface.
   *
   * Forms which will be used for the workflow UI are:
   * - 'configure' (\Drupal\workflows\WorkflowTypeInterface::PLUGIN_FORM_KEY)
   * - 'state' (\Drupal\workflows\StateInterface::PLUGIN_FORM_KEY)
   * - 'transition' (\Drupal\workflows\TransitionInterface::PLUGIN_FORM_KEY)
   *
   * @see \Drupal\Core\Plugin\PluginWithFormsInterface
   * @see \Drupal\Core\Plugin\PluginFormInterface
   * @see \Drupal\workflows\Plugin\WorkflowTypeConfigureFormBase
   * @see \Drupal\workflows\Plugin\WorkflowTypeStateFormBase
   * @see \Drupal\workflows\Plugin\WorkflowTypeTransitionFormBase
   * @see \Drupal\workflows\WorkflowTypeInterface::PLUGIN_FORM_KEY
   * @see \Drupal\workflows\StateInterface::PLUGIN_FORM_KEY
   * @see \Drupal\workflows\TransitionInterface::PLUGIN_FORM_KEY
   */
  public array $forms = [];

  /**
   * Constructs an Action attribute.
   *
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $label
   *   The label of the action.
   * @param string[] $forms
   *   A list of optional form classes implementing PluginFormInterface.
   * @param string[] $required_states
   *   States required to exist.
   */
  public function __construct(
    public readonly string $id,
    public readonly ?TranslatableMarkup $label = NULL,
    array $forms = [],
    array $required_states = [],
  ) {
    $this->forms = $forms;
    $this->required_states = $required_states;
  }

}
+2 −2
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
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');
  }
+11 −10
Original line number Diff line number Diff line
@@ -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;
+12 −11
Original line number Diff line number Diff line
@@ -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 {

  /**
Loading