diff --git a/i18n.module b/i18n.module index a0714e31..c2f1d443 100644 --- a/i18n.module +++ b/i18n.module @@ -274,7 +274,7 @@ function i18n_language_name($lang) { * Get valid language code for current page or check whether the code is a defined language */ function i18n_langcode($langcode = NULL) { - return $langcode && $langcode !== LANGUAGE_NONE ? $langcode : i18n_language()->language; + return $langcode && $langcode !== LANGUAGE_NONE ? $langcode : i18n_language()->langcode; } /** diff --git a/i18n_node/i18n_node.admin.inc b/i18n_node/i18n_node.admin.inc new file mode 100644 index 00000000..ed1d21dc --- /dev/null +++ b/i18n_node/i18n_node.admin.inc @@ -0,0 +1,56 @@ + 'checkbox', + '#title' => t('Hide content translation links'), + '#description' => t('Hide the links to translations in content body and teasers. If you choose this option, switching language will only be available from the language switcher block.'), + '#default_value' => FALSE,// TODO config_get + ); + $form['i18n_node_default_language_none'] = array( + '#type' => 'select', + '#title' => t('Default language for content types with Multilingual support disabled.'), + '#description' => t('Determines which language will be set for newly created content of types that don\'t have Multilingual support enabled.'), + '#options' => array( + 0 => t('The site\'s default language (Default behaviour).'), + 1 => t('Language neutral (Recommended).'), + ), + '#default_value' => FALSE,// TODO config_get + ); + return $form; +} + +/** + * Form items for settings per content type. + */ +function _i18n_node_content_type_settings() { + $form['i18n_node_options'] = array( + '#type' => 'checkboxes', + '#title' => t('Extended language options'), + '#options' => array( + 'current' => t('Set current language as default for new content.'), + 'required' => t('Require language (Do not allow Language Neutral).'), + 'lock' => t('Lock language (Cannot be changed).'), + ), + '#default_value' => array(),// TODO config_get + ); + $form['i18n_node_extended'] = array( + '#type' => 'radios', + '#title' => t('Extended language support'), + '#options' => array( + I18N_LANGUAGE_ENABLED => t('Normal - All enabled languages will be allowed.'), + I18N_LANGUAGE_EXTENDED => t('Extended - All defined languages will be allowed.'), + I18N_LANGUAGE_EXTENDED | I18N_LANGUAGE_HIDDEN => t('Extended, but not displayed - All defined languages will be allowed for input, but not displayed in links.'), + ), + '#default_value' => I18N_LANGUAGE_ENABLED,// TODO config_get + '#description' => t('If enabled, all defined languages will be allowed for this content type in addition to only enabled ones. This is useful to have more languages for content than for the interface.'), + ); + return $form; +} diff --git a/i18n_node/i18n_node.info b/i18n_node/i18n_node.info index c12abce8..bc2d509c 100644 --- a/i18n_node/i18n_node.info +++ b/i18n_node/i18n_node.info @@ -8,4 +8,3 @@ backdrop = 1.x type = module configure = admin/config/regional/i18n/node files[]=i18n_node.test -files[]=i18n_node.variable.inc diff --git a/i18n_node/i18n_node.module b/i18n_node/i18n_node.module index 6b7f66f6..ec9b630b 100644 --- a/i18n_node/i18n_node.module +++ b/i18n_node/i18n_node.module @@ -31,10 +31,11 @@ function i18n_node_menu() { 'title' => 'Node options', 'description' => 'Configure extended options for multilingual content and translations.', 'page callback' => 'backdrop_get_form', - 'page arguments' => array('variable_group_form', 'i18n_node'), + 'page arguments' => array('i18n_node_content_options_form'), 'access arguments' => array('administer site configuration'), 'type' => MENU_LOCAL_TASK, 'weight' => 10, + 'file' => 'i18n_node.admin.inc', ); $items['i18n/node/autocomplete'] = array( 'page callback' => 'i18n_node_autocomplete', @@ -159,7 +160,7 @@ function i18n_node_menu_alter(&$items) { * Get node language. */ function i18n_node_get_lang($nid, $default = '') { - $lang = db_query('SELECT language FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField(); + $lang = db_query('SELECT langcode FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField(); return $lang ? $lang : $default ; } @@ -180,16 +181,19 @@ function i18n_node_language_list($node, $translate = FALSE, $select = FALSE) { $languages = node_invoke($node, 'language_list', $translate); if (!$languages) { - $languages = i18n_language_list('name', i18n_node_language_mode($node)); + // FIXME @see https://github.com/backdrop-contrib/i18n/issues/9 + //$languages = i18n_language_list('name', i18n_node_language_mode($node)); + $languages = language_list(FALSE, TRUE); + if ($translate && isset($node->tnid) && $node->tnid && ($translations = translation_node_get_translations($node->tnid))) { - unset($translations[$node->language]); + unset($translations[$node->langcode]); foreach (array_keys($translations) as $langcode) { unset($languages[$langcode]); } } // Language may be locked for this node type, restrict options to current one - if ($select && i18n_node_language_options($node, 'lock') && !empty($node->language) && !empty($languages[$node->language])) { - $languages = array($node->language => $languages[$node->language]); + if ($select && i18n_node_language_options($node, 'lock') && !empty($node->langcode) && !empty($languages[$node->langcode])) { + $languages = array($node->langcode => $languages[$node->langcode]); } // Check language required for this type (no language neutral) elseif (!i18n_node_language_options($node, 'required')) { @@ -204,8 +208,11 @@ function i18n_node_language_list($node, $translate = FALSE, $select = FALSE) { * Check options for node language */ function i18n_node_language_options($node, $option) { - $options = config_get('i18n.settings', 'i18n_node_options_' . $node->type); - return in_array($option, $options, TRUE); + $options = config_get('i18n.settings', "i18n_node_type_{$node->type}.options"); + if ($options) { + return in_array($option, $options, TRUE); + } + return FALSE; } /** @@ -213,17 +220,24 @@ function i18n_node_language_options($node, $option) { */ function i18n_node_language_mode($type) { $type = is_object($type) ? $type->type : $type; - return config_get('i18n.settings', 'i18n_node_extended_' . $type); + $mode = config_get('i18n.settings', "i18n_node_type_{$type}.extended"); + if ($mode) { + return $mode; + } + // Default value if nothing has been set yet. + return I18N_LANGUAGE_ENABLED; } /** * Implements hook_node_prepare(). */ function i18n_node_node_prepare($node) { - $options = config_get('i18n.settings', 'i18n_node_options_' . $node->type); - if (i18n_node_type_enabled($node) && empty($node->nid) && !i18n_object_langcode($node) && in_array('current', $options)) { - // Set current language for new nodes if option enabled - $node->language = i18n_language_content()->language; + $options = config_get('i18n.settings', "i18n_node_type_{$node->type}.options"); + if (i18n_node_type_enabled($node) && empty($node->nid) && !i18n_object_langcode($node)) { + if ($options && in_array('current', $options)) { + // Set current language for new nodes if config exists and is enabled. + $node->langcode = i18n_language_content()->langcode; + } } } @@ -254,7 +268,7 @@ function i18n_node_node_view($node) { $node->content['language'] = array( '#type' => 'item', '#title' => t('Language'), - '#markup' => i18n_language_name($node->language), + '#markup' => i18n_language_name($node->langcode), ); } } @@ -400,39 +414,71 @@ function i18n_node_form_search_form_alter(&$form, &$form_state) { */ function i18n_node_form_node_type_form_alter(&$form, &$form_state) { if (isset($form['type'])) { - $disabled = !i18n_node_type_enabled($form['#node_type']); - $form['i18n'] = array( + $form['multilingual']['i18n_node'] = array( '#type' => 'fieldset', '#title' => t('Multilingual settings'), '#collapsible' => TRUE, - '#collapsed' => TRUE, - '#group' => 'additional_settings', '#attributes' => array( 'class' => array('i18n-node-type-settings-form'), ), - '#description' => t('Extended multilingual options provided by Internationalization module.'), - '#disabled' => $disabled, + '#description' => $description = t('Extended multilingual options provided by Internationalization module.'), + '#tree' => TRUE, + '#states' => array( + 'invisible' => array( + ':input[name="language"]' => array('value' => '0'), + ), + ), ); + $form['#submit'][] = 'i18n_node_type_settings_submit'; - // Some settings about node languages. Add variables for node type from variable definition + // Some settings about node languages. Add variables for node type from config. if ($form['#node_type']->type) { - variable_type_include('node_type'); - $form['i18n'] += node_variable_type_subform($form['#node_type']->type, array('i18n_node_options', 'i18n_node_extended')); - } - // Add disabled message - if ($disabled) { - $form['i18n']['#description'] .= ' ' . t('These will be available only when you enable Multilingual support in Publishing options above.') . ''; - foreach (element_children($form['i18n']) as $key) { - $form['i18n'][$key]['#disabled'] = TRUE; - } + $node_type = $form['#node_type']->type; + $config = config_get('i18n.settings', 'i18n_node_type_' . $node_type); + + $form['multilingual']['i18n_node']['options'] = array( + '#type' => 'checkboxes', + '#title' => t('Extended language options'), + '#options' => array( + 'current' => t('Set current language as default for new content.'), + 'required' => t('Require language (Do not allow Language Neutral).'), + 'lock' => t('Lock language (Cannot be changed).'), + ), + '#default_value' => isset($config['options']) ? $config['options'] : array(), + ); + $form['multilingual']['i18n_node']['extended'] = array( + '#type' => 'radios', + '#title' => t('Extended language support'), + '#options' => array( + I18N_LANGUAGE_ENABLED => t('Normal - All enabled languages will be allowed.'), + I18N_LANGUAGE_EXTENDED => t('Extended - All defined languages will be allowed.'), + I18N_LANGUAGE_EXTENDED | I18N_LANGUAGE_HIDDEN => t('Extended, but not displayed - All defined languages will be allowed for input, but not displayed in links.'), + ), + '#default_value' => isset($config['extended']) ? $config['extended'] : I18N_LANGUAGE_ENABLED, + '#description' => t('If enabled, all defined languages will be allowed for this content type in addition to only enabled ones. This is useful to have more languages for content than for the interface.'), + ); } } } +/** + * Custom submit function to handle i18n node settings. + */ +function i18n_node_type_settings_submit($form, $form_state) { + $node_type = $form_state['values']['type']; + + if ($form_state['values']['language'] == FALSE) { + config_clear('i18n.settings', "i18n_node_type_{$node_type}"); + } + else { + config_set('i18n.settings', "i18n_node_type_{$node_type}", $form_state['values']['i18n_node']); + } +} + /** * Implements hook_form_BASE_FORM_ID_alter(). */ -function i18n_node_form_node_form_alter(&$form, $form_state) { +function i18n_node_form_node_form_alter(&$form, &$form_state, $form_id) { $node = $form['#node']; /** * i18n has to override locale.module @@ -501,13 +547,13 @@ function i18n_node_form_submit($form, &$form_state) { function _i18n_node_form_node_form_alter($form, &$form_state) { $node = $form['#node']; if (i18n_node_type_enabled($node)) { - if (!empty($form['language']['#options'])) { - $form['language']['#options'] = i18n_node_language_list($node, TRUE, TRUE); + if (!empty($form['langcode']['#options'])) { + $form['langcode']['#options'] = i18n_node_language_list($node, TRUE, TRUE); } } elseif (config_get('i18n.settings', 'i18n_node_default_language_none') && !isset($form['#node']->nid)) { // Override locale module setting default language to nodes. It is already in form_state. - $form['language']['#value'] = $form_state['values']['language'] = LANGUAGE_NONE; + $form['langcode']['#value'] = $form_state['values']['langcode'] = LANGUAGE_NONE; } // Translate field names for title and body for the node edit form. if (!empty($form['title']['#title'])) { diff --git a/i18n_node/i18n_node.pages.inc b/i18n_node/i18n_node.pages.inc index 725805d9..d06a0955 100644 --- a/i18n_node/i18n_node.pages.inc +++ b/i18n_node/i18n_node.pages.inc @@ -48,7 +48,7 @@ function i18n_node_translation_overview($node) { else { // We have no translation source nid, this could be a new set, emulate that. $tnid = $node->nid; - $translations = array($node->language => $node); + $translations = array($node->langcode => $node); } $type = config_get('translation.settings', 'language_type'); @@ -138,7 +138,7 @@ function i18n_node_select_translation($form, &$form_state, $node, $translations) '#description' => t("Alternatively, you can select existing nodes as translations of this one or remove nodes from this translation set. Only nodes that have the right language and don't belong to other translation set will be available here.") ); foreach (i18n_node_language_list($node) as $langcode => $language_name) { - if ($langcode != $node->language && $langcode != LANGUAGE_NONE) { + if ($langcode != $node->langcode && $langcode != LANGUAGE_NONE) { $nid = isset($translations[$langcode]) ? $translations[$langcode]->nid : 0; $form['translations']['nid'][$langcode] = array( '#type' => 'value', @@ -186,11 +186,11 @@ function i18n_node_select_translation_validate($form, &$form_state) { function i18n_node_select_translation_submit($form, &$form_state) { $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : NULL; $node = $form_state['values']['node']; - $translations = $node->tnid ? translation_node_get_translations($node->tnid) : array($node->language => $node); + $translations = $node->tnid ? translation_node_get_translations($node->tnid) : array($node->langcode => $node); foreach ($translations as $trans) { - $current[$trans->language] = $trans->nid; + $current[$trans->langcode] = $trans->nid; } - $update = array($node->language => $node->nid) + array_filter($form_state['values']['translations']['nid']); + $update = array($node->langcode => $node->nid) + array_filter($form_state['values']['translations']['nid']); // Compute the difference to see which are the new translations and which ones to remove $new = array_diff_assoc($update, $current); $remove = array_diff_assoc($current, $update); @@ -234,7 +234,7 @@ function i18n_node_select_translation_submit($form, &$form_state) { * Node title autocomplete callback */ function i18n_node_autocomplete($type, $language, $string = '') { - $params = array('type' => $type, 'language' => $language, 'tnid' => 0); + $params = array('type' => $type, 'langcode' => $language, 'tnid' => 0); $matches = array(); foreach (_i18n_node_references($string, 'contains', $params) as $id => $row) { // Add a class wrapper for a few required CSS overrides. diff --git a/i18n_node/i18n_node.variable.inc b/i18n_node/i18n_node.variable.inc deleted file mode 100644 index 9362fc3f..00000000 --- a/i18n_node/i18n_node.variable.inc +++ /dev/null @@ -1,79 +0,0 @@ - t('Multilingual node options'), - 'description' => t('Extended node options for multilingual sites.'), - 'access' => 'administer site configuration', - 'path' => 'admin/config/regional/i18n/node', - ); - return $groups; -} - -/** - * Implements hook_variable_info(). - */ -function i18n_node_variable_info($options = array()) { - $variables['i18n_hide_translation_links'] = array( - 'type' => 'boolean', - 'title' => t('Hide content translation links', array(), $options), - 'description' => t('Hide the links to translations in content body and teasers. If you choose this option, switching language will only be available from the language switcher block.', array(), $options), - 'default' => 0, - 'group' => 'i18n_node', - ); - $variables['i18n_node_default_language_none'] = array( - 'title' => t('Default language for content types with Multilingual support disabled.', array(), $options), - 'description' => t('Determines which language will be set for newly created content of types that don\'t have Multilingual support enabled.', array(), $options), - 'type' => 'select', - 'options' => array( - 0 => t('The site\'s default language (Default behaviour).', array(), $options), - 1 => t('Language neutral (Recommended).', array(), $options) - ), - 'default' => 0, - 'group' => 'i18n_node', - ); - $variables['i18n_node_options_[node_type]'] = array( - 'type' => 'multiple', - 'title' => t('Extended language options', array(), $options), - 'repeat' => array( - 'type' => 'options', - 'options' => array( - 'current' => t('Set current language as default for new content.', array(), $options), - 'required' => t('Require language (Do not allow Language Neutral).', array(), $options), - 'lock' => t('Lock language (Cannot be changed).', array(), $options), - ), - ), - 'group' => 'i18n', - ); - $variables['i18n_node_extended_[node_type]'] = array( - 'type' => 'multiple', - 'title' => t('Extended language support'), - 'repeat' => array( - 'type' => 'select', - 'options callback' => 'i18n_node_variable_extended_options', - 'default' => I18N_LANGUAGE_ENABLED, - ), - 'description' => t('If enabled, all defined languages will be allowed for this content type in addition to only enabled ones. This is useful to have more languages for content than for the interface.', array(), $options), - 'group' => 'i18n', - ); - - return $variables; -} - -/** - * Options callback for i18n_node_extended_ - */ -function i18n_node_variable_extended_options($variable, $options) { - return array( - I18N_LANGUAGE_ENABLED => t('Normal - All enabled languages will be allowed.', array(), $options), - I18N_LANGUAGE_EXTENDED => t('Extended - All defined languages will be allowed.', array(), $options), - I18N_LANGUAGE_EXTENDED | I18N_LANGUAGE_HIDDEN => t('Extended, but not displayed - All defined languages will be allowed for input, but not displayed in links.', array(), $options), - ); -}