You are here

public function EntityCloneForm::buildForm in Entity Clone 8

Form constructor.

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

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/EntityCloneForm.php, line 151

Class

EntityCloneForm
Implements an entity Clone form.

Namespace

Drupal\entity_clone\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if ($this->entity && $this->entityTypeDefinition
    ->hasHandlerClass('entity_clone')) {

    /** @var \Drupal\entity_clone\EntityClone\EntityCloneFormInterface $entity_clone_handler */
    if ($this->entityTypeManager
      ->hasHandler($this->entityTypeDefinition
      ->id(), 'entity_clone_form')) {
      $entity_clone_form_handler = $this->entityTypeManager
        ->getHandler($this->entityTypeDefinition
        ->id(), 'entity_clone_form');
      $form = array_merge($form, $entity_clone_form_handler
        ->formElement($this->entity));
    }
    $entityType = $this
      ->getEntity()
      ->getEntityTypeId();
    if ($this->serviceProvider
      ->entityTypeHasOwnerTrait($this
      ->getEntity()
      ->getEntityType()) && $this->currentUser
      ->hasPermission('take_ownership_on_clone ' . $entityType . ' entity')) {
      $form['take_ownership'] = [
        '#type' => 'checkbox',
        '#title' => $this->stringTranslationManager
          ->translate('Take ownership'),
        '#default_value' => $this->entityCloneSettingsManager
          ->getTakeOwnershipSetting(),
        '#description' => $this->stringTranslationManager
          ->translate('Take ownership of the newly created cloned entity.'),
      ];
    }
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['clone'] = [
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this->stringTranslationManager
        ->translate('Clone'),
    ];
    $form['actions']['abort'] = [
      '#type' => 'submit',
      '#value' => $this->stringTranslationManager
        ->translate('Cancel'),
      '#submit' => [
        '::cancelForm',
      ],
    ];
  }
  return $form;
}