diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php index db56e8b..7e71ccf 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php @@ -248,7 +248,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); } diff --git a/core/lib/Drupal/Core/Field/FormatterBase.php b/core/lib/Drupal/Core/Field/FormatterBase.php index a6c8cc8..bc46730 100644 --- a/core/lib/Drupal/Core/Field/FormatterBase.php +++ b/core/lib/Drupal/Core/Field/FormatterBase.php @@ -76,8 +76,8 @@ 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) { + $elements = $this->viewElements($items, $langcode); // If there are actual renderable children, use #theme => field, otherwise, // let access cacheability metadata pass through for correct bubbling. diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php index b9a339f..71b2809 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php @@ -36,24 +36,30 @@ * * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items * The item list. + * @param $langcode + * The language code of entities. * * @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 = NULL) { $entities = array(); - $parent_entity_langcode = $items->getEntity()->language()->getId(); + // TODO + if (!isset($langcode)) { + $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 +82,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(); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php index c362a80..5b5cf52 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php @@ -60,11 +60,11 @@ public function settingsSummary() { /** * {@inheritdoc} */ - public function viewElements(FieldItemListInterface $items) { + public function viewElements(FieldItemListInterface $items, $langcode = NULL) { $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. diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php index f72a4de..b30dc4d 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php @@ -10,6 +10,7 @@ use Drupal\language\Entity\ConfigurableLanguage; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\simpletest\WebTestBase; +use Symfony\Component\Validator\Constraints\False; /** * Tests the translation of entity reference field display on nodes. @@ -18,6 +19,12 @@ */ class EntityReferenceFieldTranslatedReferenceViewTest extends WebTestBase { /** + * The option for untranslatable reference fields. + * + * @var bool + */ + protected $translatable = TRUE; + /** * The langcode of the source language. * * @var string @@ -188,7 +195,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( diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceFieldUntranslatedReferenceViewTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceFieldUntranslatedReferenceViewTest.php new file mode 100644 index 0000000..01a7d54 --- /dev/null +++ b/core/modules/entity_reference/src/Tests/EntityReferenceFieldUntranslatedReferenceViewTest.php @@ -0,0 +1,24 @@ +isEmpty()) { $default_image = $this->getFieldSetting('default_image'); diff --git a/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestTrait.php b/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestTrait.php index 4b828cf..93f2f83 100644 --- a/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestTrait.php +++ b/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestTrait.php @@ -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')