You are here

public function WorkflowCleanupSettingsForm::submitForm in Workflow 8

@inheritdoc

Overrides FormInterface::submitForm

File

modules/workflow_cleanup/src/Form/WorkflowCleanupSettingsForm.php, line 98

Class

WorkflowCleanupSettingsForm
Provides a Form for organizing obsolete States.

Namespace

Drupal\workflow_cleanup\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $states = $form['#workflow_states'];
  $values = $form_state
    ->getValues();
  foreach ([
    'no_workflow',
    'inactive',
  ] as $section) {
    if (!isset($values[$section])) {
      continue;
    }
    foreach ($values[$section] as $sid => $data) {
      if (!$data['check']) {
        continue;
      }

      /** @var \Drupal\workflow\Entity\WorkflowState $state */
      $state = $states[$sid];
      $state_name = $state
        ->label();

      // Delete any transitions this state is involved in.
      $count = 0;
      foreach (WorkflowConfigTransition::loadMultiple() as $config_transition) {

        /** @var \Drupal\workflow\Entity\WorkflowConfigTransition $config_transition */
        if ($config_transition
          ->getFromSid() == $sid || $config_transition
          ->getToSid() == $sid) {
          $config_transition
            ->delete();
          $count++;
        }
      }
      if ($count) {
        $this
          ->messenger()
          ->addStatus($this
          ->t('@count transitions for the "@state" state have been deleted.', [
          '@state' => $state_name,
          '@count' => $count,
        ]));
      }

      // @todo Remove history records, too.
      $count = 0;

      // $count = db_delete('workflow_node_history')->condition('sid', $sid)->execute();
      if ($count) {
        $this
          ->messenger()
          ->addStatus($this
          ->t('@count history records for the "@state" state have been deleted.', [
          '@state' => $state_name,
          '@count' => $count,
        ]));
      }
      $state
        ->delete();
      $this
        ->messenger()
        ->addStatus($this
        ->t('The "@state" state has been deleted.', [
        '@state' => $state_name,
      ]));
    }
  }
}