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