Commit 28045a2e authored by webchick's avatar webchick

Issue #1739928 by vasi1186, YesCT, webflo, Transition, plach, Gábor Hojtsy:...

Issue #1739928 by vasi1186, YesCT, webflo, Transition, plach, Gábor Hojtsy: Generalize language configuration on content types to apply to terms and other entities.
parent f63cdc38
......@@ -52,7 +52,7 @@ function setUp() {
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
// Set "Article" content type to use multilingual support.
$edit = array('node_type_language_hidden' => FALSE);
$edit = array('language_configuration[language_hidden]' => FALSE);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
// Enable content language negotiation UI.
......
......@@ -51,7 +51,7 @@ function setUp() {
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
// Set "Article" content type to use multilingual support with translation.
$edit = array('node_type_language_hidden' => FALSE, 'node_type_language_translation_enabled' => TRUE);
$edit = array('language_configuration[language_hidden]' => FALSE, 'node_type_language_translation_enabled' => TRUE);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
$this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Article')), 'Article content type has been updated.');
}
......
......@@ -232,6 +232,222 @@ function language_process_language_select($element) {
return $element;
}
/**
* Implements hook_element_info().
*/
function language_element_info() {
$types['language_configuration'] = array(
'#input' => TRUE,
'#tree' => TRUE,
'#process' => array('language_configuration_element_process'),
);
return $types;
}
/**
* Returns the default options for the language configuration form element.
*
* @return array
* An array containing the default options.
*/
function language_configuration_element_default_options() {
$language_options = array();
$languages = language_list(LANGUAGE_ALL);
foreach ($languages as $langcode => $language) {
$language_options[$langcode] = $language->locked ? t('- @name -', array('@name' => $language->name)) : $language->name;
}
$language_options += array(
'site_default' => t("Site's default language (!language)", array('!language' => language_default()->name)),
'current_interface' => t('Current interface language'),
'authors_default' => t("Author's preferred language"),
);
return $language_options;
}
/**
* Process handler for the language_configuration form element.
*/
function language_configuration_element_process($element, &$form_state, &$form) {
$options = isset($element['#options']) ? $element['#options'] : array();
// Avoid validation failure since we are moving the '#options' key in the
// nested 'language' select element.
unset($element['#options']);
$element['langcode'] = array(
'#type' => 'select',
'#title' => t('Default language'),
'#options' => $options + language_configuration_element_default_options(),
'#description' => t('Explanation of the language options is found on the <a href="@languages_list_page">languages list page</a>.', array('@languages_list_page' => url('admin/config/regional/language'))),
'#default_value' => isset($element['#default_value']['langcode']) ? $element['#default_value']['langcode'] : NULL,
);
$element['language_hidden'] = array(
'#type' => 'checkbox',
'#title' => t('Hide language selection'),
'#default_value' => isset($element['#default_value']['language_hidden']) ? $element['#default_value']['language_hidden'] : NULL,
'#description' => t('Language selector appears on create and edit pages.'),
);
// Add the entity type and bundle information to the form if they are set.
// They will be used, in the submit handler, to generate the names of the
// variables that will store the settings and are a way to uniquely identify
// the entity.
if (!isset($form_state['language'])) {
$form_state['language'] = array();
}
$form_state['language'] += array(
$element['#name'] => array(
'entity_type' => $element['#entity_information']['entity_type'],
'bundle' => $element['#entity_information']['bundle'],
),
);
return $element;
}
/**
* Submit handler for the forms that have a language_configuration element.
*/
function language_configuration_element_submit(&$form, &$form_state) {
// Iterate through all the language_configuration elements and save their
// values.
if (isset($form_state['language'])) {
foreach ($form_state['language'] as $element_name => $values) {
language_save_default_configuration($values['entity_type'], $values['bundle'], $form_state['values'][$element_name]);
}
}
}
/**
* Saves a language configuration that is attached to an entity type and bundle.
*
* @param string $entity_type
* A string representing the entity type.
* @param string $bundle
* A string representing the bundle.
* @param array $values
* An array holding the values to be saved having the following keys:
* - langcode: the language code.
* - language_hidden: if the language element should be hidden or not.
*/
function language_save_default_configuration($entity_type, $bundle, $values = array()) {
config('language.settings')->set(language_get_default_configuration_settings_key($entity_type, $bundle), array('langcode' => $values['langcode'], 'language_hidden' => $values['language_hidden']))->save();
}
/**
* Returns the language configuration stored for an entity type and bundle.
*
* @param string $entity_type
* A string representing the entity type.
* @param string $bundle
* A string representing the bundle.
*
* @return array
* An array with the following keys:
* - langcode: the language code.
* - language_hidden: if the language element is hidden or not.
*/
function language_get_default_configuration($entity_type, $bundle) {
$configuration = config('language.settings')->get(language_get_default_configuration_settings_key($entity_type, $bundle));
if (is_null($configuration)) {
$configuration = array();
}
$configuration += array('langcode' => 'site_default', 'language_hidden' => TRUE);
return $configuration;
}
/**
* Clears the default language configuration for an entity type and bundle.
*
* @param string $entity_type
* A string representing the entity type.
* @param string $bundle
* A string representing the bundle.
*/
function language_clear_default_configuration($entity_type, $bundle) {
config('language.settings')->clear(language_get_default_configuration_settings_key($entity_type, $bundle))->save();
}
/**
* Returns the root name of the variables used to store the configuration.
*
* Based on the entity type and bundle, the variables used to store the
* configuration will have a common root name.
*
* @param string $entity_type
* A string representing the entity type.
* @param string $bundle
* A string representing the bundle.
*
* @return string
* The root name of the variables.
*/
function language_get_default_configuration_settings_key($entity_type, $bundle) {
// Replace all the characters that are not letters, numbers or "_" with "_".
$entity_type = preg_replace('/[^0-9a-zA-Z_]/', "_", $entity_type);
$bundle = preg_replace('/[^0-9a-zA-Z_]/', "_", $bundle);
return $entity_type . '.' . $bundle . '.language.default_configuration';
}
/**
* Implements hook_node_type_update().
*/
function language_node_type_update($info) {
if (!empty($info->old_type) && $info->old_type != $info->type) {
language_save_default_configuration('node', $info->type, language_get_default_configuration('node', $info->old_type));
language_clear_default_configuration('node', $info->old_type);
}
}
/**
* Returns the default language code assigned to an entity type and a bundle.
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The bundle name.
*
* @return string
* The language code.
*/
function language_get_default_langcode($entity_type, $bundle) {
$configuration = language_get_default_configuration($entity_type, $bundle);
if (!isset($configuration['langcode'])) {
$configuration['langcode'] = 'site_default';
}
$default_value = NULL;
$language_interface = language(LANGUAGE_TYPE_INTERFACE);
switch ($configuration['langcode']) {
case 'site_default':
$default_value = language_default()->langcode;
break;
case 'current_interface':
$default_value = $language_interface->langcode;
break;
case 'authors_default':
global $user;
if (!empty($user->preferred_langcode)) {
$default_value = $user->preferred_langcode;
}
else {
$default_value = $language_interface->langcode;
}
break;
}
if ($default_value) {
return $default_value;
}
// If we still do not have a default value, just return the value stored in
// the configuration; it has to be an actual language code.
return $configuration['langcode'];
}
/**
* API function to add or update a language.
*
......@@ -292,6 +508,7 @@ function language_update_count() {
*
* @param $langcode
* Language code of the language to be deleted.
*
* @return
* TRUE if language is successfully deleted. Otherwise FALSE.
*/
......
<?php
/**
* @file
* Definition of Drupal\language\Tests\LanguageConfigurationElementTest.
*/
namespace Drupal\language\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/**
* Functional tests for language configuration's effect on negotiation setup.
*/
class LanguageConfigurationElementTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('language', 'language_elements_test');
public static function getInfo() {
return array(
'name' => 'Language configuration form element tests',
'description' => 'Tests the features of the language configuration element field.',
'group' => 'Language',
);
}
/**
* Tests the language settings have been saved.
*/
public function testLanguageConfigurationElement() {
$this->drupalGet('language-tests/language_configuration_element');
$edit['lang_configuration[langcode]'] = 'current_interface';
$edit['lang_configuration[language_hidden]'] = TRUE;
$this->drupalPost(NULL, $edit, 'Save');
$lang_conf = language_get_default_configuration('some_custom_type', 'some_bundle');
// Check that the settings have been saved.
$this->assertEqual($lang_conf['langcode'], 'current_interface');
$this->assertTrue($lang_conf['language_hidden']);
$this->drupalGet('language-tests/language_configuration_element');
$this->assertOptionSelected('edit-lang-configuration-langcode', 'current_interface');
$this->assertFieldChecked('edit-lang-configuration-language-hidden');
// Reload the page and save again.
$this->drupalGet('language-tests/language_configuration_element');
$edit['lang_configuration[langcode]'] = 'authors_default';
$edit['lang_configuration[language_hidden]'] = FALSE;
$this->drupalPost(NULL, $edit, 'Save');
$lang_conf = language_get_default_configuration('some_custom_type', 'some_bundle');
// Check that the settings have been saved.
$this->assertEqual($lang_conf['langcode'], 'authors_default');
$this->assertFalse($lang_conf['language_hidden']);
$this->drupalGet('language-tests/language_configuration_element');
$this->assertOptionSelected('edit-lang-configuration-langcode', 'authors_default');
$this->assertNoFieldChecked('edit-lang-configuration-language-hidden');
}
/**
* Tests that the language_get_default_langcode() returns the correct values.
*/
public function testDefaultLangcode() {
// Add some custom languages.
foreach (array('aa', 'bb', 'cc') as $language_code) {
$language = new Language(array(
'langcode' => $language_code,
'name' => $this->randomName(),
));
language_save($language);
}
// Fixed language.
language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'bb', 'language_hidden' => FALSE));
$langcode = language_get_default_langcode('custom_type', 'custom_bundle');
$this->assertEqual($langcode, 'bb');
// Current interface.
language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'current_interface', 'language_hidden' => FALSE));
$langcode = language_get_default_langcode('custom_type', 'custom_bundle');
$language_interface = language(LANGUAGE_TYPE_INTERFACE);
$this->assertEqual($langcode, $language_interface->langcode);
// Site's default.
$old_default = language_default();
$old_default->default = FALSE;
language_save($old_default);
$new_default = language_load('cc');
$new_default->default = TRUE;
language_save($new_default);
language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'site_default', 'language_hidden' => FALSE));
$langcode = language_get_default_langcode('custom_type', 'custom_bundle');
$this->assertEqual($langcode, 'cc');
// Check the default value of a language field when authors preferred option
// is selected.
// Create first an user and assign a preferred langcode to him.
$some_user = $this->drupalCreateUser();
$some_user->preferred_langcode = 'bb';
$some_user->save();
$this->drupalLogin($some_user);
language_save_default_configuration('custom_type', 'some_bundle', array('langcode' => 'authors_default', 'language_hidden' => FALSE));
$this->drupalGet('language-tests/language_configuration_element_test');
$this->assertOptionSelected('edit-langcode', 'bb');
}
/**
* Tests that the configuration is updated when the node type is changed.
*/
public function testNodeTypeUpdate() {
// Create the article content type first if the profile used is not the
// standard one.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
}
$admin_user = $this->drupalCreateUser(array('administer content types'));
$this->drupalLogin($admin_user);
$edit = array(
'language_configuration[langcode]' => 'current_interface',
'language_configuration[language_hidden]' => FALSE,
);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
// Check the language default configuration for the articles.
$configuration = language_get_default_configuration('node', 'article');
$this->assertEqual($configuration, array('langcode' => 'current_interface', 'language_hidden' => 0), 'The default language configuration has been saved on the Article content type.');
// Rename the article content type.
$edit = array(
'type' => 'article_2'
);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
// Check that we still have the settings for the new node type.
$configuration = language_get_default_configuration('node', 'article_2');
$this->assertEqual($configuration, array('langcode' => 'current_interface', 'language_hidden' => 0), 'The default language configuration has been kept on the new Article content type.');
}
}
name = "Language form elements test"
description = "Support module for the language form elements tests."
core = 8.x
package = Testing
version = VERSION
hidden = TRUE
<?php
/**
* @file
* Mock module for language form elements test.
*/
/**
* Implements hook_menu().
*/
function language_elements_test_menu() {
$items['language-tests/language_configuration_element'] = array(
'title' => 'Language configuration form element',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'page callback' => 'drupal_get_form',
'page arguments' => array('language_elements_configuration_element'),
);
$items['language-tests/language_configuration_element_test'] = array(
'title' => 'Language configuration form element',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'page callback' => 'drupal_get_form',
'page arguments' => array('language_elements_configuration_element_test'),
);
return $items;
}
/**
* A form containing a language configuration element.
*/
function language_elements_configuration_element() {
$conf = language_get_default_configuration('some_custom_type', 'some_bundle');
$form['lang_configuration'] = array(
'#type' => 'language_configuration',
'#entity_information' => array(
'entity_type' => 'some_custom_type',
'bundle' => 'some_bundle',
),
'#default_value' => $conf,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
$form['#submit'][] = 'language_configuration_element_submit';
return $form;
}
/**
* A form containing a language select element.
*/
function language_elements_configuration_element_test() {
$form['langcode'] = array(
'#type' => 'language_select',
'#default_value' => language_get_default_langcode('custom_type', 'some_bundle'),
);
return $form;
}
......@@ -88,7 +88,7 @@ function testContentTypeLanguageConfiguration() {
$this->drupalGet('admin/structure/types/manage/page');
$this->assertText(t('Language settings'), t('Multilingual support fieldset present on content type configuration form.'));
$edit = array(
'node_type_language_hidden' => FALSE,
'language_configuration[language_hidden]' => FALSE,
);
$this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
$this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.'));
......@@ -156,7 +156,7 @@ function testContentTypeDirLang() {
// Set "Article" content type to use multilingual support.
$this->drupalGet('admin/structure/types/manage/article');
$edit = array(
'node_type_language_hidden' => FALSE,
'language_configuration[language_hidden]' => FALSE,
);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
$this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Article')), t('Article content type has been updated.'));
......
......@@ -63,7 +63,7 @@ function testUninstallProcess() {
$this->assertEqual(language(LANGUAGE_TYPE_INTERFACE)->langcode, $this->langcode, t('Current language: %lang', array('%lang' => language(LANGUAGE_TYPE_INTERFACE)->langcode)));
// Enable multilingual workflow option for articles.
variable_set('node_type_language_hidden_article',FALSE);
language_save_default_configuration('node', 'article', array('langcode' => 'site_default', 'language_hidden' => FALSE));
// Change JavaScript translations directory.
variable_set('locale_js_directory', 'js_translations');
// Build the JavaScript translation file for French.
......
......@@ -206,19 +206,6 @@ function node_type_form($form, &$form_state, $type = NULL) {
'#description' => t('Users with the <em>Administer content</em> permission will be able to override these options.'),
);
if (module_exists('language')) {
$lang_options = array();
$languages = language_list(LANGUAGE_ALL);
foreach ($languages as $langcode => $language) {
$lang_options[$langcode] = $language->locked ? t('- @name -', array('@name' => $language->name)) : $language->name;
}
$lang_options += array(
'site_default' => t("Site's default language"),
'current_interface' => t('Current interface language'),
'authors_default' => t("Author's preferred language"),
);
$form['language'] = array(
'#type' => 'fieldset',
'#title' => t('Language settings'),
......@@ -226,18 +213,17 @@ function node_type_form($form, &$form_state, $type = NULL) {
'#collapsed' => TRUE,
'#group' => 'additional_settings',
);
$form['language']['node_type_language_default'] = array(
'#type' => 'select',
'#title' => t('Default language'),
'#options' => $lang_options,
'#default_value' => variable_get('node_type_language_default_' . $type->type, 'site_default'),
'#description' => t('Explanation of the language options is found on the <a href="@languages_list_page">languages list page</a>.', array('@languages_list_page' => url('admin/config/regional/language'))),
);
$form['language']['node_type_language_hidden'] = array(
'#type' => 'checkbox',
'#title' => t('Hide language selector'),
'#default_value' => variable_get('node_type_language_hidden_' . $type->type, TRUE),
$language_configuration = language_get_default_configuration('node', $type->type);
$form['language']['language_configuration'] = array(
'#type' => 'language_configuration',
'#entity_information' => array(
'entity_type' => 'node',
'bundle' => $type->type,
),
'#default_value' => $language_configuration,
);
$form['#submit'][] = 'language_configuration_element_submit';
}
$form['display'] = array(
'#type' => 'fieldset',
......@@ -293,6 +279,7 @@ function node_type_form($form, &$form_state, $type = NULL) {
);
}
}
$form['#submit'][] = 'node_type_form_submit';
return $form;
}
......
......@@ -24,7 +24,7 @@ Drupal.behaviors.contentTypes = {
$('fieldset#edit-language', context).drupalSetSummary(function(context) {
var vals = [];
vals.push($(".form-item-node-type-language-default select option:selected", context).text());
vals.push($(".form-item-language-configuration-langcode select option:selected", context).text())
$('input:checked', context).next('label').each(function() {
vals.push(Drupal.checkPlain($(this).text()));
......
......@@ -98,12 +98,13 @@ public function form(array $form, array &$form_state, EntityInterface $node) {
$form['title']['#weight'] = -5;
}
$language_configuration = module_invoke('language', 'get_default_configuration', 'node', $node->type);
$form['langcode'] = array(
'#title' => t('Language'),
'#type' => 'language_select',
'#default_value' => $node->langcode,
'#languages' => LANGUAGE_ALL,
'#access' => !variable_get('node_type_language_hidden_' . $node->type, TRUE),
'#access' => isset($language_configuration['language_hidden']) && !$language_configuration['language_hidden'],
);
$form['additional_settings'] = array(
......
......@@ -53,7 +53,7 @@ function setUp() {
// Set "Basic page" content type to use multilingual support.
$edit = array(
'node_type_language_hidden' => FALSE,
'language_configuration[language_hidden]' => FALSE,
);
$this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
$this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), 'Basic page content type has been updated.');
......@@ -69,7 +69,7 @@ function setUp() {
*/
function testMultilingualNodeForm() {
// Create "Basic page" content.
$langcode = node_type_get_default_langcode('page');
$langcode = language_get_default_langcode('node', 'page');
$title_key = "title";
$title_value = $this->randomName(8);
$body_key = "body[$langcode][0][value]";
......@@ -117,7 +117,7 @@ function testMultilingualNodeForm() {
*/
function testMultilingualDisplaySettings() {
// Create "Basic page" content.
$langcode = node_type_get_default_langcode('page');
$langcode = language_get_default_langcode('node', 'page');
$title_key = "title";
$title_value = $this->randomName(8);
$body_key = "body[$langcode][0][value]";
......
......@@ -42,8 +42,8 @@ function setUp() {
*/
function testNodeTypeInitialLanguageDefaults() {
$this->drupalGet('admin/structure/types/manage/article');
$this->assertOptionSelected('edit-node-type-language-default', 'site_default', 'The default inital language is the site default.');
$this->assertFieldChecked('edit-node-type-language-hidden', 'Language selector is hidden by default.');
$this->assertOptionSelected('edit-language-configuration-langcode', 'site_default', 'The default inital language is the site default.');
$this->assertFieldChecked('edit-language-configuration-language-hidden', 'Language selector is hidden by default.');
// Tests if the language field cannot be rearranged on the manage fields tab.
$this->drupalGet('admin/structure/types/manage/article/fields');
......@@ -64,9 +64,9 @@ function testNodeTypeInitialLanguageDefaults() {
$this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
// Tests the initial language after changing the site default language.
// First unhide the language selector
// First unhide the language selector.
$edit = array(
'node_type_language_hidden' => FALSE,
'language_configuration[language_hidden]' => FALSE,
);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
$this->drupalGet('node/add/article');
......@@ -87,7 +87,7 @@ function testNodeTypeInitialLanguageDefaults() {
// Changes the inital language settings.
$edit = array(
'node_type_language_default' => 'en',
'language_configuration[langcode]' => 'en',
);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
$this->drupalGet('node/add/article');
......
......@@ -472,11 +472,10 @@ function node_uninstall() {
->condition('name', 'node_options_' . $type)
->condition('name', 'node_submitted_' . $type)
->condition('name', 'node_permissions_' . $type)
->condition('name', 'node_type_language_default_' . $type)
->condition('name', 'node_type_language_hidden_' . $type)
->condition('name', 'node_type_language_translation_enabled_' . $type)
)
->execute();
config('language.settings')->clear('node. ' . $type . '.language.default_configuration')->save();
}
// Delete node search ranking variables.
......@@ -677,6 +676,21 @@ function node_update_8006(&$sandbox) {
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
}
/**
* Move the language default values to config.
*/
function node_update_8007() {
$types = db_query('SELECT type FROM {node_type}')->fetchCol();
foreach ($types as $type) {
$language_default = update_variable_get('node_type_language_default_' . $type, NULL);
$language_hidden = update_variable_get('node_type_language_hidden_' . $type, NULL);
if (isset($language_default) || isset($language_hidden)) {
$values = array('langcode' => $language_default, 'language_hidden' => $language_hidden);
config('language.settings')->set('node.' . $type . '.language.default_configuration', $values)->save();
}
}
}
/**
* @} End of "addtogroup updates-7.x-to-8.x"
* The next series of updates should start at 9000.
......
......@@ -693,13 +693,16 @@ function node_field_extra_fields() {
// Add also the 'language' select if Language module is enabled and the
// bundle has multilingual support.
// Visibility of the ordering of the language selector is the same as on the
// node/add form, i.e. node_type_language_hidden_TYPE variable.
if ($module_language_enabled && !variable_get('node_type_language_hidden_' . $bundle->type, TRUE)) {
$extra['node'][$bundle->type]['form']['language'] = array(
'label' => t('Language'),
'description' => $description,
'weight' => 0,
);
// node/add form.
if ($module_language_enabled) {
$configuration = language_get_default_configuration('node', $bundle->type);
if (!$configuration['language_hidden']) {
$extra['node'][$bundle->type]['form']['language'] = array(
'label' => t('Language'),
'description' => $description,
'weight' => 0,
);
}
}
$extra['node'][$bundle->type]['display']['language'] = array(
'label' => t('Language'),
......@@ -712,47 +715,6 @@ function node_field_extra_fields() {