You are here

class TranslationLanguageRenderer in Display Suite 8.2

Same name and namespace in other branches
  1. 8.4 src/Plugin/views/Entity/Render/TranslationLanguageRenderer.php \Drupal\ds\Plugin\views\Entity\Render\TranslationLanguageRenderer
  2. 8.3 src/Plugin/views/Entity/Render/TranslationLanguageRenderer.php \Drupal\ds\Plugin\views\Entity\Render\TranslationLanguageRenderer

Renders entities in the current language.

Hierarchy

Expanded class hierarchy of TranslationLanguageRenderer

1 string reference to 'TranslationLanguageRenderer'
EntityRow::getEntityTranslationRenderer in src/Plugin/views/row/EntityRow.php
Returns the current renderer.

File

src/Plugin/views/Entity/Render/TranslationLanguageRenderer.php, line 11

Namespace

Drupal\ds\Plugin\views\Entity\Render
View source
class TranslationLanguageRenderer extends DefaultLanguageRenderer {

  /**
   * Stores the field alias of the langcode column.
   *
   * @var string
   */
  protected $langcodeAlias;

  /**
   * {@inheritdoc}
   */
  public function query(QueryPluginBase $query, $relationship = NULL) {

    // There is no point in getting the language, in case the site is not
    // multilingual.
    if (!$this->languageManager
      ->isMultilingual()) {
      return;
    }

    // If the data table is defined, we use the translation language as render
    // language, otherwise we fall back to the default entity language, which is
    // stored in the revision table for revisionable entity types.
    $langcode_key = $this->entityType
      ->getKey('langcode');
    foreach (array(
      'data_table',
      'revision_table',
      'base_table',
    ) as $key) {
      if ($table = $this->entityType
        ->get($key)) {
        $table_alias = $query
          ->ensureTable($table);
        $this->langcodeAlias = $query
          ->addField($table_alias, $langcode_key);
        break;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function preRender(array $result) {
    parent::dsPreRender($result, TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $row) {
    $entity_id = $row->_entity
      ->id();
    $langcode = $this
      ->getLangcode($row);
    return $this->build[$entity_id][$langcode];
  }

  /**
   * {@inheritdoc}
   */
  public function getLangcode(ResultRow $row) {
    return isset($row->{$this->langcodeAlias}) ? $row->{$this->langcodeAlias} : $this->languageManager
      ->getDefaultLanguage()
      ->getId();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::dsPreRender protected function Pre renders all the Display Suite rows.
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
TranslationLanguageRenderer::$langcodeAlias protected property Stores the field alias of the langcode column.
TranslationLanguageRenderer::getLangcode public function Returns the language code associated to the given row. Overrides DefaultLanguageRenderer::getLangcode
TranslationLanguageRenderer::preRender public function Runs before each entity is rendered. Overrides RendererBase::preRender
TranslationLanguageRenderer::query public function Alters the query if needed. Overrides EntityTranslationRendererBase::query
TranslationLanguageRenderer::render public function Renders entity data. Overrides EntityTranslationRendererBase::render