Skip to content

Commit

Permalink
Implemented language selection for taxonomy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Reyero committed Feb 12, 2011
1 parent 8c79dcd commit a0dfaff
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion i18n_select/i18n_select.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function i18n_select_admin_settings() {
);
$form['selection']['i18n_select_taxonomy'] = array(
'#type' => 'checkbox',
'#title' => t('Taxonomy (not implemented)'),
'#title' => t('Taxonomy'),
'#default_value' => variable_get('i18n_select_taxonomy', TRUE),
);

Expand Down
61 changes: 49 additions & 12 deletions i18n_select/i18n_select.module
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php
// $Id: i18n_select.module,v 1.1.2.1 2010/11/09 10:31:32 brucepearson Exp $

/**
* @file
* Multilingual content selection module.
*
* Alters content queries to add language conditions.
*
* Queries tagged with 'i18n_select' or that already have a language condition will not be altered.
*/
/**
* Implementation of hook_menu().
*/
Expand All @@ -24,36 +31,33 @@ function i18n_select_menu() {
* Rewrite node queries so language selection options are enforced.
*/
function i18n_select_query_node_access_alter(QueryAlterableInterface $query) {
if (variable_get('i18n_select_nodes', TRUE) && ($table_alias = i18n_select_check_table($query, 'node'))) {
if (variable_get('i18n_select_nodes', TRUE) && ($table_alias = i18n_select_check_table($query, 'node')) && i18n_select_check_query($query, $table_alias)) {
// if the language field is present we don't want to do any filtering.
$fields = $query->getFields();
dpm($fields);
dpm($query->conditions());
if (isset($fields['language'])) {
return;
}

$language = i18n_select_language();
$default_lang = language_default('language');

if (variable_get('i18n_select_missing_translation', FALSE) && $language != $default_lang) {

if (variable_get('i18n_select_missing_translation', FALSE) && $language != $default_lang) {
// we want to include nodes from the default language that haven't been
// translated to the current language

// SELECT * FROM node n
// LEFT JOIN node i18n ON n.tnid > 0 AND n.tnid = i18n.tnid AND i18n.language = 'es'
// WHERE n.language='es' OR (n.language='en' AND i18n.nid IS NULL)

// Make sure the conditions refer to the node table. eg using 'n.promote' instead of 'promote'
i18n_select_check_conditions($query, $table_alias);

$query->leftjoin('node', 'i18n', 'n.tnid > 0 AND n.tnid = i18n.tnid AND i18n.language = :lang', array(':lang' => $language));
$query->where($table_alias . '.language=:lang OR (' . $table_alias . '.language=:default_lang AND i18n.nid IS NULL)', array(':lang' => $language, ':default_lang' => $default_lang));

} else {
$query->where($table_alias . '.language=:lang', array(':lang' => $language));
}
// Mark query as altered
$query->addTag('i18n_select');
}
}

Expand All @@ -63,13 +67,17 @@ function i18n_select_query_node_access_alter(QueryAlterableInterface $query) {
* Rewrite taxonomy term queries so language selection options are enforced.
*/
function i18n_select_query_term_access_alter(QueryAlterableInterface $query) {
if (variable_get('i18n_select_taxonomy', TRUE) && ($table_alias = i18n_select_check_table($query, 'taxonomy_term_data'))) {
$query->condition($table_alias . '.language', i18n_select_language());
if (variable_get('i18n_select_taxonomy', TRUE) && ($table_alias = i18n_select_check_table($query, 'taxonomy_term_data')) && i18n_select_check_query($query, $table_alias)) {
$query->condition($table_alias . '.language', i18n_select_langcodes());
// Mark query as altered
$query->addTag('i18n_select');
}
}

/**
* Check table exists in query and get alias for it
*
* @todo Should we add the table if not there?
*/
function i18n_select_check_table($query, $table_name) {
foreach ($query->getTables() as $table) {
Expand All @@ -94,8 +102,37 @@ function i18n_select_check_conditions($query, $table_alias) {
}

/**
* Get language code for content selection
* Check whether we should apply language conditions here:
* - The query has not been tagged with 'i18n_select'
* - The query doesn't include a language field for selection ??
* - The query doesn't have already a language condition
*/
function i18n_select_check_query($query, $table_alias, $field_name = 'language') {
if (!$query->hasTag('i18n_select')) {
$fields = $query->getFields();
$table_field = $table_alias . '.' . $field_name;
if (isset($fields[$field_name]) || isset($fields[$table_field])) {
return FALSE;
}
foreach ($query->conditions() as $condition) {
if (is_array($condition) && isset($condition['field']) && ($condition['field'] == $field_name || $condition['field'] == $table_field)) {
return FALSE;
}
}
return TRUE;
}
}

/**
* Get main language code for content selection
*/
function i18n_select_language() {
return $GLOBALS['language_content']->language;
}

/**
* Get language codes for content selection to use in query conditions
*/
function i18n_select_langcodes() {
return array(i18n_select_language(), LANGUAGE_NONE);
}

0 comments on commit a0dfaff

Please sign in to comment.