You are here

public function DomainForm::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/src/DomainForm.php, line 78

Class

DomainForm
Base form for domain edit forms.

Namespace

Drupal\domain

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\domain\Entity\Domain $domain */
  $domain = $this->entity;

  // Create defaults if this is the first domain.
  $count_existing = $this->domainStorage
    ->getQuery()
    ->count()
    ->execute();
  if (!$count_existing) {
    $domain
      ->addProperty('hostname', $this->domainStorage
      ->createHostname());
    $domain
      ->addProperty('name', $this
      ->config('system.site')
      ->get('name'));
  }
  $form['domain_id'] = [
    '#type' => 'value',
    '#value' => $domain
      ->getDomainId(),
  ];
  $form['hostname'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Hostname'),
    '#size' => 40,
    '#maxlength' => 80,
    '#default_value' => $domain
      ->getCanonical(),
    '#description' => $this
      ->t('The canonical hostname, using the full <em>subdomain.example.com</em> format. Leave off the http:// and the trailing slash and do not include any paths.<br />If this domain uses a custom http(s) port, you should specify it here, e.g.: <em>subdomain.example.com:1234</em><br />The hostname may contain only lowercase alphanumeric characters, dots, dashes, and a colon (if using alternative ports).'),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => !empty($domain
      ->id()) ? $domain
      ->id() : '',
    '#disabled' => !empty($domain
      ->id()),
    '#machine_name' => [
      'source' => [
        'hostname',
      ],
      'exists' => [
        $this->domainStorage,
        'load',
      ],
    ],
  ];
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#size' => 40,
    '#maxlength' => 80,
    '#default_value' => $domain
      ->label(),
    '#description' => $this
      ->t('The human-readable name is shown in domain lists and may be used as the title tag.'),
  ];

  // Do not use the :// suffix when storing data.
  $add_suffix = FALSE;
  $form['scheme'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Domain URL scheme'),
    '#options' => [
      'http' => 'http://',
      'https' => 'https://',
      'variable' => 'Variable',
    ],
    '#default_value' => $domain
      ->getRawScheme(),
    '#description' => $this
      ->t('This URL scheme will be used when writing links and redirects to this domain and its resources. Selecting <strong>Variable</strong> will inherit the current scheme of the web request.'),
  ];
  $form['status'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Domain status'),
    '#options' => [
      1 => $this
        ->t('Active'),
      0 => $this
        ->t('Inactive'),
    ],
    '#default_value' => (int) $domain
      ->status(),
    '#description' => $this
      ->t('"Inactive" domains are only accessible to user roles with that assigned permission.'),
  ];
  $form['weight'] = [
    '#type' => 'weight',
    '#title' => $this
      ->t('Weight'),
    '#delta' => $count_existing + 1,
    '#default_value' => $domain
      ->getWeight(),
    '#description' => $this
      ->t('The sort order for this record. Lower values display first.'),
  ];
  $form['is_default'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Default domain'),
    '#default_value' => $domain
      ->isDefault(),
    '#description' => $this
      ->t('If a URL request fails to match a domain record, the settings for this domain will be used. Only one domain can be default.'),
  ];
  $form['validate_url'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Test server response'),
    '#default_value' => TRUE,
    '#description' => $this
      ->t('Validate that  url of the host is accessible to Drupal before saving.'),
  ];
  $required = $this->validator
    ->getRequiredFields();
  foreach ($form as $key => $element) {
    if (in_array($key, $required)) {
      $form[$key]['#required'] = TRUE;
    }
  }
  return $form;
}