You are here

public function WorkflowStateListBuilder::submitForm in Workflow 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides DraggableListBuilder::submitForm

File

src/WorkflowStateListBuilder.php, line 282

Class

WorkflowStateListBuilder
Defines a class to build a draggable listing of Workflow State entities.

Namespace

Drupal\workflow

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if (!($workflow = workflow_url_get_workflow())) {
    return [];
  }

  // The default min_weight is -10. Work with it.
  $creation_weight = -11;
  $max_weight = -9;

  // The WorkflowState entities are always saved.
  foreach ($form_state
    ->getValue($this->entitiesKey) as $sid => $value) {
    if (!isset($this->entities[$sid])) {
      continue;
    }
    if (empty($value['label_new'])) {

      // No new state/state name entered, so skip it.
      continue;
    }

    /** @var \Drupal\workflow\Entity\WorkflowState $state */
    $state = $this->entities[$sid];
    if ($state && $state
      ->isActive() && !$value['status'] && $sid) {

      // State is deactivated, reassigning current content.
      $new_sid = $value['reassign'];
      $new_state = WorkflowState::load($new_sid);
      $args = [
        '%workflow' => $workflow
          ->label(),
        '%old_state' => $state
          ->label(),
        '%new_state' => isset($new_state) ? $new_state
          ->label() : '',
      ];
      if ($value['count'] > 0) {
        if ($form['#last_mohican']) {

          // Do not reassign to new state.
          $new_sid = NULL;
          $message = 'Removing workflow states from content in the %workflow.';
          $this
            ->messenger()
            ->addStatus($this
            ->t($message, $args));
          $message = 'Since you have deleted the last available
                workflow state in this workflow, all content items
                which with this %workflow workflow have their workflow state
                removed.';
          $this
            ->messenger()
            ->addWarning($this
            ->t($message, $args));
        }
        else {

          // Prepare the state delete function.
          $message = 'Reassigning content from %old_state to %new_state.';
          $this
            ->messenger()
            ->addStatus($this
            ->t($message, $args));
        }
      }

      // Delete old State without orphaning content by moving it to new State.
      $state
        ->deactivate($new_sid);
      $message = $this
        ->t('Deactivated workflow state %old_state in %workflow.', $args);
      \Drupal::logger('workflow')
        ->notice($message, []);
      $this
        ->messenger()
        ->addStatus($message);
    }
    $weight = $value['weight'];
    if ($value['sysid'] == WORKFLOW_CREATION_STATE) {

      // Assure Creation state is first in line.
      $weight = $creation_weight;
    }
    elseif ($sid === 'placeholder') {

      // Add a new State.
      $state
        ->set('id', $value['id']);

      // Set a proper weight to the new state, adding as last.
      $max_weight = max($max_weight, $state
        ->get($this->weightKey));
      $weight = $max_weight + 1;
    }
    $state
      ->set($this->weightKey, $weight);
    $state
      ->set('label', $value['label_new']);
    $state
      ->set('status', $value['status']);
    try {
      $state
        ->save();
    } catch (\Exception $e) {
      $args = [
        '%id' => $state
          ->id(),
      ];
      return $this
        ->messenger()
        ->addError($this
        ->t('ID %id already exists', $args));
    }
  }
  if ($form_state
    ->getTriggeringElement()['#name'] === 'add_state') {

    // Unset previous input in placeholder and rebuild the form.
    $input = $form_state
      ->getUserInput();
    unset($input['entities']['placeholder']['label_new']);
    $form_state
      ->setUserInput($input);
    $form_state
      ->setRebuild();
  }
  return $this
    ->messenger()
    ->addStatus($this
    ->t('The Workflow states have been updated.'));
}