Commit b892fe36 authored by webchick's avatar webchick
Browse files

Issue #2082071 by plopesc: Convert language_negotiation_configure_browser_form() to a Controller.

parent 53d757cb
......@@ -489,86 +489,6 @@ function language_negotiation_configure_form_submit($form, &$form_state) {
drupal_set_message(t('Language negotiation configuration saved.'));
}
/**
* Builds the browser language negotiation method configuration form.
*/
function language_negotiation_configure_browser_form($form, &$form_state) {
$form = array();
// Initialize a language list to the ones available, including English.
$languages = language_list();
$existing_languages = array();
foreach ($languages as $langcode => $language) {
$existing_languages[$langcode] = $language->name;
}
// If we have no languages available, present the list of predefined languages
// only. If we do have already added languages, set up two option groups with
// the list of existing and then predefined languages.
if (empty($existing_languages)) {
$language_options = language_admin_predefined_list();
}
else {
$language_options = array(
t('Existing languages') => $existing_languages,
t('Languages not yet added') => language_admin_predefined_list()
);
}
$form['mappings'] = array(
'#tree' => TRUE,
'#theme' => 'language_negotiation_configure_browser_form_table',
);
$mappings = language_get_browser_drupal_langcode_mappings();
foreach ($mappings as $browser_langcode => $drupal_langcode) {
$form['mappings'][$browser_langcode] = array(
'browser_langcode' => array(
'#type' => 'textfield',
'#default_value' => $browser_langcode,
'#size' => 20,
'#required' => TRUE,
),
'drupal_langcode' => array(
'#type' => 'select',
'#options' => $language_options,
'#default_value' => $drupal_langcode,
'#required' => TRUE,
),
);
}
// Add empty row.
$form['new_mapping'] = array(
'#type' => 'details',
'#title' => t('Add a new mapping'),
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$form['new_mapping']['browser_langcode'] = array(
'#type' => 'textfield',
'#title' => t('Browser language code'),
'#description' => t('Use language codes as <a href="@w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')),
'#default_value' => '',
'#size' => 20,
);
$form['new_mapping']['drupal_langcode'] = array(
'#type' => 'select',
'#title' => t('Drupal language'),
'#options' => $language_options,
'#default_value' => '',
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Theme browser configuration form as table.
*
......@@ -620,56 +540,6 @@ function theme_language_negotiation_configure_browser_form_table($variables) {
return $output;
}
/**
* Browser language negotiation form validation.
*/
function language_negotiation_configure_browser_form_validate($form, &$form_state) {
// Array to check if all browser language codes are unique.
$unique_values = array();
// Check all mappings.
$mappings = array();
if (isset($form_state['values']['mappings'])) {
$mappings = $form_state['values']['mappings'];
foreach ($mappings as $key => $data) {
// Make sure browser_langcode is unique.
if (array_key_exists($data['browser_langcode'], $unique_values)) {
form_set_error('mappings][' . $key . '][browser_langcode', t('Browser language codes must be unique.'));
}
elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
form_set_error('mappings][' . $key . '][browser_langcode', t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
}
$unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
}
}
// Check new mapping.
$data = $form_state['values']['new_mapping'];
if (!empty($data['browser_langcode'])) {
// Make sure browser_langcode is unique.
if (array_key_exists($data['browser_langcode'], $unique_values)) {
form_set_error('mappings][' . $key . '][browser_langcode', t('Browser language codes must be unique.'));
}
elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
form_set_error('mappings][' . $key . '][browser_langcode', t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
}
$unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
}
$form_state['mappings'] = $unique_values;
}
/**
* Browser language negotiation form submit.
*/
function language_negotiation_configure_browser_form_submit($form, &$form_state) {
$mappings = $form_state['mappings'];
if (!empty($mappings)) {
language_set_browser_drupal_langcode_mappings($mappings);
}
$form_state['redirect'] = 'admin/config/regional/language/detection';
}
/**
* Returns the content language settings form.
*/
......
......@@ -119,10 +119,7 @@ function language_menu() {
);
$items['admin/config/regional/language/detection/browser'] = array(
'title' => 'Browser language detection configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array('language_negotiation_configure_browser_form'),
'access arguments' => array('administer languages'),
'file' => 'language.admin.inc',
'route_name' => 'language_negotiation_browser',
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
$items['admin/config/regional/language/detection/selected'] = array(
......
......@@ -33,6 +33,14 @@ language_delete:
requirements:
_entity_access: 'language_entity.delete'
language_negotiation_browser:
pattern: '/admin/config/regional/language/detection/browser'
defaults:
_form: '\Drupal\language\Form\NegotiationBrowserForm'
_title: 'Browser language detection configuration'
requirements:
_permission: 'administer languages'
language_negotiation_browser_delete:
pattern: 'admin/config/regional/language/detection/browser/delete/{browser_langcode}'
defaults:
......
<?php
/**
* @file
* Contains \Drupal\language\Form\NegotiationBrowserForm.
*/
namespace Drupal\language\Form;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Config\Context\ContextInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\system\SystemConfigFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configure the browser language negotiation method for this site.
*/
class NegotiationBrowserForm extends SystemConfigFormBase {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* {@inheritdoc}
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler
*/
public function __construct(ConfigFactory $config_factory, ContextInterface $context, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory, $context);
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('config.context.free'),
$container->get('module_handler')
);
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'language_negotiation_configure_browser_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$this->moduleHandler->loadInclude('language', 'inc', 'language.admin');
$form = array();
// Initialize a language list to the ones available, including English.
$languages = language_list();
$existing_languages = array();
foreach ($languages as $langcode => $language) {
$existing_languages[$langcode] = $language->name;
}
// If we have no languages available, present the list of predefined languages
// only. If we do have already added languages, set up two option groups with
// the list of existing and then predefined languages.
if (empty($existing_languages)) {
$language_options = language_admin_predefined_list();
}
else {
$language_options = array(
$this->t('Existing languages') => $existing_languages,
$this->t('Languages not yet added') => language_admin_predefined_list()
);
}
$form['mappings'] = array(
'#tree' => TRUE,
'#theme' => 'language_negotiation_configure_browser_form_table',
);
$mappings = $this->language_get_browser_drupal_langcode_mappings();
foreach ($mappings as $browser_langcode => $drupal_langcode) {
$form['mappings'][$browser_langcode] = array(
'browser_langcode' => array(
'#type' => 'textfield',
'#default_value' => $browser_langcode,
'#size' => 20,
'#required' => TRUE,
),
'drupal_langcode' => array(
'#type' => 'select',
'#options' => $language_options,
'#default_value' => $drupal_langcode,
'#required' => TRUE,
),
);
}
// Add empty row.
$form['new_mapping'] = array(
'#type' => 'details',
'#title' => $this->t('Add a new mapping'),
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$form['new_mapping']['browser_langcode'] = array(
'#type' => 'textfield',
'#title' => $this->t('Browser language code'),
'#description' => $this->t('Use language codes as <a href="@w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')),
'#default_value' => '',
'#size' => 20,
);
$form['new_mapping']['drupal_langcode'] = array(
'#type' => 'select',
'#title' => $this->t('Drupal language'),
'#options' => $language_options,
'#default_value' => '',
);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
// Array to check if all browser language codes are unique.
$unique_values = array();
// Check all mappings.
$mappings = array();
if (isset($form_state['values']['mappings'])) {
$mappings = $form_state['values']['mappings'];
foreach ($mappings as $key => $data) {
// Make sure browser_langcode is unique.
if (array_key_exists($data['browser_langcode'], $unique_values)) {
form_set_error('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes must be unique.'));
}
elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
form_set_error('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
}
$unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
}
}
// Check new mapping.
$data = $form_state['values']['new_mapping'];
if (!empty($data['browser_langcode'])) {
// Make sure browser_langcode is unique.
if (array_key_exists($data['browser_langcode'], $unique_values)) {
form_set_error('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes must be unique.'));
}
elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
form_set_error('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
}
$unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
}
$form_state['mappings'] = $unique_values;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$mappings = $form_state['mappings'];
if (!empty($mappings)) {
$config = $this->configFactory->get('language.mappings');
$config->setData($mappings);
$config->save();
}
$form_state['redirect'] = 'admin/config/regional/language/detection';
parent::submitForm($form, $form_state);
}
/**
* Retrieves the browser's langcode mapping configuration array.
*
* @return array
* The browser's langcode mapping configuration array.
*/
protected function language_get_browser_drupal_langcode_mappings() {
$config = $this->configFactory->get('language.mappings');
if ($config->isNew()) {
return array();
}
return $config->get();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment