Commit 2103890d authored by webchick's avatar webchick

Issue #258785 by Désiré, Schnitzel, kbasarab, pkiraly, desbeers, Bojhan,...

Issue #258785 by Désiré, Schnitzel, kbasarab, pkiraly, desbeers, Bojhan, voxpelli, clemens.tolboom, Artusamak, Gábor Hojtsy, dvinegla: Added Provide more flexible settings for initial language on content types.
parent 142778af
......@@ -1836,12 +1836,13 @@ function comment_form($form, &$form_state, Comment $comment) {
}
$form['node_type'] = array('#type' => 'value', '#value' => 'comment_node_' . $node->type);
// If a content type has multilingual support we set the comment to inherit the
// content language. Otherwise mark the comment as language neutral.
// Make the comment inherit the node language unless specifically set.
$comment_langcode = $comment->langcode;
if (($comment_langcode == LANGUAGE_NOT_SPECIFIED) && variable_get('node_type_language_' . $node->type, 0)) {
if ($comment_langcode == LANGUAGE_NOT_SPECIFIED) {
$comment_langcode = $language_content->langcode;
}
// Uses the language of the content as comment language.
$form['langcode'] = array(
'#type' => 'value',
'#value' => $comment_langcode,
......
......@@ -43,7 +43,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' => TRANSLATION_ENABLED);
$edit = array('node_type_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')), t('Article content type has been updated.'));
}
......
......@@ -38,7 +38,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' => 1);
$edit = array('node_type_language_hidden' => FALSE);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
// Enable content language negotiation UI.
......
......@@ -82,9 +82,9 @@ function testContentTypeLanguageConfiguration() {
// Set "Basic page" content type to use multilingual support.
$this->drupalGet('admin/structure/types/manage/page');
$this->assertText(t('Multilingual support'), t('Multilingual support fieldset present on content type configuration form.'));
$this->assertText(t('Language settings'), t('Multilingual support fieldset present on content type configuration form.'));
$edit = array(
'node_type_language' => 1,
'node_type_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.'));
......@@ -152,7 +152,7 @@ function testContentTypeDirLang() {
// Set "Article" content type to use multilingual support.
$this->drupalGet('admin/structure/types/manage/article');
$edit = array(
'node_type_language' => 1,
'node_type_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.'));
......
......@@ -52,11 +52,9 @@ function testUninstallProcess() {
$this->assertEqual(drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode, $this->langcode, t('Current language: %lang', array('%lang' => drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode)));
// Enable multilingual workflow option for articles.
variable_set('node_type_language_article', 1);
variable_set('node_type_language_hidden_article',FALSE);
// Change JavaScript translations directory.
variable_set('locale_js_directory', 'js_translations');
// Build the JavaScript translation file for French.
$user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
$this->drupalLogin($user);
......
......@@ -185,11 +185,37 @@ 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')) {
$form['workflow']['node_type_language'] = array(
$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'),
'#collapsible' => TRUE,
'#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('Multilingual support'),
'#default_value' => variable_get('node_type_language_' . $type->type, 0),
'#description' => t('Add a language selection field to the editing form, allowing you to select from one of the <a href="!languages">enabled languages</a>. If disabled, new posts are saved with the default language. Existing content will not be affected by changing this option.', array('!languages' => url('admin/config/regional/language'))),
'#title' => t('Hide language selector'),
'#default_value' => variable_get('node_type_language_hidden_' . $type->type, TRUE),
);
}
$form['display'] = array(
......
......@@ -21,6 +21,17 @@ Drupal.behaviors.contentTypes = {
}
return vals.join(', ');
});
$('fieldset#edit-language', context).drupalSetSummary(function(context) {
var vals = [];
vals.push($(".form-item-node-type-language-default select option:selected", context).text());
$('input:checked', context).next('label').each(function() {
vals.push(Drupal.checkPlain($(this).text()));
});
return vals.join(', ');
});
$context.find('fieldset#edit-display').drupalSetSummary(function(context) {
var vals = [];
var $context = $(context);
......
......@@ -44,7 +44,7 @@ function setUp() {
// Set "Basic page" content type to use multilingual support.
$edit = array(
'node_type_language' => 1,
'node_type_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.'));
......@@ -60,7 +60,7 @@ function setUp() {
*/
function testMultilingualNodeForm() {
// Create "Basic page" content.
$langcode = LANGUAGE_NOT_SPECIFIED;
$langcode = node_type_get_default_langcode('page');
$title_key = "title";
$title_value = $this->randomName(8);
$body_key = "body[$langcode][0][value]";
......@@ -70,7 +70,6 @@ function testMultilingualNodeForm() {
$edit = array();
$edit[$title_key] = $title_value;
$edit[$body_key] = $body_value;
$edit['langcode'] = 'en';
$this->drupalPost('node/add/page', $edit, t('Save'));
// Check that the node exists in the database.
......@@ -109,7 +108,7 @@ function testMultilingualNodeForm() {
*/
function testMultilingualDisplaySettings() {
// Create "Basic page" content.
$langcode = LANGUAGE_NOT_SPECIFIED;
$langcode = node_type_get_default_langcode('page');
$title_key = "title";
$title_value = $this->randomName(8);
$body_key = "body[$langcode][0][value]";
......@@ -119,7 +118,6 @@ function testMultilingualDisplaySettings() {
$edit = array();
$edit[$title_key] = $title_value;
$edit[$body_key] = $body_value;
$edit['langcode'] = 'en';
$this->drupalPost('node/add/page', $edit, t('Save'));
// Check that the node exists in the database.
......
<?php
/**
* @file
* Definition of Drupal\node\Tests\NodeTypeInitalLanguageTest.
*/
namespace Drupal\node\Tests;
/**
* Tests related to node type initial language.
*/
class NodeTypeInitialLanguageTest extends NodeTestBase {
public static function getInfo() {
return array(
'name' => 'Node type initial language',
'description' => 'Tests node type initial language settings.',
'group' => 'Node',
);
}
function setUp() {
parent::setUp(array('language'));
$web_user = $this->drupalCreateUser(array('bypass node access', 'administer content types', 'administer languages'));
$this->drupalLogin($web_user);
}
/**
* Tests the node type initial language defaults, and modify them.
*
* The default initial language must be the site's default, and the language
* locked option must be on.
*/
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->drupalGet('node/add/article');
$this->assertNoField('langcode', 'Language is not selectable on node add/edit page by default.');
// Adds a new language and set it as default.
$edit = array(
'predefined_langcode' => 'hu',
);
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
$edit = array(
'site_default' => 'hu',
);
$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
$edit = array(
'node_type_language_hidden' => FALSE,
);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
$this->drupalGet('node/add/article');
$this->assertField('langcode', 'Language is selectable on node add/edit page when language not hidden.');
$this->assertOptionSelected('edit-langcode', 'hu', 'The inital language is the site default on the node add page after the site default language is changed.');
// Changes the inital language settings.
$edit = array(
'node_type_language_default' => 'en',
);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
$this->drupalGet('node/add/article');
$this->assertOptionSelected('edit-langcode', 'en', 'The inital language is the defined language.');
}
}
......@@ -458,7 +458,9 @@ function node_uninstall() {
->condition('name', 'node_options_' . $type)
->condition('name', 'node_submitted_' . $type)
->condition('name', 'node_permissions_' . $type)
->condition('name', 'node_type_language_' . $type)
->condition('name', 'node_type_language_default_' . $type)
->condition('name', 'node_type_language_hidden_' . $type)
->condition('name', 'node_type_language_translation_enabled_' . $type)
)
->execute();
}
......@@ -520,9 +522,9 @@ function _update_7000_node_get_types() {
function node_update_8001() {
$types = db_query('SELECT type FROM {node_type}')->fetchCol();
foreach ($types as $type) {
$language = variable_get('language_content_type_' . $type);
if (isset($language)) {
variable_set('node_type_language_' . $type, $language);
$node_type_language = variable_get('language_content_type_' . $type);
if (isset($node_type_language)) {
variable_set('node_type_language_' . $type, $node_type_language);
}
variable_del('language_content_type_' . $type);
}
......@@ -543,6 +545,29 @@ function node_update_8002() {
}
/**
* @} End of "addtogroup updates-7.x-to-8.x".
* Rename node type language variable names.
*/
function node_update_8003() {
$types = db_query('SELECT type FROM {node_type}')->fetchCol();
foreach ($types as $type) {
variable_set('node_type_language_default_' . $type, LANGUAGE_NOT_SPECIFIED);
$node_type_language = variable_get('node_type_language_' . $type, 0);
if ($node_type_language == 0) {
variable_set('node_type_language_hidden_' . $type, TRUE);
}
if ($node_type_language == 2) {
// Translation was enabled, so enable it again and
// unhide the language selector. Because if language is
// LANGUAGE_NOT_SPECIFIED and the selector hidden, translation
// cannot be enabled.
variable_set('node_type_language_hidden_' . $type, FALSE);
variable_set('node_type_language_translation_enabled_' . $type, TRUE);
}
variable_del('node_type_language_' . $type);
}
}
/**
* @} End of "addtogroup updates-7.x-to-8.x"
* The next series of updates should start at 9000.
*/
......@@ -633,6 +633,47 @@ function node_field_extra_fields() {
return $extra;
}
/**
* Get the default language for a node type.
*
* @param string $node_type
* The type of node.
*
* @return string
* The language code of the node type's default langcode.
*/
function node_type_get_default_langcode($node_type) {
$default_value = variable_get('node_type_language_default_' . $node_type, 'site_default');
$language_interface = drupal_container()->get(LANGUAGE_TYPE_INTERFACE);
if ($default_value == LANGUAGE_NOT_SPECIFIED) {
return LANGUAGE_NOT_SPECIFIED;
}
switch ($default_value) {
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;
}
return $default_value;
}
/**
* Deletes a node type from the database.
*
......
......@@ -91,7 +91,7 @@ function node_add($type) {
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => $type,
'langcode' => LANGUAGE_NOT_SPECIFIED,
'langcode' => node_type_get_default_langcode($type)
));
drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
$output = drupal_get_form($type . '_node_form', $node);
......@@ -186,7 +186,7 @@ function node_form($form, &$form_state, Node $node) {
// @todo D8: Remove. Modules should access the node using $form_state['node'].
$form['#node'] = $node;
if (variable_get('node_type_language_' . $node->type, 0) && module_exists('language')) {
if (module_exists('language')) {
$languages = language_list(LANGUAGE_ALL);
$language_options = array();
foreach ($languages as $langcode => $language) {
......@@ -196,16 +196,15 @@ function node_form($form, &$form_state, Node $node) {
$form['langcode'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#default_value' => (isset($node->langcode) ? $node->langcode : LANGUAGE_NOT_SPECIFIED),
'#default_value' => $node->langcode,
'#options' => $language_options,
'#access' => !variable_get('node_type_language_hidden_' . $node->type, TRUE),
);
}
else {
$form['langcode'] = array(
'#type' => 'value',
// New nodes without multilingual support get the default language, old
// nodes keep their language if language.module is not available.
'#value' => !isset($form['#node']->nid) ? language_default()->langcode : $node->langcode,
'#value' => $node->langcode,
);
}
......
......@@ -42,7 +42,8 @@ function setUp() {
*/
function testAliasTranslation() {
// Set 'page' content type to enable translation.
variable_set('node_type_language_page', TRANSLATION_ENABLED);
variable_set('node_type_language_hidden_page', FALSE);
variable_set('node_type_language_translation_enabled_page', TRUE);
$english_node = $this->drupalCreateNode(array('type' => 'page'));
$english_alias = $this->randomName();
......
......@@ -50,8 +50,7 @@ function testPollTranslate() {
// Set "Poll" content type to use multilingual support with translation.
$this->drupalGet('admin/structure/types/manage/poll');
$edit = array();
$edit['node_type_language'] = TRANSLATION_ENABLED;
$edit = array('node_type_language_hidden' => FALSE, 'node_type_language_translation_enabled' => TRUE);
$this->drupalPost('admin/structure/types/manage/poll', $edit, t('Save content type'));
$this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Poll')), t('Poll content type has been updated.'));
......
......@@ -75,12 +75,11 @@ public function testLanguageUpgrade() {
$this->assertRaw('<td>Chuvash</td>', 'There is a Chuvash translation of the node.');
$this->assertLinkByHref("node/$translation_nid", 0, 'The translation table links to the Chuvash translation.');
$this->assertRaw('<td>Catalan</td><td>n/a</td><td>Not translated</td>', 'There is no Catalan translation of this node.');
// Check for node content type settings upgrade.
$this->drupalGet('node/add/article');
$this->assertFieldByName('langcode');
$this->assertField('langcode', 'There is a language selector.');
$this->drupalGet('node/add/page');
$this->assertNoFieldByName('langcode');
$this->assertNoField('langcode', 'There is no language selector.');
// Check that the user language value was retained in both langcode and
// preferred_langcode.
......
......@@ -42,8 +42,7 @@ function setUp() {
// Set "Basic page" content type to use multilingual support with
// translation.
$this->drupalGet('admin/structure/types/manage/page');
$edit = array();
$edit['node_type_language'] = TRANSLATION_ENABLED;
$edit = array('node_type_language_hidden' => FALSE, 'node_type_language_translation_enabled' => TRUE);
$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.'));
......@@ -222,7 +221,7 @@ function testLanguageSwitcherBlockIntegration() {
// Disable translation support to check that the language switcher is left
// untouched only for new nodes.
$this->drupalLogin($this->admin_user);
$edit = array('node_type_language' => 0);
$edit = array('node_type_language_hidden' => TRUE, 'node_type_language_translation_enabled' => FALSE);
$this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
$this->drupalLogin($this->translator);
......
......@@ -4,10 +4,10 @@
Drupal.behaviors.TranslationEnable = {
attach: function (context) {
$('#edit-node-type-language-default, #edit-node-type-language-locked', context).change(function(context) {
$('#edit-node-type-language-default, #edit-node-type-language-hidden', context).change(function(context) {
var default_language = $('#edit-node-type-language-default').val();
if ((default_language == 'und' || default_language == 'zxx' || default_language == 'mul') && $('#edit-node-type-language-locked').attr('checked')) {
if ((default_language == 'und' || default_language == 'zxx' || default_language == 'mul') && $('#edit-node-type-language-hidden').attr('checked')) {
$('.form-item-node-type-language-translation-enabled').hide();
$('#edit-node-type-language-translation-enabled').removeAttr('checked');
} else {
......
......@@ -21,11 +21,6 @@
use Drupal\node\Node;
/**
* Identifies a content type which has translation support enabled.
*/
const TRANSLATION_ENABLED = 2;
/**
* Implements hook_help().
*/
......@@ -119,11 +114,34 @@ function translation_permission() {
* Implements hook_form_FORM_ID_alter() for node_type_form().
*/
function translation_form_node_type_form_alter(&$form, &$form_state) {
// Hide form element if default language is a constant.
// TODO: When form #states allows OR values change this to use form #states.
$form['#attached']['js'] = array(
drupal_get_path('module', 'translation') . '/translation.js',
);
// Add translation option to content type form.
$form['workflow']['node_type_language']['#type'] = 'radios';
$form['workflow']['node_type_language']['#options'] = array(t('Disabled'), t('Enabled'), TRANSLATION_ENABLED => t('Enabled, with translation'));
// Description based on text from node.module.
$form['workflow']['node_type_language']['#description'] = t('Add a language selection field to the editing form, allowing you to select from one of the <a href="@languages">enabled languages</a>. You can also turn on translation for this content type, which lets you have content translated to any of the installed languages. If disabled, new posts are saved with the default language. Existing content will not be affected by changing this option.', array('@languages' => url('admin/config/regional/language')));
$form['language']['node_type_language_translation_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable translation'),
'#default_value' => variable_get('node_type_language_translation_enabled_' . $form['#node_type']->type, FALSE),
'#element_validate' => array('translation_node_type_language_translation_enabled_validate'),
'#prefix' => "<label class='form-item-node-type-language-translation-enabled'>" . t('Translation') . "</label>",
);
}
/**
* Checks if translation can be enabled.
*
* If language is set to one of the special languages
* and language selector is not hidden, translation cannot be enabled.
*/
function translation_node_type_language_translation_enabled_validate($element, &$form_state, $form) {
if (language_is_locked($form_state['values']['node_type_language_default']) && $form_state['values']['node_type_language_hidden'] && $form_state['values']['node_type_language_translation_enabled']) {
foreach (language_locked_languages() as $language) {
$locked_languages[] = $language->name;
}
form_set_error('node_type_language_translation_enabled', t('Translation is not supported if language is always one of: @locked_languages', array('@locked_languages' => implode(", ", $locked_languages))));
}
}
/**
......@@ -144,9 +162,9 @@ function translation_form_node_form_alter(&$form, &$form_state) {
$form['langcode']['#disabled'] = TRUE;
}
elseif (!empty($node->nid) && !empty($node->tnid)) {
// Disable languages for existing translations, so it is not possible to switch this
// node to some language which is already in the translation set. Also remove the
// language neutral option.
// Disable languages for existing translations, so it is not possible
// to switch this node to some language which is already in the
// translation set. Also remove the language neutral option.
unset($form['langcode']['#options'][LANGUAGE_NOT_SPECIFIED]);
foreach (translation_node_get_translations($node->tnid) as $langcode => $translation) {
if ($translation->nid != $node->nid) {
......@@ -165,7 +183,7 @@ function translation_form_node_form_alter(&$form, &$form_state) {
'#weight' => 30,
);
if ($node->tnid == $node->nid) {
// This is the source node of the translation
// This is the source node of the translation.
$form['translation']['retranslate'] = array(
'#type' => 'checkbox',
'#title' => t('Flag translations as outdated'),
......@@ -366,7 +384,7 @@ function translation_node_validate(Node $node, $form) {
if (translation_supported_type($node->type) && (!empty($node->tnid) || !empty($form['#node']->translation_source->nid))) {
$tnid = !empty($node->tnid) ? $node->tnid : $form['#node']->translation_source->nid;
$translations = translation_node_get_translations($tnid);
if (isset($translations[$node->langcode]) && $translations[$node->langcode]->nid != $node->nid ) {
if (isset($translations[$node->langcode]) && $translations[$node->langcode]->nid != $node->nid) {
form_set_error('langcode', t('There is already a translation in this language.'));
}
}
......@@ -460,7 +478,7 @@ function translation_node_get_translations($tnid) {
* TRUE if translation is supported, and FALSE if not.
*/
function translation_supported_type($type) {
return variable_get('node_type_language_' . $type, 0) == TRANSLATION_ENABLED;
return variable_get('node_type_language_translation_enabled_' . $type, FALSE);
}
/**
......
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