Commit 4a11bb59 authored by catch's avatar catch

Issue #2584745 by mbovan, s_leu: Entity references should be displayed...

Issue #2584745 by mbovan, s_leu: Entity references should be displayed translated on non translatable  entity types
parent c267836c
......@@ -13,6 +13,7 @@
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\EntityDisplayBase;
use Drupal\Core\TypedData\TranslatableInterface;
/**
* Configuration entity that contains display options for all components of a
......@@ -249,7 +250,18 @@ 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, $entity->language()->getId()) : [];
// The language of the field values to display is already determined
// in the incoming $entity. The formatter should build its output of
// those values using:
// - the entity language if the entity is translatable,
// - the current "content language" otherwise.
if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {
$view_langcode = $entity->language()->getId();
}
else {
$view_langcode = NULL;
}
$build_list[$id][$name] = $field_access->isAllowed() ? $formatter->view($items, $view_langcode) : [];
// Apply the field access cacheability metadata to the render array.
$this->renderer->addCacheableDependency($build_list[$id][$name], $field_access);
}
......
......@@ -140,9 +140,26 @@ protected function setUp() {
}
/**
* Tests if the translated entity is displayed in an entity reference field.
* Tests if the entity is displayed in an entity reference field.
*/
public function testTranslatedEntityReferenceDisplay() {
public function testEntityReferenceDisplay() {
// Create a translated referrer entity.
$this->referrerEntity = $this->createReferrerEntity();
$this->assertEntityReferenceDisplay();
// Disable translation for referrer content type.
$this->drupalLogin($this->rootUser);
$this->drupalPostForm('admin/config/regional/content-language', ['settings[node][referrer][translatable]' => FALSE], t('Save configuration'));
// Create a referrer entity without translation.
$this->referrerEntity = $this->createReferrerEntity(FALSE);
$this->assertEntityReferenceDisplay();
}
/**
* Assert entity reference display.
*/
protected function assertEntityReferenceDisplay() {
$url = $this->referrerEntity->urlInfo();
$translation_url = $this->referrerEntity->urlInfo('canonical', ['language' => ConfigurableLanguage::load($this->translateToLangcode)]);
......@@ -169,7 +186,6 @@ protected function setUpLanguages() {
protected function createContent() {
$this->referencedEntityWithTranslation = $this->createReferencedEntityWithTranslation();
$this->referencedEntityWithoutTranslation = $this->createNotTranslatedReferencedEntity();
$this->referrerEntity = $this->createReferrerEntity();
}
/**
......@@ -283,7 +299,7 @@ protected function createNotTranslatedReferencedEntity() {
/**
* Create the referrer entity.
*/
protected function createReferrerEntity() {
protected function createReferrerEntity($translatable = TRUE) {
/** @var \Drupal\node\Entity\Node $node */
$node = entity_create($this->testEntityTypeName, array(
'title' => $this->randomMachineName(),
......@@ -298,8 +314,9 @@ protected function createReferrerEntity() {
),
'langcode' => $this->baseLangcode,
));
$node->save();
$node->addTranslation($this->translateToLangcode, $node->toArray());
if ($translatable) {
$node->addTranslation($this->translateToLangcode, $node->toArray());
}
$node->save();
return $node;
......
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