class EntityTranslationRendererBase

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

Defines a base class for entity translation renderers.

Hierarchy

Expanded class hierarchy of EntityTranslationRendererBase

File

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

Namespace

Drupal\views\Entity\Render
View source
abstract class EntityTranslationRendererBase extends RendererBase {
    
    /**
     * Returns the language code associated with the given row.
     *
     * @param \Drupal\views\ResultRow $row
     *   The result row.
     *
     * @return string
     *   A language code.
     */
    public abstract function getLangcode(ResultRow $row);
    
    /**
     * Returns the language code associated with the given row.
     *
     * @param \Drupal\views\ResultRow $row
     *   The result row.
     * @param string $relationship
     *   The relationship to be used.
     *
     * @return string
     */
    public function getLangcodeByRelationship(ResultRow $row, string $relationship) : string {
        // This method needs to be overridden if the relationship is needed in the
        // implementation of getLangcode().
        return $this->getLangcode($row);
    }
    
    /**
     * {@inheritdoc}
     */
    public function query(QueryPluginBase $query, $relationship = NULL) {
    }
    
    /**
     * {@inheritdoc}
     */
    public function preRender(array $result) {
        $this->preRenderByRelationship($result, 'none');
    }
    
    /**
     * Runs before each entity is rendered if a relationship is needed.
     *
     * @param \Drupal\views\ResultRow[] $result
     *   The full array of results from the query.
     * @param string $relationship
     *   The relationship to be used.
     */
    public function preRenderByRelationship(array $result, string $relationship) : void {
        $view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->entityType
            ->id());
        foreach ($result as $row) {
            if ($entity = $this->getEntity($row, $relationship)) {
                $entity->view = $this->view;
                $this->build[$entity->id()] = $view_builder->view($entity, $this->view->rowPlugin->options['view_mode'], $this->getLangcodeByRelationship($row, $relationship));
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function render(ResultRow $row) {
        return $this->renderByRelationship($row, 'none');
    }
    
    /**
     * Renders entity data.
     *
     * @param \Drupal\views\ResultRow $row
     *   A single row of the query result.
     * @param string $relationship
     *   The relationship to be used.
     *
     * @return array
     *   A renderable array for the entity data contained in the result row.
     */
    public function renderByRelationship(ResultRow $row, string $relationship) : array {
        if ($entity = $this->getEntity($row, $relationship)) {
            $entity_id = $entity->id();
            return $this->build[$entity_id];
        }
        return [];
    }
    
    /**
     * Gets the entity associated with a row.
     *
     * @param \Drupal\views\ResultRow $row
     *   The result row.
     * @param string $relationship
     *   (optional) The relationship.
     *
     * @return \Drupal\Core\Entity\EntityInterface|null
     *   The entity might be optional, because the relationship entity might not
     *   always exist.
     */
    protected function getEntity(ResultRow $row, string $relationship = 'none') : ?EntityInterface {
        if ($relationship === 'none') {
            return $row->_entity;
        }
        elseif (isset($row->_relationship_entities[$relationship])) {
            return $row->_relationship_entities[$relationship];
        }
        return NULL;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
EntityTranslationRendererBase::getEntity protected function Gets the entity associated with a row.
EntityTranslationRendererBase::getLangcode abstract public function Returns the language code associated with the given row. 3
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::preRenderByRelationship public function Runs before each entity is rendered if a relationship is needed. 1
EntityTranslationRendererBase::query public function Alters the query if needed. Overrides RendererBase::query 1
EntityTranslationRendererBase::render public function Renders entity data. Overrides RendererBase::render
EntityTranslationRendererBase::renderByRelationship public function Renders entity data. 1
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::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 2
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

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