You are here

public function NegotiationUrlForm::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/language/src/Form/NegotiationUrlForm.php \Drupal\language\Form\NegotiationUrlForm::buildForm()
  2. 10 core/modules/language/src/Form/NegotiationUrlForm.php \Drupal\language\Form\NegotiationUrlForm::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 ConfigFormBase::buildForm

File

core/modules/language/src/Form/NegotiationUrlForm.php, line 68

Class

NegotiationUrlForm
Configure the URL language negotiation method for this site.

Namespace

Drupal\language\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  global $base_url;
  $config = $this
    ->config('language.negotiation');
  $form['language_negotiation_url_part'] = [
    '#title' => $this
      ->t('Part of the URL that determines language'),
    '#type' => 'radios',
    '#options' => [
      LanguageNegotiationUrl::CONFIG_PATH_PREFIX => $this
        ->t('Path prefix'),
      LanguageNegotiationUrl::CONFIG_DOMAIN => $this
        ->t('Domain'),
    ],
    '#default_value' => $config
      ->get('url.source'),
  ];
  $form['prefix'] = [
    '#type' => 'details',
    '#tree' => TRUE,
    '#title' => $this
      ->t('Path prefix configuration'),
    '#open' => TRUE,
    '#description' => $this
      ->t('Language codes or other custom text to use as a path prefix for URL language detection. For the selected fallback language, this value may be left blank. <strong>Modifying this value may break existing URLs. Use with caution in a production environment.</strong> Example: Specifying "deutsch" as the path prefix code for German results in URLs like "example.com/deutsch/contact".'),
    '#states' => [
      'visible' => [
        ':input[name="language_negotiation_url_part"]' => [
          'value' => (string) LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
        ],
      ],
    ],
  ];
  $form['domain'] = [
    '#type' => 'details',
    '#tree' => TRUE,
    '#title' => $this
      ->t('Domain configuration'),
    '#open' => TRUE,
    '#description' => $this
      ->t('The domain names to use for these languages. <strong>Modifying this value may break existing URLs. Use with caution in a production environment.</strong> Example: Specifying "de.example.com" as language domain for German will result in a URL like "http://de.example.com/contact".'),
    '#states' => [
      'visible' => [
        ':input[name="language_negotiation_url_part"]' => [
          'value' => (string) LanguageNegotiationUrl::CONFIG_DOMAIN,
        ],
      ],
    ],
  ];
  $languages = $this->languageManager
    ->getLanguages();
  $prefixes = $config
    ->get('url.prefixes');
  $domains = $config
    ->get('url.domains');
  foreach ($languages as $langcode => $language) {
    $t_args = [
      '%language' => $language
        ->getName(),
      '%langcode' => $language
        ->getId(),
    ];
    $form['prefix'][$langcode] = [
      '#type' => 'textfield',
      '#title' => $language
        ->isDefault() ? $this
        ->t('%language (%langcode) path prefix (Default language)', $t_args) : $this
        ->t('%language (%langcode) path prefix', $t_args),
      '#maxlength' => 64,
      '#default_value' => isset($prefixes[$langcode]) ? $prefixes[$langcode] : '',
      '#field_prefix' => $base_url . '/',
    ];
    $form['domain'][$langcode] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('%language (%langcode) domain', [
        '%language' => $language
          ->getName(),
        '%langcode' => $language
          ->getId(),
      ]),
      '#maxlength' => 128,
      '#default_value' => isset($domains[$langcode]) ? $domains[$langcode] : '',
    ];
  }
  $form_state
    ->setRedirect('language.negotiation');
  return parent::buildForm($form, $form_state);
}