You are here

public function LocalTaskItemForm::save in Translation Management Tool 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

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

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

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

Class

LocalTaskItemForm
Form controller for the localTaskItem edit forms.

Namespace

Drupal\tmgmt_local\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  parent::save($form, $form_state);

  /** @var LocalTaskItem $task_item */
  $task_item = $this->entity;
  $form_state
    ->cleanValues();
  foreach ($form_state
    ->getValues() as $key => $value) {
    if (is_array($value) && isset($value['translation'])) {

      // Update the translation, this will only update the translation in case
      // it has changed. We have two different cases, the first is for nested
      // texts.
      if (is_array($value['translation'])) {
        $update['#translation']['#text'] = $value['translation']['value'];
      }
      else {
        $update['#translation']['#text'] = $value['translation'];
      }
      $task_item
        ->updateData($key, $update);
    }
  }
  $task_item
    ->save();
  if ($form_state
    ->getTriggeringElement()['#value'] == $form['actions']['save']['#value']) {
    $this
      ->messenger()
      ->addStatus(t('The translation for <a href=:task_item>@task_item_title</a> has been saved.', [
      ':task_item' => $task_item
        ->toUrl()
        ->toString(),
      '@task_item_title' => $task_item
        ->label(),
    ]));
  }
  $task = $task_item
    ->getTask();
  $uri = $task
    ->toUrl();
  $form_state
    ->setRedirect($uri
    ->getRouteName(), $uri
    ->getRouteParameters());
}