You are here

public function ReplicateConfirmForm::buildForm in Replicate UI 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 ContentEntityConfirmFormBase::buildForm

File

src/Form/ReplicateConfirmForm.php, line 36

Class

ReplicateConfirmForm

Namespace

Drupal\replicate_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL) {
  $this->routeMatch = $route_match;

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $this->routeMatch
    ->getParameter($this
    ->getEntityTypeId());
  $this
    ->setEntity($entity);

  // Expose a field to allow users to customize the label of the copied
  // entity, defaulting to "{original label} (Copy)".
  if ($entity
    ->getEntityType()
    ->hasKey('label')) {

    // If there are translations, expose one element per language.
    if ($entity instanceof TranslatableInterface) {
      foreach ($entity
        ->getTranslationLanguages() as $translation_language) {
        $langcode = $translation_language
          ->getId();

        /** @var \Drupal\Core\Entity\TranslatableInterface $translation */
        $translation = $entity
          ->getTranslation($langcode);
        $form['new_label_' . $langcode] = [
          '#type' => 'textfield',
          '#title' => $this
            ->t('New label (@language)', [
            '@language' => $translation_language
              ->getName(),
          ]),
          '#description' => $this
            ->t('This text will be used as the label of the new entity being created, in <em>@language</em>.', [
            '@language' => $translation_language
              ->getName(),
          ]),
          '#required' => TRUE,
          '#default_value' => $this
            ->t('@title (Copy)', [
            '@title' => $translation
              ->label(),
          ], [
            'langcode' => $langcode,
          ]),
          '#maxlength' => 255,
        ];
      }
    }
    else {
      $form['new_label'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('New label'),
        '#description' => $this
          ->t('This text will be used as the label of the new entity being created.'),
        '#required' => TRUE,
        '#default_value' => t('@title (Copy)', [
          '@title' => $entity
            ->label(),
        ]),
        '#maxlength' => 255,
      ];
    }
  }
  return parent::buildForm($form, $form_state);
}