class TranslationLanguageRenderer

Same name in other branches
  1. 9 core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php \Drupal\views\Entity\Render\TranslationLanguageRenderer
  2. 8.9.x core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php \Drupal\views\Entity\Render\TranslationLanguageRenderer
  3. 10 core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php \Drupal\views\Entity\Render\TranslationLanguageRenderer

Renders entity translations in their row language.

Hierarchy

Expanded class hierarchy of TranslationLanguageRenderer

1 string reference to 'TranslationLanguageRenderer'
EntityTranslationRenderTrait::getEntityTranslationRenderer in core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php
Returns the current renderer.

File

core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php, line 12

Namespace

Drupal\views\Entity\Render
View source
class TranslationLanguageRenderer extends EntityTranslationRendererBase {
    
    /**
     * Stores the field alias of the langcode column.
     *
     * @var string
     */
    protected $langcodeAlias;
    
    /**
     * {@inheritdoc}
     */
    public function query(QueryPluginBase $query, $relationship = NULL) {
        // In order to render in the translation language of the entity, we need
        // to add the language code of the entity to the query. Skip if the site
        // is not multilingual or the entity is not translatable.
        if (!$this->languageManager
            ->isMultilingual() || !$this->entityType
            ->hasKey('langcode')) {
            return;
        }
        $langcode_table = $this->getLangcodeTable($query, $relationship);
        if ($langcode_table) {
            
            /** @var \Drupal\views\Plugin\views\query\Sql $query */
            $table_alias = $query->ensureTable($langcode_table, $relationship);
            $langcode_key = $this->entityType
                ->getKey('langcode');
            $this->langcodeAlias = $query->addField($table_alias, $langcode_key);
        }
    }
    
    /**
     * Returns the name of the table holding the "langcode" field.
     *
     * @param \Drupal\views\Plugin\views\query\QueryPluginBase $query
     *   The query being executed.
     * @param string $relationship
     *   The relationship used by the entity type.
     *
     * @return string
     *   A table name.
     */
    protected function getLangcodeTable(QueryPluginBase $query, $relationship) {
        
        /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
        $storage = \Drupal::entityTypeManager()->getStorage($this->entityType
            ->id());
        $langcode_key = $this->entityType
            ->getKey('langcode');
        $langcode_table = $storage->getTableMapping()
            ->getFieldTableName($langcode_key);
        // If the entity type is revisionable, we need to take into account views of
        // entity revisions. Usually the view will use the entity data table as the
        // query base table, however, in case of an entity revision view, we need to
        // use the revision table or the revision data table, depending on which one
        // is being used as query base table.
        if ($this->entityType
            ->isRevisionable()) {
            $query_base_table = $query->relationships[$relationship]['base'] ?? $this->view->storage
                ->get('base_table');
            $revision_table = $storage->getRevisionTable();
            $revision_data_table = $storage->getRevisionDataTable();
            if ($query_base_table === $revision_table) {
                $langcode_table = $revision_table;
            }
            elseif ($query_base_table === $revision_data_table) {
                $langcode_table = $revision_data_table;
            }
        }
        return $langcode_table;
    }
    
    /**
     * {@inheritdoc}
     */
    public function preRenderByRelationship(array $result, string $relationship) : void {
        $view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->entityType
            ->id());
        
        /** @var \Drupal\views\ResultRow $row */
        foreach ($result as $row) {
            if ($entity = $this->getEntity($row, $relationship)) {
                $entity->view = $this->view;
                $langcode = $this->getLangcodeByRelationship($row, $relationship);
                $this->build[$entity->id()][$langcode] = $view_builder->view($entity, $this->view->rowPlugin->options['view_mode'], $langcode);
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function renderByRelationship(ResultRow $row, string $relationship) : array {
        if ($entity = $this->getEntity($row, $relationship)) {
            $entity_id = $entity->id();
            return $this->build[$entity_id][$this->getLangcodeByRelationship($row, $relationship)];
        }
        return [];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getLangcode(ResultRow $row) {
        return $row->{$this->langcodeAlias} ?? $this->languageManager
            ->getDefaultLanguage()
            ->getId();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheContexts() {
        return [
            'languages:' . LanguageInterface::TYPE_CONTENT,
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
EntityTranslationRendererBase::getEntity protected function Gets the entity associated with a row.
EntityTranslationRendererBase::getLangcodeByRelationship public function Returns the language code associated with the given row. 1
EntityTranslationRendererBase::preRender public function Runs before each entity is rendered. Overrides RendererBase::preRender
EntityTranslationRendererBase::render public function Renders entity data. Overrides RendererBase::render
RendererBase::$build protected property Contains an array of render arrays, one for each rendered entity.
RendererBase::$entityType protected property The type of the entity being rendered.
RendererBase::$languageManager protected property The language manager.
RendererBase::$view public property The view executable wrapping the view storage entity.
RendererBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
RendererBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
RendererBase::__construct public function Constructs a renderer object. 2
TranslationLanguageRenderer::$langcodeAlias protected property Stores the field alias of the langcode column.
TranslationLanguageRenderer::getCacheContexts public function The cache contexts associated with this object. Overrides RendererBase::getCacheContexts
TranslationLanguageRenderer::getLangcode public function Returns the language code associated with the given row. Overrides EntityTranslationRendererBase::getLangcode
TranslationLanguageRenderer::getLangcodeTable protected function Returns the name of the table holding the "langcode" field.
TranslationLanguageRenderer::preRenderByRelationship public function Runs before each entity is rendered if a relationship is needed. Overrides EntityTranslationRendererBase::preRenderByRelationship
TranslationLanguageRenderer::query public function Alters the query if needed. Overrides EntityTranslationRendererBase::query
TranslationLanguageRenderer::renderByRelationship public function Renders entity data. Overrides EntityTranslationRendererBase::renderByRelationship

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.