You are here

public function WorkflowManager::processDefinition in State Machine 8

Performs extra processing on plugin definitions.

By default we add defaults for the type to the definition. If a type has additional processing logic they can do that by replacing or extending the method.

Overrides DefaultPluginManager::processDefinition

File

src/WorkflowManager.php, line 102

Class

WorkflowManager
Manages discovery and instantiation of workflow plugins.

Namespace

Drupal\state_machine

Code

public function processDefinition(&$definition, $plugin_id) {
  parent::processDefinition($definition, $plugin_id);
  $definition['id'] = $plugin_id;
  foreach ([
    'label',
    'group',
    'states',
    'transitions',
  ] as $required_property) {
    if (empty($definition[$required_property])) {
      throw new PluginException(sprintf('The workflow %s must define the %s property.', $plugin_id, $required_property));
    }
  }
  foreach ($definition['states'] as $state_id => $state_definition) {
    if (empty($state_definition['label'])) {
      throw new PluginException(sprintf('The workflow state %s must define the label property.', $state_id));
    }
  }
  foreach ($definition['transitions'] as $transition_id => $transition_definition) {
    foreach ([
      'label',
      'from',
      'to',
    ] as $required_property) {
      if (empty($transition_definition[$required_property])) {
        throw new PluginException(sprintf('The workflow transition %s must define the %s property.', $transition_id, $required_property));
      }
    }

    // Validate the referenced "from" and "to" states.
    foreach ($transition_definition['from'] as $from_state) {
      if (!isset($definition['states'][$from_state])) {
        throw new PluginException(sprintf('The workflow transition %s specified an invalid "from" property: %s.', $transition_id, $from_state));
      }
    }
    $to_state = $transition_definition['to'];
    if (!isset($definition['states'][$to_state])) {
      throw new PluginException(sprintf('The workflow transition %s specified an invalid "to" property.', $transition_id));
    }

    // Don't allow transitions to the same state.
    foreach ($transition_definition['from'] as $from_state) {
      if ($from_state == $transition_definition['to']) {
        throw new PluginException(sprintf('Invalid workflow transition %s: the "from" and "to" properties cannot overlap.', $transition_id));
      }
    }
  }
}