class Workflow in State Machine 8
Defines the class for workflows.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\state_machine\Plugin\Workflow\Workflow implements ContainerFactoryPluginInterface, WorkflowInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\WorkflowView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
Workflow:: |
protected | property | The guard factory. | |
Workflow:: |
protected | property | The initialized states. | |
Workflow:: |
protected | property | The initialized transitions. | |
Workflow:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
Workflow:: |
public | function |
Finds the workflow transition for moving between two given states. Overrides WorkflowInterface:: |
|
Workflow:: |
public | function |
Gets the allowed workflow transitions for the given state ID. Overrides WorkflowInterface:: |
|
Workflow:: |
public | function |
Gets the workflow group. Overrides WorkflowInterface:: |
|
Workflow:: |
public | function |
Gets the workflow ID. Overrides WorkflowInterface:: |
|
Workflow:: |
public | function |
Gets the translated label. Overrides WorkflowInterface:: |
|
Workflow:: |
public | function |
Gets the possible workflow transitions for the given state ID. Overrides WorkflowInterface:: |
|
Workflow:: |
public | function |
Gets a workflow state with the given ID. Overrides WorkflowInterface:: |
|
Workflow:: |
public | function |
Gets the workflow states. Overrides WorkflowInterface:: |
|
Workflow:: |
public | function |
Gets a workflow transition with the given ID. Overrides WorkflowInterface:: |
|
Workflow:: |
public | function |
Gets the workflow transitions. Overrides WorkflowInterface:: |
|
Workflow:: |
protected | function | Gets whether the given transition is allowed. | |
Workflow:: |
public | function |
Constructs a new Workflow object. Overrides PluginBase:: |