You are here

public function EntityCloneForm::submitForm in Entity Clone 8

Form submission handler.

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

File

src/Form/EntityCloneForm.php, line 194

Class

EntityCloneForm
Implements an entity Clone form.

Namespace

Drupal\entity_clone\Form

Code

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

  /** @var \Drupal\entity_clone\EntityClone\EntityCloneInterface $entity_clone_handler */
  $entity_clone_handler = $this->entityTypeManager
    ->getHandler($this->entityTypeDefinition
    ->id(), 'entity_clone');
  if ($this->entityTypeManager
    ->hasHandler($this->entityTypeDefinition
    ->id(), 'entity_clone_form')) {
    $entity_clone_form_handler = $this->entityTypeManager
      ->getHandler($this->entityTypeDefinition
      ->id(), 'entity_clone_form');
  }
  $properties = [];
  if (isset($entity_clone_form_handler) && $entity_clone_form_handler) {
    $properties = $entity_clone_form_handler
      ->getValues($form_state);
  }
  $duplicate = $this->entity
    ->createDuplicate();
  $this->eventDispatcher
    ->dispatch(EntityCloneEvents::PRE_CLONE, new EntityCloneEvent($this->entity, $duplicate, $properties));
  $cloned_entity = $entity_clone_handler
    ->cloneEntity($this->entity, $duplicate, $properties);
  $this->eventDispatcher
    ->dispatch(EntityCloneEvents::POST_CLONE, new EntityCloneEvent($this->entity, $duplicate, $properties));
  $this->messenger
    ->addMessage($this->stringTranslationManager
    ->translate('The entity <em>@entity (@entity_id)</em> of type <em>@type</em> was cloned.', [
    '@entity' => $this->entity
      ->label(),
    '@entity_id' => $this->entity
      ->id(),
    '@type' => $this->entity
      ->getEntityTypeId(),
  ]));
  $this
    ->formSetRedirect($form_state, $cloned_entity);
}