public NegotiationUrlForm::buildForm(array $form, FormStateInterface $form_state)
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 65
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'] = array(
'#title' => $this->t('Part of the URL that determines language'),
'#type' => 'radios',
'#options' => array(
LanguageNegotiationUrl::CONFIG_PATH_PREFIX => $this->t('Path prefix'),
LanguageNegotiationUrl::CONFIG_DOMAIN => $this->t('Domain'),
),
'#default_value' => $config->get('url.source'),
);
$form['prefix'] = array(
'#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' => array(
'visible' => array(
':input[name="language_negotiation_url_part"]' => array(
'value' => (string) LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
),
),
),
);
$form['domain'] = array(
'#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' => array(
'visible' => array(
':input[name="language_negotiation_url_part"]' => array(
'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 = array('%language' => $language->getName(), '%langcode' => $language->getId());
$form['prefix'][$langcode] = array(
'#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] = array(
'#type' => 'textfield',
'#title' => $this->t('%language (%langcode) domain', array('%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);
}
Please login to continue.