Commit d0c13760 authored by alexpott's avatar alexpott

Issue #2543258 by LKS90, hchonov, tassilogroeper, sasanikolic, Berdir, yched,...

Issue #2543258 by LKS90, hchonov, tassilogroeper, sasanikolic, Berdir, yched, plach: Entity references of untranslatable fields are not displayed in the correct  translation
parent 0cd35ba0
......@@ -249,7 +249,7 @@ public function buildMultiple(array $entities) {
$items = $grouped_items[$id];
/** @var \Drupal\Core\Access\AccessResultInterface $field_access */
$field_access = $items->access('view', NULL, TRUE);
$build_list[$id][$name] = $field_access->isAllowed() ? $formatter->view($items) : [];
$build_list[$id][$name] = $field_access->isAllowed() ? $formatter->view($items, $entity->language()->getId()) : [];
// Apply the field access cacheability metadata to the render array.
$this->renderer->addCacheableDependency($build_list[$id][$name], $field_access);
}
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\Element;
/**
......@@ -76,8 +77,12 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
/**
* {@inheritdoc}
*/
public function view(FieldItemListInterface $items) {
$elements = $this->viewElements($items);
public function view(FieldItemListInterface $items, $langcode = NULL) {
// Default the language to the current content language.
if (empty($langcode)) {
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
}
$elements = $this->viewElements($items, $langcode);
// If there are actual renderable children, use #theme => field, otherwise,
// let access cacheability metadata pass through for correct bubbling.
......
......@@ -69,23 +69,28 @@ public function prepareView(array $entities_items);
*
* @param \Drupal\Core\Field\FieldItemListInterface $items
* The field values to be rendered.
* @param string $langcode
* (optional) The language that should be used to render the field. Defaults
* to the current content language.
*
* @return array
* A renderable array for a themed field with its label and all its values.
*/
public function view(FieldItemListInterface $items);
public function view(FieldItemListInterface $items, $langcode = NULL);
/**
* Builds a renderable array for a field value.
*
* @param \Drupal\Core\Field\FieldItemListInterface $items
* The field values to be rendered.
* @param string $langcode
* The language that should be used to render the field.
*
* @return array
* A renderable array for $items, as an array of child elements keyed by
* consecutive numeric indexes starting from 0.
*/
public function viewElements(FieldItemListInterface $items);
public function viewElements(FieldItemListInterface $items, $langcode);
/**
* Returns if the formatter can be used for the provided field.
......
......@@ -30,7 +30,7 @@ class BasicStringFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
......
......@@ -116,7 +116,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$formats = $this->getOutputFormats();
......
......@@ -10,6 +10,7 @@
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -119,11 +120,11 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$view_mode = $this->getSetting('view_mode');
$elements = array();
foreach ($this->getEntitiesToView($items) as $delta => $entity) {
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
// Protect ourselves from recursive rendering.
static $depth = 0;
$depth++;
......
......@@ -36,24 +36,25 @@ abstract class EntityReferenceFormatterBase extends FormatterBase {
*
* @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
* The item list.
* @param string $langcode
* The language code of the referenced entities to display.
*
* @return \Drupal\Core\Entity\EntityInterface[]
* The array of referenced entities to display, keyed by delta.
*
* @see ::prepareView()
*/
protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items) {
protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
$entities = array();
$parent_entity_langcode = $items->getEntity()->language()->getId();
foreach ($items as $delta => $item) {
// Ignore items where no entity could be loaded in prepareView().
if (!empty($item->_loaded)) {
$entity = $item->entity;
// Set the entity in the correct language for display.
if ($entity instanceof TranslatableInterface && $entity->hasTranslation($parent_entity_langcode)) {
$entity = $entity->getTranslation($parent_entity_langcode);
if ($entity instanceof TranslatableInterface) {
$entity = \Drupal::entityManager()->getTranslationFromContext($entity, $langcode);
}
$access = $this->checkAccess($entity);
......@@ -76,8 +77,8 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item
* @see ::prepareView()
* @see ::getEntitiestoView()
*/
public function view(FieldItemListInterface $items) {
$elements = parent::view($items);
public function view(FieldItemListInterface $items, $langcode = NULL) {
$elements = parent::view($items, $langcode);
$field_level_access_cacheability = new CacheableMetadata();
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Language\LanguageInterface;
/**
* Plugin implementation of the 'entity reference ID' formatter.
......@@ -26,10 +27,10 @@ class EntityReferenceIdFormatter extends EntityReferenceFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($this->getEntitiesToView($items) as $delta => $entity) {
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
if ($entity->id()) {
$elements[$delta] = array(
'#plain_text' => $entity->id(),
......
......@@ -59,11 +59,11 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$output_as_link = $this->getSetting('link');
foreach ($this->getEntitiesToView($items) as $delta => $entity) {
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
$label = $entity->label();
// If the link is to be displayed and the entity has a uri, display a
// link.
......
......@@ -27,7 +27,7 @@ class MailToFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -66,7 +66,7 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$settings = $this->getFieldSettings();
......
......@@ -28,7 +28,7 @@ class NumericUnformattedFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -117,7 +117,7 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$url = NULL;
if ($this->getSetting('link_to_entity')) {
......
......@@ -153,7 +153,7 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -165,7 +165,7 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$date_format = $this->getSetting('date_format');
......
......@@ -27,7 +27,7 @@ class UriLinkFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -54,7 +54,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
if ($items->getEntity()->getEntityTypeId() == 'aggregator_feed') {
......
......@@ -30,7 +30,7 @@ class AggregatorXSSFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
......
......@@ -28,7 +28,7 @@ class AuthorNameFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -137,7 +137,7 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$output = array();
......
......@@ -36,7 +36,7 @@ public static function defaultSettings() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -36,7 +36,7 @@ public static function defaultSettings() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -25,7 +25,7 @@ class DateTimePlainFormatter extends DateTimeFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -107,7 +107,7 @@ public static function create(ContainerInterface $container, array $configuratio
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -17,6 +17,14 @@
* @group entity_reference
*/
class EntityReferenceFieldTranslatedReferenceViewTest extends WebTestBase {
/**
* Flag indicating whether the field is translatable.
*
* @var bool
*/
protected $translatable = TRUE;
/**
* The langcode of the source language.
*
......@@ -188,7 +196,7 @@ protected function setUpEntityReferenceField() {
'entity_type' => $this->testEntityTypeName,
'type' => 'entity_reference',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'translatable' => TRUE,
'translatable' => $this->translatable,
'settings' => array(
'allowed_values' => array(
array(
......
......@@ -38,7 +38,7 @@ public static function isApplicable(FieldDefinitionInterface $field_definition)
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
return array('#markup' => 'Nothing to see here');
}
}
......@@ -62,7 +62,7 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -35,7 +35,7 @@ public static function defaultSettings() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
if ($items->isEmpty()) {
......
......@@ -63,7 +63,7 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
if (!empty($items)) {
......
......@@ -63,7 +63,7 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
if (!empty($items)) {
......
......@@ -27,7 +27,7 @@ class TestFieldNoSettingsFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -75,7 +75,7 @@ public function prepareView(array $entities_items) {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -46,7 +46,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$url = NULL;
......
......@@ -34,7 +34,7 @@ public static function isApplicable(FieldDefinitionInterface $field_definition)
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
......
......@@ -25,10 +25,10 @@ class GenericFileFormatter extends FileFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($this->getEntitiesToView($items) as $delta => $file) {
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $file) {
$item = $file->_referringItem;
$elements[$delta] = array(
'#theme' => 'file_link',
......
......@@ -25,11 +25,11 @@ class RSSEnclosureFormatter extends FileFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$entity = $items->getEntity();
// Add the first file as an enclosure to the RSS item. RSS allows only one
// enclosure per item. See: http://en.wikipedia.org/wiki/RSS_enclosure
foreach ($this->getEntitiesToView($items) as $delta => $file) {
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $file) {
$entity->rss_elements[] = array(
'key' => 'enclosure',
'attributes' => array(
......
......@@ -25,10 +25,10 @@ class TableFormatter extends FileFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
if ($files = $this->getEntitiesToView($items)) {
if ($files = $this->getEntitiesToView($items, $langcode)) {
$header = array(t('Attachment'), t('Size'));
$rows = array();
foreach ($files as $delta => $file) {
......
......@@ -25,10 +25,10 @@ class UrlPlainFormatter extends FileFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($this->getEntitiesToView($items) as $delta => $file) {
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $file) {
$elements[$delta] = array(
'#markup' => file_create_url($file->getFileUri()),
'#cache' => array(
......
......@@ -169,9 +169,9 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$files = $this->getEntitiesToView($items);
$files = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
if (empty($files)) {
......
......@@ -8,6 +8,7 @@
namespace Drupal\image\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\FieldConfigInterface;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
......@@ -19,7 +20,7 @@ abstract class ImageFormatterBase extends FileFormatterBase {
/**
* {@inheritdoc}
*/
protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items) {
protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
// Add the default image if needed.
if ($items->isEmpty()) {
$default_image = $this->getFieldSetting('default_image');
......@@ -28,7 +29,6 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item
if (empty($default_image['uuid']) && $this->fieldDefinition instanceof FieldConfigInterface) {
$default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
}
if (!empty($default_image['uuid']) && $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid'])) {
// Clone the FieldItemList into a runtime-only object for the formatter,
// so that the fallback image can be rendered without affecting the
......@@ -48,7 +48,7 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item
}
}
return parent::getEntitiesToView($items);
return parent::getEntitiesToView($items, $langcode);
}
}
......@@ -176,7 +176,7 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = array();
$entity = $items->getEntity();
$settings = $this->getSettings();
......
......@@ -42,7 +42,7 @@ public static function defaultSettings() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = array();
$entity = $items->getEntity();
$settings = $this->getSettings();
......
......@@ -33,7 +33,7 @@ class OptionsDefaultFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
// Only collect allowed options if there are actually items to display.
......
......@@ -32,7 +32,7 @@ class OptionsKeyFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
......
......@@ -192,9 +192,9 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$files = $this->getEntitiesToView($items);
$files = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
if (empty($files)) {
......
......@@ -26,8 +26,8 @@ class ResponsiveImageTestFormatter extends ResponsiveImageFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = parent::viewElements($items);
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
// Unset #item_attributes to test that the theme function can handle that.
foreach ($elements as &$element) {
if (isset($element['#item_attributes'])) {
......
......@@ -28,11 +28,11 @@ class EntityReferenceTaxonomyTermRssFormatter extends EntityReferenceFormatterBa
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
public function viewElements(FieldItemListInterface $items, $langcode) {
$parent_entity = $items->getEntity();
$elements = array();
foreach ($this->getEntitiesToView($items) as $delta => $entity) {
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
$parent_entity->rss_elements[] = array(
'key' => 'category',
'value' => $entity->label(),
......
......@@ -83,7 +83,7 @@ protected function enableTranslation() {
* (optional) If TRUE, create a translatable term reference field. Defaults
* to FALSE.
*/
protected function setUpTermReferenceField($translatable = FALSE) {
protected function setUpTermReferenceField() {
$handler_settings = array(
'target_bundles' => array(
$this->vocabulary->id() => $this->vocabulary->id(),
......@@ -92,7 +92,7 @@ protected function setUpTermReferenceField($translatable = FALSE) {
);
$this->createEntityReferenceField('node', 'article', $this->termFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$field_storage = FieldStorageConfig::loadByName('node', $this->termFieldName);
$field_storage->setTranslatable($translatable);
$field_storage->setTranslatable(FALSE);
$field_storage->save();
entity_get_form_display('node', 'article', 'default')
......
......@@ -50,7 +50,7 @@ protected function setUp() {
$this->vocabulary = $this->createVocabulary();
$this->enableTranslation();
$this->setUpTerm();
$this->setUpTermReferenceField(TRUE);
$this->setUpTermReferenceField();
$this->setUpNode();
}
......
......@@ -67,7 +67,7 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
public