Version 1
[yaffs-website] / web / core / modules / workflows / src / Plugin / WorkflowTypeBase.php
1 <?php
2
3 namespace Drupal\workflows\Plugin;
4
5 use Drupal\Component\Plugin\PluginBase;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Access\AccessResult;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\workflows\StateInterface;
11 use Drupal\workflows\TransitionInterface;
12 use Drupal\workflows\WorkflowInterface;
13 use Drupal\workflows\WorkflowTypeInterface;
14
15 /**
16  * A base class for Workflow type plugins.
17  *
18  * @see \Drupal\workflows\Annotation\WorkflowType
19  *
20  * @internal
21  *   The workflow system is currently experimental and should only be leveraged
22  *   by experimental modules and development releases of contributed modules.
23  */
24 abstract class WorkflowTypeBase extends PluginBase implements WorkflowTypeInterface {
25
26   /**
27    * {@inheritdoc}
28    */
29   public function __construct(array $configuration, $plugin_id, $plugin_definition) {
30     parent::__construct($configuration, $plugin_id, $plugin_definition);
31     $this->setConfiguration($configuration);
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function initializeWorkflow(WorkflowInterface $workflow) {
38     return $workflow;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function label() {
45     $definition = $this->getPluginDefinition();
46     // The label can be an object.
47     // @see \Drupal\Core\StringTranslation\TranslatableMarkup
48     return $definition['label'];
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function checkWorkflowAccess(WorkflowInterface $entity, $operation, AccountInterface $account) {
55     return AccessResult::neutral();
56   }
57
58   /**
59    * {@inheritDoc}
60    */
61   public function decorateState(StateInterface $state) {
62     return $state;
63   }
64
65   /**
66    * {@inheritDoc}
67    */
68   public function deleteState($state_id) {
69     unset($this->configuration['states'][$state_id]);
70   }
71
72   /**
73    * {@inheritDoc}
74    */
75   public function decorateTransition(TransitionInterface $transition) {
76     return $transition;
77   }
78
79   /**
80    * {@inheritDoc}
81    */
82   public function deleteTransition($transition_id) {
83     unset($this->configuration['transitions'][$transition_id]);
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function buildStateConfigurationForm(FormStateInterface $form_state, WorkflowInterface $workflow, StateInterface $state = NULL) {
90     return [];
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function buildTransitionConfigurationForm(FormStateInterface $form_state, WorkflowInterface $workflow, TransitionInterface $transition = NULL) {
97     return [];
98   }
99
100   /**
101    * {@inheritDoc}
102    */
103   public function getConfiguration() {
104     return $this->configuration;
105   }
106
107   /**
108    * {@inheritDoc}
109    */
110   public function setConfiguration(array $configuration) {
111     $this->configuration = NestedArray::mergeDeep(
112       $this->defaultConfiguration(),
113       $configuration
114     );
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function getRequiredStates() {
121     return $this->getPluginDefinition()['required_states'];
122   }
123
124   /**
125    * {@inheritDoc}
126    */
127   public function defaultConfiguration() {
128     return [
129       'states' => [],
130       'transitions' => [],
131     ];
132   }
133
134   /**
135    * {@inheritDoc}
136    */
137   public function calculateDependencies() {
138     return [];
139   }
140
141   /**
142    * {@inheritdoc}
143    */
144   public function onDependencyRemoval(array $dependencies) {
145     return FALSE;
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function getInitialState(WorkflowInterface $workflow) {
152     $ordered_states = $workflow->getStates();
153     return reset($ordered_states);
154   }
155
156 }