Skip to content

Commit

Permalink
Issue backdrop-contrib#8: Get rid of variable module dependency, prev…
Browse files Browse the repository at this point in the history
…ent other errors on install
  • Loading branch information
indigoxela committed Mar 4, 2020
1 parent f0c5c7e commit 8b261d2
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 121 deletions.
2 changes: 1 addition & 1 deletion i18n.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
56 changes: 56 additions & 0 deletions i18n_node/i18n_node.admin.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* @file
* The i18n_node module admin forms.
*/

/**
* Additonal content option settings.
*/
function i18n_node_content_options_form($form, &$form_state) {
$form['i18n_hide_translation_links'] = array(
'#type' => '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;
}
1 change: 0 additions & 1 deletion i18n_node/i18n_node.info
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ backdrop = 1.x
type = module
configure = admin/config/regional/i18n/node
files[]=i18n_node.test
files[]=i18n_node.variable.inc
114 changes: 80 additions & 34 deletions i18n_node/i18n_node.module
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 ;
}

Expand All @@ -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')) {
Expand All @@ -204,26 +208,36 @@ 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;
}

/**
* Get language mode for node or node type
*/
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;
}
}
}

Expand Down Expand Up @@ -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),
);
}
}
Expand Down Expand Up @@ -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'] .= ' <em>' . t('These will be available only when you enable Multilingual support in Publishing options above.') . '</em>';
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
Expand Down Expand Up @@ -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'])) {
Expand Down
12 changes: 6 additions & 6 deletions i18n_node/i18n_node.pages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
79 changes: 0 additions & 79 deletions i18n_node/i18n_node.variable.inc

This file was deleted.

0 comments on commit 8b261d2

Please sign in to comment.