Workflow.php 15.4 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\workflows\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
6
use Drupal\Core\Entity\EntityStorageInterface;
7 8
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
9
use Drupal\workflows\Exception\RequiredStateMissingException;
10 11 12 13 14 15 16 17 18 19
use Drupal\workflows\State;
use Drupal\workflows\Transition;
use Drupal\workflows\WorkflowInterface;

/**
 * Defines the workflow entity.
 *
 * @ConfigEntityType(
 *   id = "workflow",
 *   label = @Translation("Workflow"),
20
 *   label_collection = @Translation("Workflows"),
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
 *   handlers = {
 *     "access" = "Drupal\workflows\WorkflowAccessControlHandler",
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
 *     },
 *   },
 *   config_prefix = "workflow",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid",
 *   },
 *   config_export = {
 *     "id",
 *     "label",
 *     "states",
 *     "transitions",
 *     "type",
 *     "type_settings"
 *   },
 * )
 *
 * @internal
 *   The workflow system is currently experimental and should only be leveraged
 *   by experimental modules and development releases of contributed modules.
 */
class Workflow extends ConfigEntityBase implements WorkflowInterface, EntityWithPluginCollectionInterface {

  /**
   * The Workflow ID.
   *
   * @var string
   */
  protected $id;

  /**
   * The Moderation state label.
   *
   * @var string
   */
  protected $label;

  /**
   * The states of the workflow.
   *
   * The array key is the machine name for the state. The structure of each
   * array item is:
   * @code
   *   label: {translatable label}
   *   weight: {integer value}
   * @endcode
   *
   * @var array
   */
  protected $states = [];

  /**
   * The permitted transitions of the workflow.
   *
   * The array key is the machine name for the transition. The machine name is
   * generated from the machine names of the states. The structure of each array
   * item is:
   * @code
   *   from:
   *     - {state machine name}
   *     - {state machine name}
   *   to: {state machine name}
   *   label: {translatable label}
   * @endcode
   *
   * @var array
   */
  protected $transitions = [];

  /**
   * The workflow type plugin ID.
   *
   * @see \Drupal\workflows\WorkflowTypeManager
   *
   * @var string
   */
  protected $type;

  /**
   * The configuration for the workflow type plugin.
   * @var array
   */
  protected $type_settings = [];

  /**
   * The workflow type plugin collection.
   *
   * @var \Drupal\Component\Plugin\LazyPluginCollection
   */
  protected $pluginCollection;

117 118 119 120 121 122 123 124 125 126 127 128
  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    $workflow_type = $this->getTypePlugin();
    $missing_states = array_diff($workflow_type->getRequiredStates(), array_keys($this->getStates()));
    if (!empty($missing_states)) {
      throw new RequiredStateMissingException(sprintf("Workflow type '{$workflow_type->label()}' requires states with the ID '%s' in workflow '{$this->id()}'", implode("', '", $missing_states)));
    }
    parent::preSave($storage);
  }

129 130 131 132 133 134 135 136 137 138 139 140 141 142
  /**
   * {@inheritdoc}
   */
  public function addState($state_id, $label) {
    if (isset($this->states[$state_id])) {
      throw new \InvalidArgumentException("The state '$state_id' already exists in workflow '{$this->id()}'");
    }
    if (preg_match('/[^a-z0-9_]+/', $state_id)) {
      throw new \InvalidArgumentException("The state ID '$state_id' must contain only lowercase letters, numbers, and underscores");
    }
    $this->states[$state_id] = [
      'label' => $label,
      'weight' => $this->getNextWeight($this->states),
    ];
143
    ksort($this->states);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function hasState($state_id) {
    return isset($this->states[$state_id]);
  }

  /**
   * {@inheritdoc}
   */
  public function getStates($state_ids = NULL) {
    if ($state_ids === NULL) {
      $state_ids = array_keys($this->states);
    }
    /** @var \Drupal\workflows\StateInterface[] $states */
    $states = array_combine($state_ids, array_map([$this, 'getState'], $state_ids));
    if (count($states) > 1) {
      // Sort states by weight and then label.
      $weights = $labels = [];
      foreach ($states as $id => $state) {
        $weights[$id] = $state->weight();
        $labels[$id] = $state->label();
      }
      array_multisort(
        $weights, SORT_NUMERIC, SORT_ASC,
        $labels, SORT_NATURAL, SORT_ASC
      );
      $states = array_replace($weights, $states);
    }
    return $states;
  }

  /**
   * {@inheritdoc}
   */
  public function getState($state_id) {
    if (!isset($this->states[$state_id])) {
      throw new \InvalidArgumentException("The state '$state_id' does not exist in workflow '{$this->id()}'");
    }
    $state = new State(
      $this,
      $state_id,
      $this->states[$state_id]['label'],
      $this->states[$state_id]['weight']
    );
    return $this->getTypePlugin()->decorateState($state);
  }

  /**
   * {@inheritdoc}
   */
  public function setStateLabel($state_id, $label) {
    if (!isset($this->states[$state_id])) {
      throw new \InvalidArgumentException("The state '$state_id' does not exist in workflow '{$this->id()}'");
    }
    $this->states[$state_id]['label'] = $label;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setStateWeight($state_id, $weight) {
    if (!isset($this->states[$state_id])) {
      throw new \InvalidArgumentException("The state '$state_id' does not exist in workflow '{$this->id()}'");
    }
    $this->states[$state_id]['weight'] = $weight;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteState($state_id) {
    if (!isset($this->states[$state_id])) {
      throw new \InvalidArgumentException("The state '$state_id' does not exist in workflow '{$this->id()}'");
    }
    if (count($this->states) === 1) {
      throw new \InvalidArgumentException("The state '$state_id' can not be deleted from workflow '{$this->id()}' as it is the only state");
    }

    foreach ($this->transitions as $transition_id => $transition) {
      $from_key = array_search($state_id, $transition['from'], TRUE);
      if ($from_key !== FALSE) {
        // Remove state from the from array.
        unset($transition['from'][$from_key]);
      }
      if (empty($transition['from']) || $transition['to'] === $state_id) {
        $this->deleteTransition($transition_id);
      }
      elseif ($from_key !== FALSE) {
        $this->setTransitionFromStates($transition_id, $transition['from']);
      }
    }
    unset($this->states[$state_id]);
    $this->getTypePlugin()->deleteState($state_id);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getInitialState() {
    $ordered_states = $this->getStates();
    return reset($ordered_states);
  }

  /**
   * {@inheritdoc}
   */
  public function addTransition($transition_id, $label, array $from_state_ids, $to_state_id) {
    if (isset($this->transitions[$transition_id])) {
      throw new \InvalidArgumentException("The transition '$transition_id' already exists in workflow '{$this->id()}'");
    }
    if (preg_match('/[^a-z0-9_]+/', $transition_id)) {
      throw new \InvalidArgumentException("The transition ID '$transition_id' must contain only lowercase letters, numbers, and underscores");
    }

    if (!$this->hasState($to_state_id)) {
      throw new \InvalidArgumentException("The state '$to_state_id' does not exist in workflow '{$this->id()}'");
    }
    $this->transitions[$transition_id] = [
      'label' => $label,
      'from' => [],
      'to' => $to_state_id,
      // Always add to the end.
      'weight' => $this->getNextWeight($this->transitions),
    ];

    try {
      $this->setTransitionFromStates($transition_id, $from_state_ids);
    }
    catch (\InvalidArgumentException $e) {
      unset($this->transitions[$transition_id]);
      throw $e;
    }

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getTransitions(array $transition_ids = NULL) {
    if ($transition_ids === NULL) {
      $transition_ids = array_keys($this->transitions);
    }
    /** @var \Drupal\workflows\TransitionInterface[] $transitions */
    $transitions = array_combine($transition_ids, array_map([$this, 'getTransition'], $transition_ids));
    if (count($transitions) > 1) {
      // Sort transitions by weights and then labels.
      $weights = $labels = [];
      foreach ($transitions as $id => $transition) {
        $weights[$id] = $transition->weight();
        $labels[$id] = $transition->label();
      }
      array_multisort(
        $weights, SORT_NUMERIC, SORT_ASC,
        $labels, SORT_NATURAL, SORT_ASC
      );
      $transitions = array_replace($weights, $transitions);
    }
    return $transitions;
  }

  /**
   * {@inheritdoc}
   */
  public function getTransition($transition_id) {
    if (!isset($this->transitions[$transition_id])) {
      throw new \InvalidArgumentException("The transition '$transition_id' does not exist in workflow '{$this->id()}'");
    }
    $transition = new Transition(
      $this,
      $transition_id,
      $this->transitions[$transition_id]['label'],
      $this->transitions[$transition_id]['from'],
      $this->transitions[$transition_id]['to'],
      $this->transitions[$transition_id]['weight']
    );
    return $this->getTypePlugin()->decorateTransition($transition);
  }

  /**
   * {@inheritdoc}
   */
  public function hasTransition($transition_id) {
    return isset($this->transitions[$transition_id]);
  }

  /**
   * {@inheritdoc}
   */
  public function getTransitionsForState($state_id, $direction = 'from') {
    $transition_ids = array_keys(array_filter($this->transitions, function ($transition) use ($state_id, $direction) {
      return in_array($state_id, (array) $transition[$direction], TRUE);
    }));
    return $this->getTransitions($transition_ids);
  }

  /**
   * {@inheritdoc}
   */
  public function getTransitionFromStateToState($from_state_id, $to_state_id) {
    $transition_id = $this->getTransitionIdFromStateToState($from_state_id, $to_state_id);
    if (empty($transition_id)) {
      throw new \InvalidArgumentException("The transition from '$from_state_id' to '$to_state_id' does not exist in workflow '{$this->id()}'");
    }
    return $this->getTransition($transition_id);
  }

  /**
   * {@inheritdoc}
   */
  public function hasTransitionFromStateToState($from_state_id, $to_state_id) {
    return !empty($this->getTransitionIdFromStateToState($from_state_id, $to_state_id));
  }

  /**
   * Gets the transition ID from state to state.
   *
   * @param string $from_state_id
   *   The state ID to transition from.
   * @param string $to_state_id
   *   The state ID to transition to.
   *
   * @return string|null
   *   The transition ID, or NULL if no transition exists.
   */
  protected function getTransitionIdFromStateToState($from_state_id, $to_state_id) {
    foreach ($this->transitions as $transition_id => $transition) {
      if (in_array($from_state_id, $transition['from'], TRUE) && $transition['to'] === $to_state_id) {
        return $transition_id;
      }
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setTransitionLabel($transition_id, $label) {
    if (isset($this->transitions[$transition_id])) {
      $this->transitions[$transition_id]['label'] = $label;
    }
    else {
      throw new \InvalidArgumentException("The transition '$transition_id' does not exist in workflow '{$this->id()}'");
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setTransitionWeight($transition_id, $weight) {
    if (isset($this->transitions[$transition_id])) {
      $this->transitions[$transition_id]['weight'] = $weight;
    }
    else {
      throw new \InvalidArgumentException("The transition '$transition_id' does not exist in workflow '{$this->id()}'");
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setTransitionFromStates($transition_id, array $from_state_ids) {
    if (!isset($this->transitions[$transition_id])) {
      throw new \InvalidArgumentException("The transition '$transition_id' does not exist in workflow '{$this->id()}'");
    }

    // Ensure that the states exist.
    foreach ($from_state_ids as $from_state_id) {
      if (!$this->hasState($from_state_id)) {
        throw new \InvalidArgumentException("The state '$from_state_id' does not exist in workflow '{$this->id()}'");
      }
      if ($this->hasTransitionFromStateToState($from_state_id, $this->transitions[$transition_id]['to'])) {
        $transition = $this->getTransitionFromStateToState($from_state_id, $this->transitions[$transition_id]['to']);
        if ($transition_id !== $transition->id()) {
          throw new \InvalidArgumentException("The '{$transition->id()}' transition already allows '$from_state_id' to '{$this->transitions[$transition_id]['to']}' transitions in workflow '{$this->id()}'");
        }
      }
    }

    // Preserve the order of the state IDs in the from value and don't save any
    // keys.
    $from_state_ids = array_values($from_state_ids);
    sort($from_state_ids);
    $this->transitions[$transition_id]['from'] = $from_state_ids;
437
    ksort($this->transitions);
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteTransition($transition_id) {
    if (isset($this->transitions[$transition_id])) {
      unset($this->transitions[$transition_id]);
      $this->getTypePlugin()->deleteTransition($transition_id);
    }
    else {
      throw new \InvalidArgumentException("The transition '$transition_id' does not exist in workflow '{$this->id()}'");
    }
    return $this;
  }

  /**
   * {@inheritDoc}
   */
  public function getTypePlugin() {
    return $this->getPluginCollection()->get($this->type);
  }

  /**
   * {@inheritDoc}
   */
  public function getPluginCollections() {
    return ['type_settings' => $this->getPluginCollection()];
  }

  /**
   * Encapsulates the creation of the workflow's plugin collection.
   *
   * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
   *   The workflow's plugin collection.
   */
  protected function getPluginCollection() {
    if (!$this->pluginCollection && $this->type) {
      $this->pluginCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.workflows.type'), $this->type, $this->type_settings);
    }
    return $this->pluginCollection;
  }

  /**
   * Loads all workflows of the provided type.
   *
   * @param string $type
   *   The workflow type to load all workflows for.
   *
   * @return static[]
   *   An array of workflow objects of the provided workflow type, indexed by
   *   their IDs.
   *
   *  @see \Drupal\workflows\Annotation\WorkflowType
   */
  public static function loadMultipleByType($type) {
    return self::loadMultiple(\Drupal::entityQuery('workflow')->condition('type', $type)->execute());
  }

  /**
   * Gets the weight for a new state or transition.
   *
   * @param array $items
   *   An array of states or transitions information where each item has a
   *   'weight' key with a numeric value.
   *
   * @return int
   *   The weight for a new item in the array so that it has the highest weight.
   */
  protected function getNextWeight(array $items) {
    return array_reduce($items, function ($carry, $item) {
      return max($carry, $item['weight'] + 1);
    }, 0);
  }

  /**
   * {@inheritdoc}
   */
  public function status() {
    // In order for a workflow to be usable it must have at least one state.
    return !empty($this->status) && !empty($this->states);
  }

}