You are here

public function ReplicateConfirmForm::submitForm in Replicate UI 8

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

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 ContentEntityForm::submitForm

File

src/Form/ReplicateConfirmForm.php, line 90

Class

ReplicateConfirmForm

Namespace

Drupal\replicate_ui\Form

Code

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

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $this
    ->getEntity();
  $label_key = $entity
    ->getEntityType()
    ->getKey('label');
  if ($entity instanceof TranslatableInterface) {
    foreach ($entity
      ->getTranslationLanguages() as $translation_language) {
      $langcode = $translation_language
        ->getId();
      if ($new_label = $form_state
        ->getValue('new_label_' . $langcode)) {

        /** @var \Drupal\Core\Entity\TranslatableInterface $translation */
        $translation = $entity
          ->getTranslation($langcode);
        $translation
          ->set($label_key, $new_label);
      }
    }
  }
  else {
    $new_label = $form_state
      ->getValue('new_label');
    if ($new_label) {
      $entity
        ->set($label_key, $new_label);
    }
  }

  // @todo Decide whether this belongs into the API module instead.
  $entity
    ->setValidationRequired(FALSE);
  $replicated_entity = $this->replicator
    ->replicateEntity($entity);

  // Add the replicated entity to the form state storage, so it can be
  // accessed by other submit callbacks.
  $form_state
    ->set('replicated_entity', $replicated_entity);
  $this
    ->messenger()
    ->addMessage(t('%type (%id) has been replicated to id %new!', [
    '%type' => $entity
      ->getEntityTypeId(),
    '%id' => $entity
      ->id(),
    '%new' => $replicated_entity
      ->id(),
  ]));
  $form_state
    ->setRedirectUrl($replicated_entity
    ->toUrl());
}