annotate core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\views\Entity\Render;
Chris@0 4
Chris@0 5 use Drupal\Core\Language\LanguageInterface;
Chris@0 6 use Drupal\views\Plugin\views\query\QueryPluginBase;
Chris@0 7 use Drupal\views\ResultRow;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Renders entity translations in their row language.
Chris@0 11 */
Chris@0 12 class TranslationLanguageRenderer extends EntityTranslationRendererBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Stores the field alias of the langcode column.
Chris@0 16 *
Chris@0 17 * @var string
Chris@0 18 */
Chris@0 19 protected $langcodeAlias;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * {@inheritdoc}
Chris@0 23 */
Chris@0 24 public function query(QueryPluginBase $query, $relationship = NULL) {
Chris@0 25 // In order to render in the translation language of the entity, we need
Chris@0 26 // to add the language code of the entity to the query. Skip if the site
Chris@0 27 // is not multilingual or the entity is not translatable.
Chris@0 28 if (!$this->languageManager->isMultilingual() || !$this->entityType->hasKey('langcode')) {
Chris@0 29 return;
Chris@0 30 }
Chris@0 31 $langcode_table = $this->getLangcodeTable($query, $relationship);
Chris@0 32 if ($langcode_table) {
Chris@0 33 /** @var \Drupal\views\Plugin\views\query\Sql $query */
Chris@0 34 $table_alias = $query->ensureTable($langcode_table, $relationship);
Chris@0 35 $langcode_key = $this->entityType->getKey('langcode');
Chris@0 36 $this->langcodeAlias = $query->addField($table_alias, $langcode_key);
Chris@0 37 }
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Returns the name of the table holding the "langcode" field.
Chris@0 42 *
Chris@0 43 * @param \Drupal\views\Plugin\views\query\QueryPluginBase $query
Chris@0 44 * The query being executed.
Chris@0 45 * @param string $relationship
Chris@0 46 * The relationship used by the entity type.
Chris@0 47 *
Chris@0 48 * @return string
Chris@0 49 * A table name.
Chris@0 50 */
Chris@0 51 protected function getLangcodeTable(QueryPluginBase $query, $relationship) {
Chris@0 52 /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
Chris@0 53 $storage = \Drupal::entityTypeManager()->getStorage($this->entityType->id());
Chris@0 54 $langcode_key = $this->entityType->getKey('langcode');
Chris@0 55 $langcode_table = $storage->getTableMapping()->getFieldTableName($langcode_key);
Chris@0 56
Chris@0 57 // If the entity type is revisionable, we need to take into account views of
Chris@0 58 // entity revisions. Usually the view will use the entity data table as the
Chris@0 59 // query base table, however, in case of an entity revision view, we need to
Chris@0 60 // use the revision table or the revision data table, depending on which one
Chris@0 61 // is being used as query base table.
Chris@0 62 if ($this->entityType->isRevisionable()) {
Chris@0 63 $query_base_table = isset($query->relationships[$relationship]['base']) ?
Chris@0 64 $query->relationships[$relationship]['base'] :
Chris@0 65 $this->view->storage->get('base_table');
Chris@0 66 $revision_table = $storage->getRevisionTable();
Chris@0 67 $revision_data_table = $storage->getRevisionDataTable();
Chris@0 68 if ($query_base_table === $revision_table) {
Chris@0 69 $langcode_table = $revision_table;
Chris@0 70 }
Chris@0 71 elseif ($query_base_table === $revision_data_table) {
Chris@0 72 $langcode_table = $revision_data_table;
Chris@0 73 }
Chris@0 74 }
Chris@0 75
Chris@0 76 return $langcode_table;
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * {@inheritdoc}
Chris@0 81 */
Chris@0 82 public function preRender(array $result) {
Chris@18 83 $view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->entityType->id());
Chris@0 84
Chris@0 85 /** @var \Drupal\views\ResultRow $row */
Chris@0 86 foreach ($result as $row) {
Chris@0 87 $entity = $row->_entity;
Chris@0 88 $entity->view = $this->view;
Chris@0 89 $langcode = $this->getLangcode($row);
Chris@0 90 $this->build[$entity->id()][$langcode] = $view_builder->view($entity, $this->view->rowPlugin->options['view_mode'], $this->getLangcode($row));
Chris@0 91 }
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * {@inheritdoc}
Chris@0 96 */
Chris@0 97 public function render(ResultRow $row) {
Chris@0 98 $entity_id = $row->_entity->id();
Chris@0 99 $langcode = $this->getLangcode($row);
Chris@0 100 return $this->build[$entity_id][$langcode];
Chris@0 101 }
Chris@0 102
Chris@0 103 /**
Chris@0 104 * {@inheritdoc}
Chris@0 105 */
Chris@0 106 public function getLangcode(ResultRow $row) {
Chris@0 107 return isset($row->{$this->langcodeAlias}) ? $row->{$this->langcodeAlias} : $this->languageManager->getDefaultLanguage()->getId();
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * {@inheritdoc}
Chris@0 112 */
Chris@0 113 public function getCacheContexts() {
Chris@0 114 return ['languages:' . LanguageInterface::TYPE_CONTENT];
Chris@0 115 }
Chris@0 116
Chris@0 117 }