You are here

public function ContentTranslationRedirectForm::save in Content Translation Redirect 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

src/Form/ContentTranslationRedirectForm.php, line 79

Class

ContentTranslationRedirectForm
Form handler for the Content Translation Redirect add and edit forms.

Namespace

Drupal\content_translation_redirect\Form

Code

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

  /** @var \Drupal\content_translation_redirect\ContentTranslationRedirectInterface $entity */
  $entity = $this->entity;

  // Set the label on new entity.
  if ($entity
    ->isNew()) {
    $entity_id = $form_state
      ->getValue('id');
    list($entity_type_id, $bundle_id) = explode('__', $entity_id);

    // Get the entity label.
    $entity_label = (string) $this->entityTypeManager
      ->getDefinition($entity_type_id)
      ->getLabel();

    // Get the bundle label.
    $bundle_info = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type_id);
    $entity_label .= ': ' . $bundle_info[$bundle_id]['label'];

    // Set the label to the config entity.
    $entity
      ->set('label', $entity_label);
  }

  // Set code and message.
  $entity
    ->setStatusCode($form_state
    ->getValue('code'));
  $entity
    ->setMessage($form_state
    ->getValue('message'));

  // Save the entity.
  $status = $entity
    ->save();
  if ($status == SAVED_NEW) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Created the %label Content Translation Redirect.', [
      '%label' => $entity
        ->label(),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Saved the %label Content Translation Redirect.', [
      '%label' => $entity
        ->label(),
    ]));
  }
  $form_state
    ->setRedirect('entity.content_translation_redirect.collection');
}