public function LocalTaskItemForm::saveAsComplete

Form submit callback for save as completed submit action.

Change items to needs review state and task to completed status.

File

translators/tmgmt_local/src/Form/LocalTaskItemForm.php, line 344

Class

LocalTaskItemForm
Form controller for the localTaskItem edit forms.

Namespace

Drupal\tmgmt_local\Form

Code

public function saveAsComplete(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\tmgmt_local\Entity\LocalTask $task */
  $task = $this->entity
    ->getTask();

  /** @var LocalTaskItem $task_item */
  $task_item = $this->entity;
  $task_item
    ->completed();
  $task_item
    ->save();

  // Mark the task as completed if all assigned job items are at needs done.
  $all_done = TRUE;

  /** @var \Drupal\tmgmt_local\Entity\LocalTaskItem $item */
  foreach ($task
    ->getItems() as $item) {
    if (!$item
      ->isCompleted() && !$item
      ->isClosed()) {
      $all_done = FALSE;
      break;
    }
  }
  if ($all_done) {
    $task
      ->setStatus(LocalTaskInterface::STATUS_COMPLETED);

    // If the task is now completed, redirect back to the overview.
    $view = Views::getView('tmgmt_local_task_overview');
    $view
      ->initDisplay();
    $form_state
      ->setRedirect($view
      ->getUrl()
      ->getRouteName());
  }
  else {

    // If there are more task items, redirect back to the task.
    $uri = $task
      ->toUrl();
    $form_state
      ->setRedirect($uri
      ->getRouteName(), $uri
      ->getRouteParameters());
  }

  /** @var \Drupal\tmgmt\Entity\JobItem $job_item */
  $job_item = $this->entity
    ->getJobItem();

  // Add the translations to the job item.
  $job_item
    ->addTranslatedData($this
    ->prepareData($task_item
    ->getData()), [], TMGMT_DATA_ITEM_STATE_TRANSLATED);
  $this
    ->messenger()
    ->addStatus(t('The translation for <a href=:task_item>@task_item_title</a> has been saved as completed.', [
    ':task_item' => $task_item
      ->toUrl()
      ->toString(),
    '@task_item_title' => $task_item
      ->label(),
  ]));
}