EntityTranslationRenderTrait.php in Drupal 8
File
core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php
View source
<?php
namespace Drupal\views\Entity\Render;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\views\Plugin\views\PluginBase;
use Drupal\views\ResultRow;
trait EntityTranslationRenderTrait {
protected $entityTranslationRenderer;
protected function getEntityTranslationRenderer() {
if (!isset($this->entityTranslationRenderer)) {
$view = $this
->getView();
$rendering_language = $view->display_handler
->getOption('rendering_language');
$langcode = NULL;
$dynamic_renderers = [
'***LANGUAGE_entity_translation***' => 'TranslationLanguageRenderer',
'***LANGUAGE_entity_default***' => 'DefaultLanguageRenderer',
];
if (isset($dynamic_renderers[$rendering_language])) {
$renderer = $dynamic_renderers[$rendering_language];
}
else {
if (strpos($rendering_language, '***LANGUAGE_') !== FALSE) {
$langcode = PluginBase::queryLanguageSubstitutions()[$rendering_language];
}
else {
$langcode = $rendering_language;
}
$renderer = 'ConfigurableLanguageRenderer';
}
$class = '\\Drupal\\views\\Entity\\Render\\' . $renderer;
$entity_type = $this
->getEntityTypeManager()
->getDefinition($this
->getEntityTypeId());
$this->entityTranslationRenderer = new $class($view, $this
->getLanguageManager(), $entity_type, $langcode);
}
return $this->entityTranslationRenderer;
}
public function getEntityTranslation(EntityInterface $entity, ResultRow $row) {
$translation = $entity;
if ($entity instanceof TranslatableInterface && count($entity
->getTranslationLanguages()) > 1) {
$langcode = $this
->getEntityTranslationRenderer()
->getLangcode($row);
$translation = $this
->getEntityRepository()
->getTranslationFromContext($entity, $langcode);
}
return $translation;
}
public abstract function getEntityTypeId();
protected function getEntityTypeManager() {
@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);
return \Drupal::entityTypeManager();
}
protected function getEntityRepository() {
@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);
return \Drupal::service('entity.repository');
}
protected abstract function getLanguageManager();
protected abstract function getView();
}