summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Wehner2011-01-06 00:37:05 (GMT)
committer Daniel Wehner2011-01-06 00:37:05 (GMT)
commit27a708ad6185cad80b08b44416afca86ef9287d2 (patch)
tree3b9d6d0ff82cb32b2c2a2db998e6cc998fcdbdc8
parent999bb688bea1ded683096f62a62853951d50a056 (diff)
#1006176 by das-peter, bojanz: Add support for field based translation.
-rw-r--r--CHANGELOG.txt1
-rw-r--r--modules/field/views_handler_field_field.inc40
-rw-r--r--modules/locale/views_handler_filter_locale_language.inc7
-rw-r--r--modules/translation/views_handler_filter_node_language.inc7
-rw-r--r--plugins/views_plugin_query_default.inc38
-rw-r--r--views.module7
6 files changed, 85 insertions, 15 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 7c9a728..1959076 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -618,3 +618,4 @@ Views 3.x-7.x-dev
o #955464 by dereine: Fix glossary and many to one mode handler.
o #1016430 by bojanz: fix two small bugs in field_field handler.
o by dereine: Add $view->version to all default views.
+ o #1006176 by das-peter, bojanz: Add support for field based translation.
diff --git a/modules/field/views_handler_field_field.inc b/modules/field/views_handler_field_field.inc
index d2ac0e4..6621bb9 100644
--- a/modules/field/views_handler_field_field.inc
+++ b/modules/field/views_handler_field_field.inc
@@ -75,8 +75,10 @@ class views_handler_field_field extends views_handler_field {
// Get the entity type according to the base table of the field.
// Then add it to the query as a formula. That way we can avoid joining
// the field table if all we need is entity_id and entity_type.
- $entity_type = $this->definition['entity_tables'][$base_table];
- $this->aliases['entity_type'] = $this->query->add_field(NULL, "'$entity_type'", $this->table_alias . '_entity_type');
+ $this->entity_type = $entity_type = $this->definition['entity_tables'][$base_table];
+ // The alias needs to be unique, so we use both the field table and the entity type.
+ $entity_type_alias = $this->definition['table'] . '_' . $entity_type . '_entity_type';
+ $this->aliases['entity_type'] = $this->query->add_field(NULL, "'$entity_type'", $entity_type_alias);
$fields = $this->additional_fields;
@@ -100,6 +102,13 @@ class views_handler_field_field extends views_handler_field {
if ($use_groupby || !empty($this->definition['add fields to query'])) {
$this->ensure_my_table();
$this->add_additional_fields($fields);
+
+ // Filter by language, if field translation is enabled.
+ $field = $this->definition['field_info'];
+ if (field_is_translatable($entity_type, $field)) {
+ $column = $this->table_alias . ".language";
+ $this->query->add_where(0, $column, $this->query->options['field_language']);
+ }
}
// The revision id inhibits grouping.
@@ -124,7 +133,7 @@ class views_handler_field_field extends views_handler_field {
function option_definition() {
$options = parent::option_definition();
- $field = field_info_field($this->definition['field_name']);
+ $field = $this->definition['field_info'];
$field_type = field_info_field_types($field['type']);
$column_names = array_keys($field['columns']);
@@ -151,7 +160,7 @@ class views_handler_field_field extends views_handler_field {
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
- $field = field_info_field($this->definition['field_name']);
+ $field = $this->definition['field_info'];
$formatters = _field_view_formatter_options($field['type']);
$column_names = array_keys($field['columns']);
@@ -299,11 +308,32 @@ class views_handler_field_field extends views_handler_field {
'label' => 'hidden',
);
- $render_array = field_view_field($entity_type, $entity, $this->definition['field_name'], $display);
+ $langcode = $this->field_language();
+ $render_array = field_view_field($entity_type, $entity, $this->definition['field_name'], $display, $langcode);
return drupal_render($render_array);
}
else {
return '';
}
}
+
+ /**
+ * Return the language code of the language the field should be displayed in,
+ * according to the settings.
+ */
+ function field_language() {
+ global $language_content;
+
+ if (field_is_translatable($this->entity_type, $this->definition['field_info'])) {
+ $default_language = language_default('language');
+ $language = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
+ array($language_content->language, $default_language),
+ $this->query->options['field_language']);
+
+ return $language;
+ }
+ else {
+ return LANGUAGE_NONE;
+ }
+ }
}
diff --git a/modules/locale/views_handler_filter_locale_language.inc b/modules/locale/views_handler_filter_locale_language.inc
index 678197d..6407ba6 100644
--- a/modules/locale/views_handler_filter_locale_language.inc
+++ b/modules/locale/views_handler_filter_locale_language.inc
@@ -8,10 +8,13 @@ class views_handler_filter_locale_language extends views_handler_filter_in_opera
function get_value_options() {
if (!isset($this->value_options)) {
$this->value_title = t('Language');
- $languages = array('***CURRENT_LANGUAGE***' => t("Current user's language"), '***DEFAULT_LANGUAGE***' => t("Default site language"), '***NO_LANGUAGE***' => t('No language'));
+ $languages = array(
+ '***CURRENT_LANGUAGE***' => t("Current user's language"),
+ '***DEFAULT_LANGUAGE***' => t("Default site language"),
+ LANGUAGE_NONE => t('No language')
+ );
$languages = array_merge($languages, locale_language_list());
$this->value_options = $languages;
}
}
}
-
diff --git a/modules/translation/views_handler_filter_node_language.inc b/modules/translation/views_handler_filter_node_language.inc
index 64ac921..01de09c 100644
--- a/modules/translation/views_handler_filter_node_language.inc
+++ b/modules/translation/views_handler_filter_node_language.inc
@@ -7,10 +7,13 @@ class views_handler_filter_node_language extends views_handler_filter_in_operato
function get_value_options() {
if (!isset($this->value_options)) {
$this->value_title = t('Language');
- $languages = array('***CURRENT_LANGUAGE***' => t("Current user's language"), '***DEFAULT_LANGUAGE***' => t("Default site language"), '***NO_LANGUAGE***' => t('No language'));
+ $languages = array(
+ '***CURRENT_LANGUAGE***' => t("Current user's language"),
+ '***DEFAULT_LANGUAGE***' => t("Default site language"),
+ LANGUAGE_NONE => t('No language')
+ );
$languages = array_merge($languages, locale_language_list());
$this->value_options = $languages;
}
}
}
-
diff --git a/plugins/views_plugin_query_default.inc b/plugins/views_plugin_query_default.inc
index ba8931f..7658944 100644
--- a/plugins/views_plugin_query_default.inc
+++ b/plugins/views_plugin_query_default.inc
@@ -170,6 +170,9 @@ class views_plugin_query_default extends views_plugin_query {
'default' => FALSE,
'bool' => TRUE,
);
+ $options['field_language'] = array(
+ 'default' => '***CURRENT_LANGUAGE***',
+ );
return $options;
}
@@ -197,6 +200,37 @@ class views_plugin_query_default extends views_plugin_query {
'#description' => t('This will make the query attempt to connect to a slave server if available. If no slave server is defined or available, it will fall back to the default server.'),
'#default_value' => !empty($this->options['slave']),
);
+
+ $entities = entity_get_info();
+ $entity_tables = array();
+ $has_translation_handlers = FALSE;
+ foreach ($entities as $type => $entity_info) {
+ $entity_tables[] = $entity_info['base table'];
+
+ if (!empty($entity_info['translation'])) {
+ $has_translation_handlers = TRUE;
+ }
+ }
+
+ // Doesn't make sense to show a field setting here if we aren't querying
+ // an entity base table. Also, we make sure that there's at least one
+ // entity type with a translation handler attached.
+ if (in_array($this->base_table, $entity_tables) && $has_translation_handlers) {
+ $languages = array(
+ '***CURRENT_LANGUAGE***' => t("Current user's language"),
+ '***DEFAULT_LANGUAGE***' => t("Default site language"),
+ LANGUAGE_NONE => t('No language')
+ );
+ $languages = array_merge($languages, locale_language_list());
+
+ $form['field_language'] = array(
+ '#type' => 'select',
+ '#title' => t('Field Language'),
+ '#description' => t('All fields which support translations will be displayed in the selected language.'),
+ '#options' => $languages,
+ '#default_value' => $this->options['field_language'],
+ );
+ }
}
// ----------------------------------------------------------------
@@ -780,7 +814,7 @@ class views_plugin_query_default extends views_plugin_query {
* The caller is reponsible for ensuring that all fields are fully qualified
* (TABLE.FIELD) and that the table already exists in the query.
* Internally the dbtng method "where" is used.
- *
+ *
* @param $group
* The WHERE group to add these to; groups are used to create AND/OR
* sections. Groups cannot be nested. Use 0 as the default group.
@@ -814,7 +848,7 @@ class views_plugin_query_default extends views_plugin_query {
/**
* Add a simple HAVING clause to the query.
- *
+ *
* The caller is responsible for ensuring that all fields are fully qualified
* (TABLE.FIELD) and that the table and an appropriate GROUP BY already exist in the query.
* Internally the dbtng method "havingCondition" is used.
diff --git a/views.module b/views.module
index 7b131a7..3412399 100644
--- a/views.module
+++ b/views.module
@@ -133,7 +133,7 @@ function views_theme($existing, $type, $theme, $path) {
* The existing cache of theme hooks to test against.
* @param $path
* The path to search.
- *
+ *
* @see drupal_find_theme_templates
*/
function _views_find_module_templates($cache, $path) {
@@ -1290,13 +1290,12 @@ function views_theme_functions($hook, $view, $display = NULL) {
* Substitute current time; this works with cached queries.
*/
function views_views_query_substitutions($view) {
- global $language;
+ global $language_content;
return array(
'***CURRENT_VERSION***' => VERSION,
'***CURRENT_TIME***' => REQUEST_TIME,
- '***CURRENT_LANGUAGE***' => $language->language,
+ '***CURRENT_LANGUAGE***' => $language_content->language,
'***DEFAULT_LANGUAGE***' => language_default('language'),
- '***NO_LANGUAGE***' => '',
);
}