Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented language selection for taxonomy

  • Loading branch information...
commit a0dfaff516dd4f8b6a2950bc5c67cbe379b4bbef 1 parent 8c79dcd
Jose Reyero authored
Showing with 50 additions and 13 deletions.
  1. +1 −1  i18n_select/i18n_select.admin.inc
  2. +49 −12 i18n_select/i18n_select.module
View
2  i18n_select/i18n_select.admin.inc
@@ -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),
);
View
61 i18n_select/i18n_select.module
@@ -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().
*/
@@ -24,11 +31,9 @@ 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;
}
@@ -36,24 +41,23 @@ function i18n_select_query_node_access_alter(QueryAlterableInterface $query) {
$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');
}
}
@@ -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) {
@@ -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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.