diff --git a/core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php b/core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php index 1be7364..1cf66fb 100644 --- a/core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php +++ b/core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php @@ -7,7 +7,9 @@ namespace Drupal\views\Entity\Render; +use Drupal\Core\Entity\EntityInterface; use Drupal\views\Plugin\views\PluginBase; +use Drupal\views\ResultRow; /** * Trait used to instantiate the view's entity language render. @@ -58,6 +60,34 @@ protected function getEntityTranslationRenderer() { } /** + * Returns language code for a result row. + * + * @param EntityInterface $entity + * An entity object. + * @param ResultRow $row + * A result row. + * + * @return string + * The language code of the row. + */ + protected function getRowLangcode(EntityInterface $entity, ResultRow $row) { + // Even if the current field is not attached to the main entity, we use it + // to determine the field language, as we assume the same language should + // be used for all values belonging to a single row, when possible. Below + // we apply language fallback to ensure a valid value is always picked. + $langcode = $this->getEntityTranslationRenderer()->getLangcode($row); + + // Give the Entity Field API a chance to fallback to a different language + // (or LanguageInterface::LANGCODE_NOT_SPECIFIED), in case the field has + // no data for the selected language. FieldItemListInterface::view() does + // this as well, but since the returned language code is used before + // calling it, the fallback needs to happen explicitly. + $langcode = $this->entityManager->getTranslationFromContext($entity, $langcode)->language()->getId(); + + return $langcode; + } + + /** * Returns the entity type identifier. * * @return string diff --git a/core/modules/views/src/Plugin/views/field/EntityOperations.php b/core/modules/views/src/Plugin/views/field/EntityOperations.php index 35fbfc3..de0ad4f 100644 --- a/core/modules/views/src/Plugin/views/field/EntityOperations.php +++ b/core/modules/views/src/Plugin/views/field/EntityOperations.php @@ -9,7 +9,9 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Routing\RedirectDestinationTrait; +use Drupal\views\Entity\Render\EntityTranslationRenderTrait; use Drupal\views\ResultRow; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -22,6 +24,7 @@ */ class EntityOperations extends FieldPluginBase { + use EntityTranslationRenderTrait; use RedirectDestinationTrait; /** @@ -32,6 +35,13 @@ class EntityOperations extends FieldPluginBase { protected $entityManager; /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected $languageManager; + + /** * Constructor. * * @param array $configuration @@ -43,9 +53,10 @@ class EntityOperations extends FieldPluginBase { * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager) { + public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityManager = $entity_manager; + $this->languageManager = $language_manager; } /** @@ -56,7 +67,8 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('entity.manager') + $container->get('entity.manager'), + $container->get('language_manager') ); } @@ -99,7 +111,9 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { */ public function render(ResultRow $values) { $entity = $this->getEntity($values); - $operations = $this->entityManager->getListBuilder($entity->getEntityTypeId())->getOperations($entity); + $langcode = $this->getRowLangcode($entity, $values); + $translated_entity = $entity->getTranslation($langcode); + $operations = $this->entityManager->getListBuilder($translated_entity->getEntityTypeId())->getOperations($translated_entity); if ($this->options['destination']) { foreach ($operations as &$operation) { if (!isset($operation['query'])) { @@ -120,8 +134,34 @@ public function render(ResultRow $values) { * {@inheritdoc} */ public function query() { - // There is nothing to ensure or add for this handler, so we purposefully do - // nothing here and do not call parent::query() either. + $this->getEntityTranslationRenderer()->query($this->query, $this->relationship); + } + + /** + * {@inheritdoc} + */ + public function getEntityTypeId() { + return $this->getEntityType(); + } + + /** + * {@inheritdoc} + */ + protected function getEntityManager() { + return $this->entityManager; + } + + /** + * {@inheritdoc} + */ + protected function getLanguageManager() { + return $this->languageManager; + } + /** + * {@inheritdoc} + */ + protected function getView() { + return $this->view; } } diff --git a/core/modules/views/src/Plugin/views/field/Field.php b/core/modules/views/src/Plugin/views/field/Field.php index 5013b21..b5a337e 100644 --- a/core/modules/views/src/Plugin/views/field/Field.php +++ b/core/modules/views/src/Plugin/views/field/Field.php @@ -909,20 +909,7 @@ protected function addSelfTokens(&$tokens, $item) { */ protected function getFieldLangcode(EntityInterface $entity, ResultRow $row) { if ($this->getFieldDefinition()->isTranslatable()) { - // Even if the current field is not attached to the main entity, we use it - // to determine the field language, as we assume the same language should - // be used for all values belonging to a single row, when possible. Below - // we apply language fallback to ensure a valid value is always picked. - $langcode = $this->getEntityTranslationRenderer()->getLangcode($row); - - // Give the Entity Field API a chance to fallback to a different language - // (or LanguageInterface::LANGCODE_NOT_SPECIFIED), in case the field has - // no data for the selected language. FieldItemListInterface::view() does - // this as well, but since the returned language code is used before - // calling it, the fallback needs to happen explicitly. - $langcode = $this->entityManager->getTranslationFromContext($entity, $langcode)->language()->getId(); - - return $langcode; + return $this->getRowLangcode($entity, $row); } else { return LanguageInterface::LANGCODE_NOT_SPECIFIED;