Commit 9d98cb98 authored by catch's avatar catch

Issue #1828528 by dawehner, xjm, tim.plunkett: Add language module integration for views.

parent a13f7575
<?php
/**
* @file
* Provide views data and handlers for language.module.
*
* @ingroup views_module_handlers
*/
/**
* Implements hook_views_data().
*/
function language_views_data() {
$data['language']['table']['group'] = t('Language');
$data['language']['table']['base'] = array(
'field' => 'langcode',
'title' => t('Language'),
'help' => t('A language used in drupal.'),
);
$data['language']['langcode'] = array(
'title' => t('Language code'),
'help' => t("Language code, e.g. 'de' or 'en-US'."),
'field' => array(
'id' => 'standard',
),
'filter' => array(
'id' => 'string'
),
'argument' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
);
$data['language']['name'] = array(
'title' => t('Language name'),
'help' => t("Language name, e.g. 'German' or 'English'."),
'field' => array(
'id' => 'standard',
),
'filter' => array(
'id' => 'string'
),
'argument' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
);
$data['language']['direction'] = array(
'title' => t('Direction'),
'help' => t('Direction of language (Left-to-Right = 0, Right-to-Left = 1).'),
'field' => array(
'id' => 'numeric',
),
'filter' => array(
'id' => 'numeric'
),
'argument' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
$data['language']['weight'] = array(
'title' => t('Weight'),
'help' => t('Weight, used in lists of languages.'),
'field' => array(
'id' => 'numeric',
),
'filter' => array(
'id' => 'numeric'
),
'argument' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
$data['language']['locked'] = array(
'title' => t('Locked'),
'help' => t('A boolean indicating whether the administrator can edit or delete the language.'),
'field' => array(
'id' => 'boolean',
),
'filter' => array(
'id' => 'boolean',
),
'argument' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
return $data;
}
<?php
/**
* @file
* Contains Drupal\language\Plugin\views\argument\LanguageArgument.
*/
namespace Drupal\language\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Drupal\Core\Annotation\Plugin;
/**
* Defines an argument handler to accept a language.
*
* @ingroup views_argument_handlers
*
* @Plugin(
* id = "language",
* module = "language"
* )
*/
class LanguageArgument extends ArgumentPluginBase {
/**
* Overrides \Drupal\views\Plugin\views\argument\ArgumentPluginBase::summary_name().
*
* Gets the user-friendly version of the language name.
*/
function summary_name($data) {
return $this->language($data->{$this->name_alias});
}
/**
* Overrides \Drupal\views\Plugin\views\argument\ArgumentPluginBase::summary_name().
*
* Gets the user friendly version of the language name for display as a
* title placeholder.
*/
function title() {
return $this->language($this->argument);
}
/**
* Returns the language name for a given langcode.
*
* @param string $langcode
* The language code.
*
* @return string
* The translated name for the language, or "Unknown language" if the
* language was not found.
*/
function language($langcode) {
$languages = views_language_list();
return isset($languages[$langcode]) ? $languages[$langcode] : t('Unknown language');
}
}
<?php
/**
* @file
* Contains Drupal\language\Plugin\views\field\LanguageField.
*/
namespace Drupal\language\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Core\Annotation\Plugin;
/**
* Defines a field handler to translate a language into its readable form.
*
* @ingroup views_field_handlers
*
* @Plugin(
* id = "language",
* module = "language"
* )
*/
class LanguageField extends FieldPluginBase {
protected function defineOptions() {
$options = parent::defineOptions();
$options['native_language'] = array('default' => FALSE, 'bool' => TRUE);
return $options;
}
public function buildOptionsForm(&$form, &$form_state) {
parent::buildOptionsForm($form, $form_state);
$form['native_language'] = array(
'#title' => t('Native language'),
'#type' => 'checkbox',
'#default_value' => $this->options['native_language'],
'#description' => t('If enabled, the native name of the language will be displayed'),
);
}
function render($values) {
// @todo: Drupal Core dropped native language until config translation is
// ready, see http://drupal.org/node/1616594.
$value = $this->get_value($values);
$language = language_load($value);
return $language ? $language->name : '';
}
}
<?php
/**
* @file
* Contains Drupal\language\Plugin\views\filter\LanguageFilter.
*/
namespace Drupal\language\Plugin\views\filter;
use Drupal\views\Plugin\views\filter\InOperator;
use Drupal\Core\Annotation\Plugin;
/**
* Provides filtering by language.
*
* @ingroup views_filter_handlers
*
* @Plugin(
* id = "language",
* module = "language"
* )
*/
class LanguageFilter 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"),
);
$languages = array_merge($languages, views_language_list());
$this->value_options = $languages;
}
}
}
<?php
/**
* @file
* Contains Drupal\views\Tests\Language\ArgumentLanguageTest.
*/
namespace Drupal\views\Tests\Language;
use Drupal\Core\Language\Language;
/**
* Tests the argument language handler.
*
* @see Drupal\language\Plugin\views\argument\Language.php
*/
class ArgumentLanguageTest extends LanguageTestBase {
public static function getInfo() {
return array(
'name' => 'Argument: Language',
'description' => 'Tests the argument language handler.',
'group' => 'Views Handlers'
);
}
/**
* Tests the language argument.
*/
public function testArgument() {
foreach (array('en' => 'John', 'xx-lolspeak' => 'George') as $langcode => $name) {
$view = $this->getView();
$view->displayHandlers['default']->overrideOption('arguments', array(
'langcode' => array(
'id' => 'langcode',
'table' => 'views_test_data',
'field' => 'langcode',
),
));
$this->executeView($view, array($langcode));
$expected = array(array(
'name' => $name,
));
$this->assertIdenticalResultset($view, $expected, array('views_test_data_name' => 'name'));
$view->destroy();
}
}
}
<?php
/**
* @file
* Contains Drupal\views\Tests\Language\FieldLanguageTest.
*/
namespace Drupal\views\Tests\Language;
use Drupal\Core\Language\Language;
/**
* Tests the field language handler.
*
* @see Drupal\language\Plugin\views\field\Language
*/
class FieldLanguageTest extends LanguageTestBase {
public static function getInfo() {
return array(
'name' => 'Field: Language',
'description' => 'Tests the field language handler.',
'group' => 'Views Handlers',
);
}
/**
* Tests the language field.
*/
public function testField() {
$view = $this->getView();
$view->displayHandlers['default']->overrideOption('fields', array(
'langcode' => array(
'id' => 'langcode',
'table' => 'views_test_data',
'field' => 'langcode',
),
));
$this->executeView($view);
$this->assertEqual($view->field['langcode']->advanced_render($view->result[0]), 'English');
$this->assertEqual($view->field['langcode']->advanced_render($view->result[1]), 'Lolspeak');
}
}
<?php
/**
* @file
* Contains Drupal\views\Tests\Language\FilterLanguageTest.
*/
namespace Drupal\views\Tests\Language;
use Drupal\Core\Language\Language;
/**
* Tests the filter language handler.
*
* @see Drupal\language\Plugin\views\filter\Language
*/
class FilterLanguageTest extends LanguageTestBase {
public static function getInfo() {
return array(
'name' => 'Filter: Language',
'description' => 'Tests the filter language handler.',
'group' => 'Views Handlers'
);
}
/**
* Tests the language filter.
*/
public function testFilter() {
foreach (array('en' => 'John', 'xx-lolspeak' => 'George') as $langcode => $name) {
$view = $this->getView();
$view->displayHandlers['default']->overrideOption('filters', array(
'langcode' => array(
'id' => 'langcode',
'table' => 'views_test_data',
'field' => 'langcode',
'value' => array($langcode),
),
));
$this->executeView($view);
$expected = array(array(
'name' => $name,
));
$this->assertIdenticalResultset($view, $expected, array('views_test_data_name' => 'name'));
$view->destroy();
}
}
}
<?php
/**
* @file
* Contains Drupal\views\Tests\Language\LanguageTestBase.
*/
namespace Drupal\views\Tests\Language;
use Drupal\views\Tests\ViewTestBase;
use Drupal\Core\Language\Language;
/**
* Defines the base class for all Language handler tests.
*/
abstract class LanguageTestBase extends ViewTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('language');
protected function setUp() {
parent::setUp();
$this->enableViewsTestModule();
// Create another language beside English.
$language = new Language(array('langcode' => 'xx-lolspeak', 'name' => 'Lolspeak'));
language_save($language);
}
/**
* Overrides \Drupal\views\Tests\ViewTestBase::schemaDefinition().
*/
protected function schemaDefinition() {
$schema = parent::schemaDefinition();
$schema['views_test_data']['fields']['langcode'] = array(
'description' => 'The {language}.langcode of this beatle.',
'type' => 'varchar',
'length' => 12,
'default' => '',
);
return $schema;
}
/**
* Overrides \Drupal\views\Tests\ViewTestBase::schemaDefinition().
*/
protected function viewsData() {
$data = parent::viewsData();
$data['views_test_data']['langcode'] = array(
'title' => t('Langcode'),
'help' => t('Langcode'),
'field' => array(
'id' => 'language',
),
'argument' => array(
'id' => 'language',
),
'filter' => array(
'id' => 'language',
),
);
return $data;
}
/**
* Overrides \Drupal\views\Tests\ViewTestBase::dataSet().
*/
protected function dataSet() {
$data = parent::dataSet();
$data[0]['langcode'] = 'en';
$data[1]['langcode'] = 'xx-lolspeak';
$data[2]['langcode'] = '';
$data[3]['langcode'] = '';
$data[4]['langcode'] = '';
return $data;
}
}
...@@ -900,26 +900,24 @@ function views_add_contextual_links(&$render_element, $location, ViewExecutable ...@@ -900,26 +900,24 @@ function views_add_contextual_links(&$render_element, $location, ViewExecutable
} }
/** /**
* Returns an array of language names. * Prepares a list of language names.
* *
* This is a one to one copy of locale_language_list because we can't rely on enabled locale module. * This is a wrapper around language_list to return a plain key value array.
* *
* @param $field * @param string $field
* 'name' => names in current language, localized * The field of the language object which should be used as the value of the
* 'native' => native names * array.
* @param $all * @param int $flags
* Boolean to return all languages or only enabled ones * (optional) Specifies the state of the languages that have to be returned.
* It can be: LANGUAGE_CONFIGURABLE, LANGUAGE_LOCKED, LANGUAGE_ALL.
*
* @return array
* An array of language names (or $field) keyed by the langcode.
* *
* @see locale_language_list() * @see locale_language_list()
* @todo Figure out whether we need this with language module.
*/ */
function views_language_list($field = 'name', $all = FALSE) { function views_language_list($field = 'name', $flags = LANGUAGE_ALL) {
if ($all) { $languages = language_list($flags);
$languages = language_list();
}
else {
$languages = language_list();
}
$list = array(); $list = array();
foreach ($languages as $language) { foreach ($languages as $language) {
$list[$language->langcode] = ($field == 'name') ? t($language->name) : $language->$field; $list[$language->langcode] = ($field == 'name') ? t($language->name) : $language->$field;
......
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