You are here

public function PreviewLinkForm::buildForm in Preview Link 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 EntityForm::buildForm

File

src/Form/PreviewLinkForm.php, line 97

Class

PreviewLinkForm
Preview link form.

Namespace

Drupal\preview_link\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $expiration = $this->entity
    ->getGeneratedTimestamp() + $this->linkExpiry
    ->getLifetime() - $this->time
    ->getRequestTime();
  $description = $this
    ->t('Generate a preview link for the <em>@entity_label</em> entity. Preview links will expire @lifetime after they were created.', [
    '@entity_label' => $this
      ->getRelatedEntity()
      ->label(),
    '@lifetime' => $this->dateFormatter
      ->formatInterval($this->linkExpiry
      ->getLifetime(), 1),
  ]);
  $form['preview_link'] = [
    '#theme' => 'preview_link',
    '#title' => $this
      ->t('Preview link'),
    '#description' => $description,
    '#remaining_lifetime' => $this->dateFormatter
      ->formatInterval($expiration),
    '#link' => $this->entity
      ->getUrl()
      ->setAbsolute()
      ->toString(),
  ];
  $form['actions']['submit']['#value'] = $this
    ->t('Regenerate preview link');
  $form['actions']['reset'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Reset lifetime'),
    '#submit' => [
      '::resetLifetime',
      '::save',
    ],
    '#weight' => 100,
  ];
  return $form;
}