Commit 88b40cc6 authored by dawehner's avatar dawehner Committed by tim.plunkett

Issue #1605332 by dawehner, tim.plunkett: Adapt language integration to d8.

parent 472311d5
......@@ -289,7 +289,7 @@ function get_results_key() {
'build_info' => $build_info,
'roles' => array_keys($user->roles),
'super-user' => $user->uid == 1, // special caching for super user.
'language' => language(LANGUAGE_TYPE_INTERFACE)->langcode,
'langcode' => language(LANGUAGE_TYPE_INTERFACE)->langcode,
'base_url' => $GLOBALS['base_url'],
);
foreach (array('exposed_info', 'page', 'sort', 'order', 'items_per_page', 'offset') as $key) {
......@@ -312,7 +312,7 @@ function get_output_key() {
'roles' => array_keys($user->roles),
'super-user' => $user->uid == 1, // special caching for super user.
'theme' => $GLOBALS['theme'],
'language' => language(LANGUAGE_TYPE_INTERFACE)->langcode,
'langcode' => language(LANGUAGE_TYPE_INTERFACE)->langcode,
'base_url' => $GLOBALS['base_url'],
);
......
......@@ -134,6 +134,15 @@ function init(&$view, &$display, $options = NULL) {
$this->unpack_options($this->options, $options);
}
// Convert the field_language and field_language_add_to_query settings.
$field_language = $this->get_option('field_language');
$field_language_add_to_query = $this->get_option('field_language_add_to_query');
if (isset($field_langcode)) {
$this->set_option('field_langcode', $field_language);
$this->set_option('field_langcode_add_to_query', $field_language_add_to_query);
$changed = TRUE;
}
// Mark the view as changed so the user has a chance to save it.
if ($changed) {
$this->view->changed = TRUE;
......@@ -487,11 +496,12 @@ function option_definition() {
'default' => FALSE,
'bool' => TRUE,
),
'field_language' => array(
'field_langcode' => array(
'default' => '***CURRENT_LANGUAGE***',
),
'field_language_add_to_query' => array(
'default' => 1,
'field_langcode_add_to_query' => array(
'default' => TRUE,
'bool' => TRUE,
),
// These types are all plugins that can have individual settings
......@@ -1213,11 +1223,10 @@ function options_summary(&$categories, &$options) {
if (module_exists('language')) {
$languages = array_merge($languages, language_list());
}
$field_language = array();
$options['field_language'] = array(
$options['field_langcode'] = array(
'category' => 'other',
'title' => t('Field Language'),
'value' => $languages[$this->get_option('field_language')],
'value' => $languages[$this->get_option('field_langcode')],
'desc' => t('All fields which support translations will be displayed in the selected language.'),
);
......@@ -1607,17 +1616,17 @@ function options_form(&$form, &$form_state) {
);
$languages = array_merge($languages, views_language_list());
$form['field_language'] = array(
$form['field_langcode'] = 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->get_option('field_language'),
'#default_value' => $this->get_option('field_langcode'),
);
$form['field_language_add_to_query'] = array(
$form['field_langcode_add_to_query'] = array(
'#type' => 'checkbox',
'#title' => t('When needed, add the field language condition to the query'),
'#default_value' => $this->get_option('field_language_add_to_query'),
'#default_value' => $this->get_option('field_langcode_add_to_query'),
);
}
else {
......@@ -2281,8 +2290,8 @@ function options_submit(&$form, &$form_state) {
$this->set_option($section, $form_state['values'][$section]);
break;
case 'field_language':
$this->set_option('field_language', $form_state['values']['field_language']);
$this->set_option('field_language_add_to_query', $form_state['values']['field_language_add_to_query']);
$this->set_option('field_langcode', $form_state['values']['field_langcode']);
$this->set_option('field_langcode_add_to_query', $form_state['values']['field_langcode_add_to_query']);
break;
case 'use_ajax':
case 'hide_attachment_summary':
......
......@@ -223,26 +223,26 @@ function query($use_groupby = FALSE) {
$this->ensure_my_table();
$this->add_additional_fields($fields);
// Filter by language, if field translation is enabled.
// Filter by langcode, if field translation is enabled.
$field = $this->field_info;
if (field_is_translatable($entity_type, $field) && !empty($this->view->display_handler->options['field_language_add_to_query'])) {
$column = $this->table_alias . '.language';
if (field_is_translatable($entity_type, $field) && !empty($this->view->display_handler->options['field_langcode_add_to_query'])) {
$column = $this->table_alias . '.langcode';
// By the same reason as field_language the field might be LANGUAGE_NOT_SPECIFIED in reality so allow it as well.
// @see this::field_language()
$default_language = language_default()->langcode;
$language = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
array(drupal_container()->get(LANGUAGE_TYPE_CONTENT)->langcode, $default_language),
$this->view->display_handler->options['field_language']);
// @see this::field_langcode()
$default_langcode = language_default()->langcode;
$langcode = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
array(drupal_container()->get(LANGUAGE_TYPE_CONTENT)->langcode, $default_langcode),
$this->view->display_handler->options['field_langcode']);
$placeholder = $this->placeholder();
$language_fallback_candidates = array($language);
$langcode_fallback_candidates = array($langcode);
if (variable_get('locale_field_language_fallback', TRUE)) {
require_once DRUPAL_ROOT . '/includes/language.inc';
$language_fallback_candidates = array_merge($language_fallback_candidates, language_fallback_get_candidates());
$langcode_fallback_candidates = array_merge($langcode_fallback_candidates, language_fallback_get_candidates());
}
else {
$language_fallback_candidates[] = LANGUAGE_NOT_SPECIFIED;
$langcode_fallback_candidates[] = LANGUAGE_NOT_SPECIFIED;
}
$this->query->add_where_expression(0, "$column IN($placeholder) OR $column IS NULL", array($placeholder => $language_fallback_candidates));
$this->query->add_where_expression(0, "$column IN($placeholder) OR $column IS NULL", array($placeholder => $langcode_fallback_candidates));
}
}
......@@ -719,7 +719,7 @@ function get_value($values, $field = NULL) {
$entity = clone $values->_field_data[$this->field_alias]['entity'];
$entity_type = $values->_field_data[$this->field_alias]['entity_type'];
$langcode = $this->field_language($entity_type, $entity);
$langcode = $this->field_langcode($entity_type, $entity);
// If we are grouping, copy our group fields into the cloned entity.
// It's possible this will cause some weirdness, but there's only
// so much we can hope to do.
......@@ -840,7 +840,7 @@ function set_items($values, $row_id) {
return array();
}
$langcode = $this->field_language($entity_type, $entity);
$langcode = $this->field_langcode($entity_type, $entity);
$render_array = field_view_field($entity_type, $entity, $this->definition['field_name'], $display, $langcode);
$items = array();
......@@ -906,20 +906,20 @@ function add_self_tokens(&$tokens, $item) {
* Return the language code of the language the field should be displayed in,
* according to the settings.
*/
function field_language($entity_type, $entity) {
function field_langcode($entity_type, $entity) {
if (field_is_translatable($entity_type, $this->field_info)) {
$default_language = language_default()->langcode;
$language = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
array(drupal_container()->get(LANGUAGE_TYPE_CONTENT)->langcode, $default_language),
$default_langcode = language_default()->langcode;
$langcode = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
array(drupal_container()->get(LANGUAGE_TYPE_CONTENT)->langcode, $default_langcode),
$this->view->display_handler->options['field_language']);
// Give the Field Language API a chance to fallback to a different language
// (or LANGUAGE_NOT_SPECIFIED), in case the field has no data for the selected language.
// field_view_field() does this as well, but since the returned language code
// is used before calling it, the fallback needs to happen explicitly.
$language = field_language($entity_type, $entity, $this->field_info['field_name'], $language);
$langcode = field_language($entity_type, $entity, $this->field_info['field_name'], $langcode);
return $language;
return $langcode;
}
else {
return LANGUAGE_NOT_SPECIFIED;
......
......@@ -2,10 +2,10 @@
/**
* @file
* Definition of Views\locale\Plugin\views\argument\Language.
* Definition of Views\language\Plugin\views\argument\Language.
*/
namespace Views\locale\Plugin\views\argument;
namespace Views\language\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Drupal\Core\Annotation\Plugin;
......@@ -16,22 +16,18 @@
* @ingroup views_argument_handlers
*
* @Plugin(
* id = "locale_language",
* module = "locale"
* id = "language",
* module = "language"
* )
*/
class Language extends ArgumentPluginBase {
function construct() {
parent::construct('language');
}
/**
* Override the behavior of summary_name(). Get the user friendly version
* of the language.
*/
function summary_name($data) {
return $this->locale_language($data->{$this->name_alias});
return $this->language($data->{$this->name_alias});
}
/**
......@@ -39,10 +35,10 @@ function summary_name($data) {
* of the language.
*/
function title() {
return $this->locale_language($this->argument);
return $this->language($this->argument);
}
function locale_language($langcode) {
function language($langcode) {
$languages = views_language_list();
return isset($languages[$langcode]) ? $languages[$langcode] : t('Unknown language');
}
......
......@@ -2,10 +2,10 @@
/**
* @file
* Definition of Views\locale\Plugin\views\field\Language.
* Definition of Views\language\Plugin\views\field\Language.
*/
namespace Views\locale\Plugin\views\field;
namespace Views\language\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Core\Annotation\Plugin;
......@@ -16,8 +16,8 @@
* @ingroup views_field_handlers
*
* @Plugin(
* id = "locale_language",
* module = "locale"
* id = "language",
* module = "language"
* )
*/
class Language extends FieldPluginBase {
......@@ -40,9 +40,11 @@ function options_form(&$form, &$form_state) {
}
function render($values) {
$languages = locale_language_list(empty($this->options['native_language']) ? 'name' : 'native');
// @todo: Drupal Core dropped native language until config translation is
// ready, see http://drupal.org/node/1616594.
$value = $this->get_value($values);
return isset($languages[$value]) ? $languages[$value] : '';
$language = language_load($value);
return $language ? $language->name : '';
}
}
......@@ -2,10 +2,10 @@
/**
* @file
* Definition of Views\locale\Plugin\views\field\NodeLanguage.
* Definition of Views\node\Plugin\views\field\Language.
*/
namespace Views\locale\Plugin\views\field;
namespace Views\node\Plugin\views\field;
use Views\node\Plugin\views\field\Node;
use Drupal\Core\Annotation\Plugin;
......@@ -17,10 +17,10 @@
*
* @Plugin(
* id = "node_language",
* module = "locale"
* module = "node"
* )
*/
class NodeLanguage extends Node {
class Language extends Node {
function option_definition() {
$options = parent::option_definition();
......@@ -40,9 +40,11 @@ function options_form(&$form, &$form_state) {
}
function render($values) {
$languages = views_language_list(empty($this->options['native_language']) ? 'name' : 'native');
// @todo: Drupal Core dropped native language until config translation is
// ready, see http://drupal.org/node/1616594.
$value = $this->get_value($values);
$value = isset($languages[$value]) ? $languages[$value] : '';
$language = language_load($value);
$value = $language ? $language->name : '';
return $this->render_link($value, $values);
}
......
......@@ -2,13 +2,13 @@
/**
* @file
* Definition of Views\locale\Plugin\views\filter\Language.
* Definition of Views\language\Plugin\views\filter\Language.
*/
namespace Views\locale\Plugin\views\filter;
namespace Views\language\Plugin\views\filter;
use Drupal\Core\Annotation\Plugin;
use Drupal\views\Plugin\views\filter\InOperator;
use Drupal\Core\Annotation\Plugin;
/**
* Filter by language.
......@@ -16,8 +16,8 @@
* @ingroup views_filter_handlers
*
* @Plugin(
* id = "locale_language",
* module = "locale"
* id = "language",
* module = "language"
* )
*/
class Language extends InOperator {
......
<?php
/**
* @file
* Definition of Views\locale\Plugin\views\filter\NodeLanguage.
*/
namespace Views\locale\Plugin\views\filter;
use Drupal\Core\Annotation\Plugin;
use Drupal\views\Plugin\views\filter\InOperator;
/**
* Filter by language.
*
* @ingroup views_filter_handlers
*
* @Plugin(
* id = "node_language",
* module = "locale"
* )
*/
class NodeLanguage extends InOperator {
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"),
LANGUAGE_NOT_SPECIFIED => t('No language')
);
$languages = array_merge($languages, views_language_list());
$this->value_options = $languages;
}
}
}
<?php
/**
* @file
* Definition of Views\node\Plugin\views\argument\Language.
*/
namespace Views\node\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Drupal\Core\Annotation\Plugin;
/**
* Argument handler to accept a language.
*
* @Plugin(
* id = "node_language",
* module = "node"
* )
*/
class Language extends ArgumentPluginBase {
function construct() {
parent::construct('language');
}
/**
* Override the behavior of summary_name(). Get the user friendly version
* of the language.
*/
function summary_name($data) {
return $this->node_language($data->{$this->name_alias});
}
/**
* Override the behavior of title(). Get the user friendly version of the
* node type.
*/
function title() {
return $this->node_language($this->argument);
}
function node_language($langcode) {
$languages = views_language_list();
return isset($languages[$langcode]) ? $languages[$langcode] : t('Unknown language');
}
}
......@@ -30,7 +30,7 @@ function init(&$view, &$options) {
if (!empty($this->options['link_to_node'])) {
$this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
if (module_exists('translation')) {
$this->additional_fields['language'] = array('table' => 'node', 'field' => 'language');
$this->additional_fields['langcode'] = array('table' => 'node', 'field' => 'langcode');
}
}
}
......@@ -65,11 +65,11 @@ function render_link($data, $values) {
if ($data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = "node/" . $this->get_value($values, 'nid');
if (isset($this->aliases['language'])) {
if (isset($this->aliases['langcode'])) {
$languages = language_list();
$language = $this->get_value($values, 'language');
if (isset($languages[$language])) {
$this->options['alter']['language'] = $languages[$language];
$langcode = $this->get_value($values, 'langcode');
if (isset($languages[$langcode])) {
$this->options['alter']['language'] = $languages[$langcode];
}
else {
unset($this->options['alter']['language']);
......
......@@ -28,7 +28,7 @@ function init(&$view, &$options) {
$this->additional_fields['vid'] = 'vid';
$this->additional_fields['nid'] = 'nid';
if (module_exists('translation')) {
$this->additional_fields['language'] = array('table' => 'node', 'field' => 'language');
$this->additional_fields['langcode'] = array('table' => 'node', 'field' => 'langcode');
}
}
}
......@@ -63,10 +63,10 @@ function render_link($data, $values) {
$vid = $this->get_value($values, 'vid');
$this->options['alter']['path'] = "node/" . $nid . '/revisions/' . $vid . '/view';
if (module_exists('translation')) {
$language = $this->get_value($values, 'language');
$langcode = $this->get_value($values, 'langcode');
$languages = language_list();
if (isset($languages[$language])) {
$this->options['alter']['language'] = $languages[$language];
if (isset($languages[$langcode])) {
$this->options['alter']['langcode'] = $languages[$langcode];
}
}
}
......
......@@ -26,7 +26,7 @@ function render_link($data, $values) {
// ensure user has access to edit this node.
$node = $this->get_value($values);
$node->status = 1; // unpublished nodes ignore access control
if (empty($node->language) || !translation_supported_type($node->type) || !node_access('view', $node) || !user_access('translate content')) {
if (empty($node->langcode) || !translation_supported_type($node->type) || !node_access('view', $node) || !user_access('translate content')) {
return;
}
......
......@@ -27,7 +27,7 @@ function construct() {
$this->additional_fields['nid'] = 'nid';
$this->additional_fields['tnid'] = 'tnid';
$this->additional_fields['title'] = 'title';
$this->additional_fields['language'] = 'language';
$this->additional_fields['langcode'] = 'langcode';
}
function query() {
......@@ -45,7 +45,7 @@ function render_link($data, $values) {
$tnid = $this->get_value($values, 'tnid');
// Only load translations if the node isn't in the current language.
if ($this->get_value($values, 'language') != $language_interface->langcode) {
if ($this->get_value($values, 'langcode') != $language_interface->langcode) {
$translations = translation_node_get_translations($tnid);
if (isset($translations[$language_interface->langcode])) {
$values->{$this->aliases['nid']} = $translations[$language_interface->langcode]->nid;
......
......@@ -41,7 +41,7 @@ function options_form(&$form, &$form_state) {
'current' => t('Current language'),
'default' => t('Default language'),
);
$options = array_merge($options, locale_language_list());
$options = array_merge($options, views_language_list());
$form['language'] = array(
'#type' => 'select',
'#options' => $options,
......@@ -75,20 +75,20 @@ function query() {
switch ($this->options['language']) {
case 'current':
$def['extra'][] = array(
'field' => 'language',
'field' => 'langcode',
'value' => '***CURRENT_LANGUAGE***',
);
break;
case 'default':
$def['extra'][] = array(
'field' => 'language',
'field' => 'langcode',
'value' => '***DEFAULT_LANGUAGE***',
);
break;
// Other values will be the language codes.
default:
$def['extra'][] = array(
'field' => 'language',
'field' => 'langcode',
'value' => $this->options['language'],
);
break;
......
......@@ -165,9 +165,10 @@ function comment_views_data() {
);
// Language field
if (module_exists('locale')) {
$data['comment']['language'] = array(
// Langcode field
if (module_exists('language')) {
$data['comment']['language']['moved to'] = array('comment', 'langcode');
$data['comment']['langcode'] = array(
'title' => t('Language'),
'help' => t('The language the comment is in.'),
'field' => array(
......
......@@ -165,7 +165,7 @@ function field_views_field_default_views_data($field) {
$tables[FIELD_LOAD_REVISION] = $revision_table;
}
$add_fields = array('delta', 'language', 'bundle');
$add_fields = array('delta', 'langcode', 'bundle');
foreach ($field['columns'] as $column_name => $attributes) {
$add_fields[] = _field_sql_storage_columnname($field['field_name'], $column_name);
}
......
......@@ -19,10 +19,6 @@ function language_views_data() {
'help' => t('A language used in drupal.'),
);
// name
// direction
// weight
$data['language']['langcode'] = array(
'title' => t('Language code'),
'help' => t("Language code, e.g. 'de' or 'en-US'."),
......
......@@ -195,27 +195,3 @@ function locale_views_data() {
return $data;
}
/**
* Implements hook_views_data_alter().
*/
function locale_views_data_alter(&$data) {
// Language field
$data['node']['language'] = array(
'title' => t('Language'),
'help' => t('The language the content is in.'),
'field' => array(
'id' => 'node_language',
'click sortable' => TRUE,
),
'filter' => array(
'id' => 'node_language',
),
'argument' => array(
'id' => 'node_language',
),
'sort' => array(
'id' => 'standard',
),
);
}
......@@ -221,6 +221,28 @@ function node_views_data() {
),
);
// Language field
if (module_exists('language')) {
$data['node']['language']['moved to'] = array('node', 'langcode');
$data['node']['langcode'] = array(
'title' => t('Language'),
'help' => t('The language the content is in.'),
'field' => array(
'id' => 'node_language',
'click sortable' => TRUE,
),
'filter' => array(
'id' => 'language',
),
'argument' => array(
'id' => 'language',
),
'sort' => array(
'id' => 'standard',
),
);
}
// Define some fields based upon views_handler_field_entity in the entity
// table so they can be re-used with other query backends.
// @see views_handler_field_entity
......
......@@ -151,7 +151,8 @@ function user_views_data() {
);
// language
$data['users']['language'] = array(
$data['users']['language']['moved to'] = array('users', 'langcode');
$data['users']['langcode'] = array(
'title' => t('Language'), // The item it appears as on the UI,
'help' => t('Language of the user'),
'field' => array(
......
......@@ -36,6 +36,7 @@ function views_core_modules() {
'field',
'filter',
'file',
'language',
'locale',
'node',
'search',
......@@ -964,18 +965,18 @@ function views_add_contextual_links(&$render_element, $location, $view, $display
* Boolean to return all languages or only enabled ones
*
* @see locale_language_list()
* @todo Figure out whether we need this with language module.
*/
function views_language_list($field = 'name', $all = FALSE) {
if ($all) {
$languages = language_list();
}
else {
$languages = language_list('enabled');
$languages = $languages[1];
$languages = language_list();
}
$list = array();
foreach ($languages as $language) {
$list[$language->language] = ($field == 'name') ? t($language->name) : $language->$field;
$list[$language->langcode] = ($field == 'name') ? t($language->name) : $language->$field;
}
return $list;
}
......
......@@ -48,7 +48,7 @@ function views_tokens($type, $tokens, array $data = array(), array $options = ar
$url_options['language'] = $options['language'];
}
$sanitize = !empty($options['sanitize']);
$langcode = isset($options['language']) ? $options['language']->language : NULL;
$langcode = isset($options['language']) ? $options['language']->langcode : NULL;
$replacements = array();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment