You are here

public function DomainAliasForm::form in Domain Access 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

domain_alias/src/DomainAliasForm.php, line 93

Class

DomainAliasForm
Base form controller for domain alias edit forms.

Namespace

Drupal\domain_alias

Code

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

  /** @var \Drupal\domain_alias\DomainAliasInterface $alias */
  $alias = $this->entity;
  $form['domain_id'] = [
    '#type' => 'value',
    '#value' => $alias
      ->getDomainId(),
  ];
  $form['pattern'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Pattern'),
    '#size' => 40,
    '#maxlength' => 80,
    '#default_value' => $alias
      ->getPattern(),
    '#description' => $this
      ->t('The matching pattern for this alias.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $alias
      ->id(),
    '#machine_name' => [
      'source' => [
        'pattern',
      ],
      'exists' => '\\Drupal\\domain_alias\\Entity\\DomainAlias::load',
    ],
  ];
  $form['redirect'] = [
    '#type' => 'select',
    '#options' => $this
      ->redirectOptions(),
    '#default_value' => $alias
      ->getRedirect(),
    '#description' => $this
      ->t('Set an optional redirect directive when this alias is invoked.'),
  ];
  $environments = $this
    ->environmentOptions();
  $form['environment'] = [
    '#type' => 'select',
    '#options' => $environments,
    '#default_value' => $alias
      ->getEnvironment(),
    '#description' => $this
      ->t('Map the alias to a development environment.'),
  ];
  $form['environment_help'] = [
    '#type' => 'details',
    '#open' => FALSE,
    '#collapsed' => TRUE,
    '#title' => $this
      ->t('Environment list'),
    '#description' => $this
      ->t('The table below shows the registered aliases for each environment.'),
  ];
  $domains = $this->domainStorage
    ->loadMultipleSorted();
  $rows = [];
  foreach ($domains as $domain) {

    // If the user cannot edit the domain, then don't show in the list.
    $access = $this->accessHandler
      ->checkAccess($domain, 'update');
    if ($access
      ->isForbidden()) {
      continue;
    }
    $row = [];
    $row[] = $domain
      ->label();
    foreach ($environments as $environment) {
      $match_output = [];
      if ($environment == 'default') {
        $match_output[] = $domain
          ->getCanonical();
      }
      $matches = $this->aliasStorage
        ->loadByEnvironmentMatch($domain, $environment);
      foreach ($matches as $match) {
        $match_output[] = $match
          ->getPattern();
      }
      $output = [
        '#items' => $match_output,
        '#theme' => 'item_list',
      ];
      $row[] = \Drupal::service('renderer')
        ->render($output);
    }
    $rows[] = $row;
  }
  $form['environment_help']['table'] = [
    '#type' => 'table',
    '#header' => array_merge([
      $this
        ->t('Domain'),
    ], $environments),
    '#rows' => $rows,
  ];
  return parent::form($form, $form_state);
}