You are here

class Workflow in State Machine 8

Defines the class for workflows.

Hierarchy

Expanded class hierarchy of Workflow

4 files declare their use of Workflow
WorkflowGroupManager.php in src/WorkflowGroupManager.php
WorkflowGroupTest.php in tests/src/Unit/Plugin/WorkflowGroup/WorkflowGroupTest.php
WorkflowManagerCacheTest.php in tests/src/Unit/WorkflowManagerCacheTest.php
WorkflowTest.php in tests/src/Unit/Plugin/Workflow/WorkflowTest.php
3 string references to 'Workflow'
StateItem::fieldSettingsForm in src/Plugin/Field/FieldType/StateItem.php
Returns a form for the field-level settings.
state_machine.plugin_type.yml in ./state_machine.plugin_type.yml
state_machine.plugin_type.yml
state_machine.schema.yml in config/schema/state_machine.schema.yml
config/schema/state_machine.schema.yml

File

src/Plugin/Workflow/Workflow.php, line 14

Namespace

Drupal\state_machine\Plugin\Workflow
View source
class Workflow extends PluginBase implements WorkflowInterface, ContainerFactoryPluginInterface {

  /**
   * The guard factory.
   *
   * @var \Drupal\state_machine\Guard\GuardFactoryInterface
   */
  protected $guardFactory;

  /**
   * The initialized states.
   *
   * @var \Drupal\state_machine\Plugin\Workflow\WorkflowState[]
   */
  protected $states = [];

  /**
   * The initialized transitions.
   *
   * @var \Drupal\state_machine\Plugin\Workflow\WorkflowTransition[]
   */
  protected $transitions = [];

  /**
   * Constructs a new Workflow object.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The workflow plugin_id.
   * @param mixed $plugin_definition
   *   The workflow plugin implementation definition.
   * @param \Drupal\state_machine\Guard\GuardFactoryInterface $guard_factory
   *   The guard factory.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, GuardFactoryInterface $guard_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->guardFactory = $guard_factory;

    // Populate value objects for states and transitions.
    foreach ($plugin_definition['states'] as $id => $state_definition) {
      $this->states[$id] = new WorkflowState($id, $state_definition['label']);
    }
    foreach ($plugin_definition['transitions'] as $id => $transition_definition) {
      $label = $transition_definition['label'];
      $from_states = [];
      foreach ($transition_definition['from'] as $from_state) {
        $from_states[$from_state] = $this->states[$from_state];
      }
      $to_state = $this->states[$transition_definition['to']];
      $this->transitions[$id] = new WorkflowTransition($id, $label, $from_states, $to_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('state_machine.guard_factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->pluginDefinition['id'];
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->pluginDefinition['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function getGroup() {
    return $this->pluginDefinition['group'];
  }

  /**
   * {@inheritdoc}
   */
  public function getStates() {
    return $this->states;
  }

  /**
   * {@inheritdoc}
   */
  public function getState($id) {
    return isset($this->states[$id]) ? $this->states[$id] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getTransitions() {
    return $this->transitions;
  }

  /**
   * {@inheritdoc}
   */
  public function getTransition($id) {
    return isset($this->transitions[$id]) ? $this->transitions[$id] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getPossibleTransitions($state_id) {
    if (empty($state_id)) {
      return $this->transitions;
    }
    $possible_transitions = [];
    foreach ($this->transitions as $id => $transition) {
      if (array_key_exists($state_id, $transition
        ->getFromStates())) {
        $possible_transitions[$id] = $transition;
      }
    }
    return $possible_transitions;
  }

  /**
   * {@inheritdoc}
   */
  public function getAllowedTransitions($state_id, EntityInterface $entity) {
    $allowed_transitions = [];
    foreach ($this
      ->getPossibleTransitions($state_id) as $transition_id => $transition) {
      if ($this
        ->isTransitionAllowed($transition, $entity)) {
        $allowed_transitions[$transition_id] = $transition;
      }
    }
    return $allowed_transitions;
  }

  /**
   * {@inheritdoc}
   */
  public function findTransition($from_state_id, $to_state_id) {
    foreach ($this
      ->getPossibleTransitions($from_state_id) as $transition) {
      if ($transition
        ->getToState()
        ->getId() == $to_state_id) {
        return $transition;
      }
    }
    return NULL;
  }

  /**
   * Gets whether the given transition is allowed.
   *
   * @param \Drupal\state_machine\Plugin\Workflow\WorkflowTransition $transition
   *   The transition.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The parent entity.
   *
   * @return bool
   *   TRUE if the transition is allowed, FALSE otherwise.
   */
  protected function isTransitionAllowed(WorkflowTransition $transition, EntityInterface $entity) {
    foreach ($this->guardFactory
      ->get($this
      ->getGroup()) as $guard) {
      if ($guard
        ->allowed($transition, $this, $entity) === FALSE) {
        return FALSE;
      }
    }
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Workflow::$guardFactory protected property The guard factory.
Workflow::$states protected property The initialized states.
Workflow::$transitions protected property The initialized transitions.
Workflow::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Workflow::findTransition public function Finds the workflow transition for moving between two given states. Overrides WorkflowInterface::findTransition
Workflow::getAllowedTransitions public function Gets the allowed workflow transitions for the given state ID. Overrides WorkflowInterface::getAllowedTransitions
Workflow::getGroup public function Gets the workflow group. Overrides WorkflowInterface::getGroup
Workflow::getId public function Gets the workflow ID. Overrides WorkflowInterface::getId
Workflow::getLabel public function Gets the translated label. Overrides WorkflowInterface::getLabel
Workflow::getPossibleTransitions public function Gets the possible workflow transitions for the given state ID. Overrides WorkflowInterface::getPossibleTransitions
Workflow::getState public function Gets a workflow state with the given ID. Overrides WorkflowInterface::getState
Workflow::getStates public function Gets the workflow states. Overrides WorkflowInterface::getStates
Workflow::getTransition public function Gets a workflow transition with the given ID. Overrides WorkflowInterface::getTransition
Workflow::getTransitions public function Gets the workflow transitions. Overrides WorkflowInterface::getTransitions
Workflow::isTransitionAllowed protected function Gets whether the given transition is allowed.
Workflow::__construct public function Constructs a new Workflow object. Overrides PluginBase::__construct