Commit 145b53ce authored by alexpott's avatar alexpott

Issue #2394041 by Gábor Hojtsy: Row language settings from entity views should...

Issue #2394041 by Gábor Hojtsy: Row language settings from entity views should be on display level for all views
parent 47b7c006
......@@ -56,7 +56,6 @@ display:
type: 'entity:comment'
options:
view_mode: default
rendering_language: translation_language_renderer
relationships:
node:
id: node
......@@ -150,6 +149,7 @@ display:
arguments: { }
field_langcode: '***LANGUAGE_language_content***'
field_langcode_add_to_query: null
rendering_language: translation_language_renderer
page_bf:
display_plugin: page
id: page_bf
......
......@@ -251,7 +251,7 @@ function testTranslationRendering() {
// Enable the translation language renderer.
$view = \Drupal::entityManager()->getStorage('view')->load('frontpage');
$display = &$view->getDisplay('default');
$display['display_options']['row']['options']['rendering_language'] = 'translation_language_renderer';
$display['display_options']['rendering_language'] = 'translation_language_renderer';
$view->save();
// Need to check from the beginning, including the base_path, in the url
......
......@@ -55,7 +55,6 @@ display:
type: 'entity:node'
options:
view_mode: teaser
rendering_language: translation_language_renderer
fields:
title:
id: title
......@@ -165,6 +164,7 @@ display:
arguments: { }
field_langcode: '***LANGUAGE_language_content***'
field_langcode_add_to_query: null
rendering_language: translation_language_renderer
page_bf:
display_plugin: page
id: page_bf
......
......@@ -77,7 +77,6 @@ display:
options:
relationship: none
view_mode: default
rendering_language: translation_language_renderer
fields:
name:
id: name
......@@ -164,3 +163,4 @@ display:
entity_field: tid
field_langcode: '***LANGUAGE_site_default***'
field_langcode_add_to_query: null
rendering_language: translation_language_renderer
......@@ -250,6 +250,9 @@ views_display:
field_langcode_add_to_query:
type: string
label: 'Add the field language to the query'
rendering_language:
type: string
label: 'Entity language'
cache_metadata:
type: mapping
label: 'Cache metadata'
......@@ -835,9 +838,6 @@ views_entity_row:
view_mode:
type: string
label: 'View mode'
rendering_language:
type: string
label: 'Rendering language'
views_cache:
type: mapping
......
......@@ -601,6 +601,9 @@ protected function defineOptions() {
'field_langcode_add_to_query' => array(
'default' => TRUE,
),
'rendering_language' => array(
'default' => 'translation_language_renderer',
),
// These types are all plugins that can have individual settings
// and therefore need special handling.
......@@ -1105,6 +1108,10 @@ public function optionsSummary(&$categories, &$options) {
'title' => $this->t('Pager'),
'column' => 'second',
),
'language' => array(
'title' => $this->t('Language'),
'column' => 'second',
),
'exposed' => array(
'title' => $this->t('Exposed form'),
'column' => 'third',
......@@ -1267,14 +1274,21 @@ public function optionsSummary(&$categories, &$options) {
'desc' => $this->t('Allow to set some advanced settings for the query plugin'),
);
$language_options = $this->listLanguages(LanguageInterface::STATE_ALL | LanguageInterface::STATE_SITE_DEFAULT | PluginBase::INCLUDE_NEGOTIATED);
$options['field_langcode'] = array(
'category' => 'other',
'title' => $this->t('Field Language'),
'value' => $language_options[$this->getOption('field_langcode')],
'desc' => $this->t('All fields that support translations will be displayed in the selected language.'),
);
if (\Drupal::languageManager()->isMultilingual() && $this->isBaseTableTranslatable()) {
$rendering_language_options = $this->buildRenderingLanguageOptions();
$options['rendering_language'] = array(
'category' => 'language',
'title' => $this->t('Entity Language'),
'value' => $rendering_language_options[$this->getOption('rendering_language')],
);
$language_options = $this->listLanguages(LanguageInterface::STATE_ALL | LanguageInterface::STATE_SITE_DEFAULT | PluginBase::INCLUDE_NEGOTIATED);
$options['field_langcode'] = array(
'category' => 'language',
'title' => $this->t('Field Language'),
'value' => $language_options[$this->getOption('field_langcode')],
'desc' => $this->t('All fields that support translations will be displayed in the selected language.'),
);
}
$access_plugin = $this->getPlugin('access');
if (!$access_plugin) {
......@@ -1624,18 +1638,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
break;
case 'field_langcode':
$form['#title'] .= $this->t('Field Language');
$translatable_entity_tables = array();
foreach (\Drupal::entityManager()->getDefinitions() as $entity_type) {
if ($entity_type->isTranslatable() && $base_table = $entity_type->getBaseTable()) {
$translatable_entity_tables[] = $base_table;
}
}
// 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->view->storage->get('base_table'), $translatable_entity_tables)) {
if ($this->isBaseTableTranslatable()) {
$languages = $this->listLanguages(LanguageInterface::STATE_ALL | LanguageInterface::STATE_SITE_DEFAULT | PluginBase::INCLUDE_NEGOTIATED);
$form['field_langcode'] = array(
......@@ -1655,6 +1658,21 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['field_language']['#markup'] = $this->t("You don't have translatable entity types.");
}
break;
case 'rendering_language':
$form['#title'] .= $this->t('Entity Language');
if ($this->isBaseTableTranslatable()) {
$options = $this->buildRenderingLanguageOptions();
$form['rendering_language'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => $this->t('Entity language'),
'#default_value' => $this->getOption('rendering_language'),
);
}
else {
$form['rendering_language']['#markup'] = $this->t("You don't have translatable entity types.");
}
break;
case 'style':
$form['#title'] .= $this->t('How should this view be styled');
$style_plugin = $this->getPlugin('style');
......@@ -1972,6 +1990,9 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$this->setOption('field_langcode', $form_state->getValue('field_langcode'));
$this->setOption('field_langcode_add_to_query', $form_state->getValue('field_langcode_add_to_query'));
break;
case 'rendering_language':
$this->setOption('rendering_language', $form_state->getValue('rendering_language'));
break;
case 'use_ajax':
case 'hide_attachment_summary':
case 'show_admin_links':
......@@ -2673,6 +2694,38 @@ public function getExtenders() {
return $this->extenders;
}
/**
* Returns the available rendering strategies for language-aware entities.
*
* @return array
* An array of available entity row renderers keyed by renderer identifiers.
*/
protected function buildRenderingLanguageOptions() {
// @todo Consider making these plugins. See https://drupal.org/node/2173811.
return array(
'current_language_renderer' => $this->t('Current language'),
'default_language_renderer' => $this->t('Default language'),
'translation_language_renderer' => $this->t('Translation language'),
);
}
/**
* Returns whether the base table is of a translatable entity type.
*
* @return bool
* TRUE if the base table is of a translatable entity type, FALSE otherwise.
*/
protected function isBaseTableTranslatable() {
$view_base_table = $this->view->storage->get('base_table');
foreach (\Drupal::entityManager()->getDefinitions() as $entity_type) {
if ($entity_type->isTranslatable() && $base_table = $entity_type->getBaseTable()) {
if ($base_table === $view_base_table) {
return TRUE;
}
}
}
return FALSE;
}
}
/**
......
......@@ -114,12 +114,7 @@ public static function create(ContainerInterface $container, array $configuratio
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['view_mode'] = array('default' => 'default');
// @todo Make the current language renderer the default as soon as we have a
// translation language filter. See https://drupal.org/node/2161845.
$options['rendering_language'] = array('default' => 'translation_language_renderer');
return $options;
}
......@@ -135,30 +130,6 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
'#title' => $this->t('View mode'),
'#default_value' => $this->options['view_mode'],
);
$options = $this->buildRenderingLanguageOptions();
$form['rendering_language'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => $this->t('Rendering language'),
'#default_value' => $this->options['rendering_language'],
'#access' => $this->languageManager->isMultilingual(),
);
}
/**
* Returns the available rendering strategies for language-aware entities.
*
* @return array
* An array of available entity row renderers keyed by renderer identifiers.
*/
protected function buildRenderingLanguageOptions() {
// @todo Consider making these plugins. See https://drupal.org/node/2173811.
return array(
'current_language_renderer' => $this->t('Current language'),
'default_language_renderer' => $this->t('Default language'),
'translation_language_renderer' => $this->t('Translation language'),
);
}
/**
......@@ -182,7 +153,7 @@ public function summaryTitle() {
*/
protected function getRenderer() {
if (!isset($this->renderer)) {
$class = '\Drupal\views\Entity\Render\\' . Container::camelize($this->options['rendering_language']);
$class = '\Drupal\views\Entity\Render\\' . Container::camelize($this->displayHandler->getOption('rendering_language'));
$this->renderer = new $class($this->view, $this->languageManager, $this->entityType);
}
return $this->renderer;
......
......@@ -155,8 +155,7 @@ public function testRenderers() {
*/
protected function assertTranslations($renderer_id, array $expected, $message = '', $group = 'Other') {
$view = Views::getView('test_entity_row_renderers');
$row_plugin = $view->getDisplay()->getPlugin('row');
$row_plugin->options['rendering_language'] = $renderer_id;
$view->getDisplay()->setOption('rendering_language', $renderer_id);
$view->preview();
$result = TRUE;
......
......@@ -31,7 +31,6 @@ display:
options:
relationship: none
view_mode: teaser
rendering_language: translation_language_renderer
sorts:
title:
id: title
......@@ -42,3 +41,4 @@ display:
entity_field: title
field_langcode: '***LANGUAGE_language_content***'
field_langcode_add_to_query: null
rendering_language: translation_language_renderer
......@@ -8,6 +8,7 @@
namespace Drupal\views_ui\Tests;
use Drupal\Component\Utility\String;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\views\Entity\View;
use Drupal\views\Views;
......@@ -88,27 +89,52 @@ public function testMachineNameOption() {
}
/**
* Tests the 'Other' options category on the views edit form.
* Tests the language options on the views edit form.
*/
public function testEditFormOtherOptions() {
// Test the Field language form.
$this->drupalGet('admin/structure/views/view/test_view');
$langcode_url = 'admin/structure/views/nojs/display/test_view/default/field_langcode';
$this->assertLinkByHref($langcode_url);
$this->assertLink(t('Language selected for !type', array('!type' => t('Content'))));
// Click the link and check the form before language is added.
$this->drupalGet($langcode_url);
$this->assertResponse(200);
$this->assertText(t("You don't have translatable entity types."));
// A node view should have language options.
$this->container->get('module_installer')->install(array('node', 'language'));
public function testEditFormLanguageOptions() {
// Language options should not exist without language module.
$test_views = array(
'test_view' => 'default',
'test_display' => 'page_1',
);
foreach ($test_views as $view_name => $display) {
$this->drupalGet('admin/structure/views/view/' . $view_name);
$this->assertResponse(200);
$langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/field_langcode';
$this->assertNoLinkByHref($langcode_url);
$this->assertNoLink(t('Language selected for !type', array('!type' => t('Content'))));
}
// Make the site multilingual and test the options again.
$this->container->get('module_installer')->install(array('language'));
ConfigurableLanguage::createFromLangcode('hu')->save();
$this->resetAll();
$this->rebuildContainer();
$this->drupalGet('admin/structure/views/nojs/display/test_display/page_1/field_langcode');
$this->assertResponse(200);
$this->assertFieldByName('field_langcode', '***LANGUAGE_language_content***');
$this->assertFieldByName('field_langcode_add_to_query', TRUE);
// Language options should now exist with content language defaults.
foreach ($test_views as $view_name => $display) {
$this->drupalGet('admin/structure/views/view/' . $view_name);
$this->assertResponse(200);
$langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/field_langcode';
if ($view_name == 'test_view') {
$this->assertNoLinkByHref($langcode_url);
$this->assertNoLink(t('Language selected for !type', array('!type' => t('Content'))));
}
else {
$this->assertLinkByHref($langcode_url);
$this->assertLink(t('Language selected for !type', array('!type' => t('Content'))));
}
$this->drupalGet($langcode_url);
$this->assertResponse(200);
if ($view_name == 'test_view') {
$this->assertText(t("You don't have translatable entity types."));
}
else {
$this->assertFieldByName('field_langcode', '***LANGUAGE_language_content***');
$this->assertFieldByName('field_langcode_add_to_query', TRUE);
}
}
}
/**
......
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