Chris@0: entityTranslationRenderer)) { Chris@0: $view = $this->getView(); Chris@0: $rendering_language = $view->display_handler->getOption('rendering_language'); Chris@0: $langcode = NULL; Chris@0: $dynamic_renderers = [ Chris@0: '***LANGUAGE_entity_translation***' => 'TranslationLanguageRenderer', Chris@0: '***LANGUAGE_entity_default***' => 'DefaultLanguageRenderer', Chris@0: ]; Chris@0: if (isset($dynamic_renderers[$rendering_language])) { Chris@0: // Dynamic language set based on result rows or instance defaults. Chris@0: $renderer = $dynamic_renderers[$rendering_language]; Chris@0: } Chris@0: else { Chris@0: if (strpos($rendering_language, '***LANGUAGE_') !== FALSE) { Chris@0: $langcode = PluginBase::queryLanguageSubstitutions()[$rendering_language]; Chris@0: } Chris@0: else { Chris@0: // Specific langcode set. Chris@0: $langcode = $rendering_language; Chris@0: } Chris@0: $renderer = 'ConfigurableLanguageRenderer'; Chris@0: } Chris@0: $class = '\Drupal\views\Entity\Render\\' . $renderer; Chris@18: $entity_type = $this->getEntityTypeManager()->getDefinition($this->getEntityTypeId()); Chris@0: $this->entityTranslationRenderer = new $class($view, $this->getLanguageManager(), $entity_type, $langcode); Chris@0: } Chris@0: return $this->entityTranslationRenderer; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the entity translation matching the configured row language. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@0: * The entity object the field value being processed is attached to. Chris@0: * @param \Drupal\views\ResultRow $row Chris@0: * The result row the field value being processed belongs to. Chris@0: * Chris@0: * @return \Drupal\Core\Entity\FieldableEntityInterface Chris@0: * The entity translation object for the specified row. Chris@0: */ Chris@0: public function getEntityTranslation(EntityInterface $entity, ResultRow $row) { Chris@0: // We assume the same language should be used for all entity fields Chris@0: // belonging to a single row, even if they are attached to different entity Chris@0: // types. Below we apply language fallback to ensure a valid value is always Chris@0: // picked. Chris@0: $translation = $entity; Chris@0: if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) { Chris@0: $langcode = $this->getEntityTranslationRenderer()->getLangcode($row); Chris@18: $translation = $this->getEntityRepository()->getTranslationFromContext($entity, $langcode); Chris@0: } Chris@0: return $translation; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the entity type identifier. Chris@0: * Chris@0: * @return string Chris@0: * The entity type identifier. Chris@0: */ Chris@0: abstract public function getEntityTypeId(); Chris@0: Chris@0: /** Chris@18: * Returns the entity type manager. Chris@0: * Chris@18: * @return \Drupal\Core\Entity\EntityTypeManagerInterface Chris@18: * The entity type manager. Chris@0: */ Chris@18: protected function getEntityTypeManager() { Chris@18: @trigger_error('Classes that use EntityTranslationRenderTrait must provide a getEntityTypeManager() method since drupal:8.7.0. This implementation will become abstract before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: return \Drupal::entityTypeManager(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Returns the entity repository. Chris@18: * Chris@18: * @return \Drupal\Core\Entity\EntityRepositoryInterface Chris@18: * The entity repository. Chris@18: */ Chris@18: protected function getEntityRepository() { Chris@18: @trigger_error('Classes that use EntityTranslationRenderTrait must provide a getEntityRepository() method since drupal:8.7.0. This implementation will become abstract before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: return \Drupal::service('entity.repository'); Chris@18: } Chris@0: Chris@0: /** Chris@0: * Returns the language manager. Chris@0: * Chris@0: * @return \Drupal\Core\Language\LanguageManagerInterface Chris@0: * The language manager. Chris@0: */ Chris@0: abstract protected function getLanguageManager(); Chris@0: Chris@0: /** Chris@0: * Returns the top object of a view. Chris@0: * Chris@0: * @return \Drupal\views\ViewExecutable Chris@0: * The view object. Chris@0: */ Chris@0: abstract protected function getView(); Chris@0: Chris@0: }