You are here

public function DomainLangNegotiationUrlForm::buildForm in Domain Lang 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 NegotiationUrlForm::buildForm

File

src/Form/DomainLangNegotiationUrlForm.php, line 71

Class

DomainLangNegotiationUrlForm
Configure the URL language negotiation method for this site.

Namespace

Drupal\domain_lang\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config($this->languageNegotiationConfig);
  $form = parent::buildForm($form, $form_state);

  // Fill with initial values on first page visit.
  if (!$config
    ->get('url.source')) {
    $config
      ->set('url.source', $this
      ->config('language.negotiation')
      ->get('url.source'));
  }
  if (!$config
    ->get('url.prefixes')) {
    $config
      ->set('url.prefixes', $this
      ->config('language.negotiation')
      ->get('url.prefixes'));
  }
  if (!$config
    ->get('url.domains')) {
    $config
      ->set('url.domains', $this
      ->config('language.negotiation')
      ->get('url.domains'));
  }
  if (isset($form['language_negotiation_url_part'])) {
    $form['language_negotiation_url_part']['#default_value'] = $config
      ->get('url.source');
  }
  $domain = $this->domainLangHandler
    ->getDomainFromUrl();
  $languages = $this->languageManager
    ->getLanguages();
  $prefixes = $config
    ->get('url.prefixes');
  $domains = $config
    ->get('url.domains');
  foreach ($languages as $langcode => $language) {
    if (isset($form['prefix'], $form['prefix'][$langcode])) {
      $form['prefix'][$langcode]['#default_value'] = isset($prefixes[$langcode]) ? $prefixes[$langcode] : '';
      $form['prefix'][$langcode]['#field_prefix'] = $domain
        ->getPath();
    }
    if (isset($form['domain'], $form['domain'][$langcode])) {
      $form['domain'][$langcode]['#default_value'] = isset($domains[$langcode]) ? $domains[$langcode] : '';
    }
  }
  return $form;
}