You are here

public function DeploymentForm::buildForm in Build Hooks 8.2

Same name and namespace in other branches
  1. 3.x src/Form/DeploymentForm.php \Drupal\build_hooks\Form\DeploymentForm::buildForm()

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/DeploymentForm.php, line 105

Class

DeploymentForm
Class DeploymentForm.

Namespace

Drupal\build_hooks\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, FrontendEnvironment $frontend_environment = NULL) {

  // TODO: render this with some theme hook instead of html tags.
  $form['display'] = [
    '#markup' => '<h2>' . $this
      ->t('@envName Environment', [
      '@envName' => $frontend_environment
        ->label(),
    ]) . '</h2>',
  ];
  $form['environment_link'] = [
    '#markup' => $this
      ->t('Frontend @environmentName site url: @link', [
      '@link' => Link::fromTextAndUrl($frontend_environment
        ->getUrl(), Url::fromUri($frontend_environment
        ->getUrl(), [
        'attributes' => [
          'target' => '_blank',
        ],
      ]))
        ->toString(),
      '@environmentName' => $frontend_environment
        ->label(),
    ]),
  ];

  // When was the last deployment?
  $last_deployment_timestamp = $this->buildHooksDeploylogger
    ->getLastDeployTimeForEnvironment($frontend_environment);
  if ($last_deployment_timestamp) {
    $last_deployment_timestamp_formatted = $this->dateFormatter
      ->format($last_deployment_timestamp, 'long');
    $form['last_deployment'] = [
      '#markup' => '<p>' . $this
        ->t('Last deployment triggered on: <strong>@date</strong>', [
        '@date' => $last_deployment_timestamp_formatted,
      ]) . '</p>',
    ];
  }
  $form['changelog'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Changelog'),
    '#description' => $this
      ->t("This is a summary of the changes since the previous deployment to the <strong>%name</strong> environment:", [
      '%name' => $frontend_environment
        ->label(),
    ]) . '</p>',
    '#open' => TRUE,
  ];

  // Have we logged any changes since last deployment?
  if ($this->buildHooksDeploylogger
    ->getNumberOfItemsSinceLastDeploymentForEnvironment($frontend_environment) > 0) {
    try {
      $form['changelog']['log'] = $this
        ->getChangelogView($frontend_environment);
    } catch (\Exception $e) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Could not preview the deployment. Check configuration.'));
    }
  }
  else {
    $form['changelog']['#description'] = '<p>' . $this
      ->t('No changes recorded since the last deployment for this environment. If needed you can still trigger a deployment using this page.') . '</p>';
  }

  // Add the entity to the form:
  $form['frontend_environment'] = [
    '#type' => 'value',
    '#value' => $frontend_environment,
  ];

  // Plugins have a possibility to return additional elements for this form:

  /** @var \Drupal\build_hooks\Plugin\FrontendEnvironmentBase $plugin */
  $plugin = $frontend_environment
    ->getPlugin();
  $additionalFormElements = $plugin
    ->getAdditionalDeployFormElements($form_state);

  // If they do, merge their form elements into the form:
  if (!empty($additionalFormElements)) {
    $form = NestedArray::mergeDeep($form, $additionalFormElements);
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Start a new deployment to the @environment environment', [
      '@environment' => $frontend_environment
        ->label(),
    ]),
  ];
  return $form;
}