Mercurial > hg > isophonics-drupal-site
comparison core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\views\Entity\Render; | |
4 | |
5 use Drupal\Core\Entity\EntityInterface; | |
6 use Drupal\Core\TypedData\TranslatableInterface; | |
7 use Drupal\views\Plugin\views\PluginBase; | |
8 use Drupal\views\ResultRow; | |
9 | |
10 /** | |
11 * Trait used to instantiate the view's entity translation renderer. | |
12 */ | |
13 trait EntityTranslationRenderTrait { | |
14 | |
15 /** | |
16 * The renderer to be used to render the entity row. | |
17 * | |
18 * @var \Drupal\views\Entity\Render\EntityTranslationRendererBase | |
19 */ | |
20 protected $entityTranslationRenderer; | |
21 | |
22 /** | |
23 * Returns the current renderer. | |
24 * | |
25 * @return \Drupal\views\Entity\Render\EntityTranslationRendererBase | |
26 * The configured renderer. | |
27 */ | |
28 protected function getEntityTranslationRenderer() { | |
29 if (!isset($this->entityTranslationRenderer)) { | |
30 $view = $this->getView(); | |
31 $rendering_language = $view->display_handler->getOption('rendering_language'); | |
32 $langcode = NULL; | |
33 $dynamic_renderers = [ | |
34 '***LANGUAGE_entity_translation***' => 'TranslationLanguageRenderer', | |
35 '***LANGUAGE_entity_default***' => 'DefaultLanguageRenderer', | |
36 ]; | |
37 if (isset($dynamic_renderers[$rendering_language])) { | |
38 // Dynamic language set based on result rows or instance defaults. | |
39 $renderer = $dynamic_renderers[$rendering_language]; | |
40 } | |
41 else { | |
42 if (strpos($rendering_language, '***LANGUAGE_') !== FALSE) { | |
43 $langcode = PluginBase::queryLanguageSubstitutions()[$rendering_language]; | |
44 } | |
45 else { | |
46 // Specific langcode set. | |
47 $langcode = $rendering_language; | |
48 } | |
49 $renderer = 'ConfigurableLanguageRenderer'; | |
50 } | |
51 $class = '\Drupal\views\Entity\Render\\' . $renderer; | |
52 $entity_type = $this->getEntityManager()->getDefinition($this->getEntityTypeId()); | |
53 $this->entityTranslationRenderer = new $class($view, $this->getLanguageManager(), $entity_type, $langcode); | |
54 } | |
55 return $this->entityTranslationRenderer; | |
56 } | |
57 | |
58 /** | |
59 * Returns the entity translation matching the configured row language. | |
60 * | |
61 * @param \Drupal\Core\Entity\EntityInterface $entity | |
62 * The entity object the field value being processed is attached to. | |
63 * @param \Drupal\views\ResultRow $row | |
64 * The result row the field value being processed belongs to. | |
65 * | |
66 * @return \Drupal\Core\Entity\FieldableEntityInterface | |
67 * The entity translation object for the specified row. | |
68 */ | |
69 public function getEntityTranslation(EntityInterface $entity, ResultRow $row) { | |
70 // We assume the same language should be used for all entity fields | |
71 // belonging to a single row, even if they are attached to different entity | |
72 // types. Below we apply language fallback to ensure a valid value is always | |
73 // picked. | |
74 $translation = $entity; | |
75 if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) { | |
76 $langcode = $this->getEntityTranslationRenderer()->getLangcode($row); | |
77 $translation = $this->getEntityManager()->getTranslationFromContext($entity, $langcode); | |
78 } | |
79 return $translation; | |
80 } | |
81 | |
82 /** | |
83 * Returns the entity type identifier. | |
84 * | |
85 * @return string | |
86 * The entity type identifier. | |
87 */ | |
88 abstract public function getEntityTypeId(); | |
89 | |
90 /** | |
91 * Returns the entity manager. | |
92 * | |
93 * @return \Drupal\Core\Entity\EntityManagerInterface | |
94 * The entity manager. | |
95 */ | |
96 abstract protected function getEntityManager(); | |
97 | |
98 /** | |
99 * Returns the language manager. | |
100 * | |
101 * @return \Drupal\Core\Language\LanguageManagerInterface | |
102 * The language manager. | |
103 */ | |
104 abstract protected function getLanguageManager(); | |
105 | |
106 /** | |
107 * Returns the top object of a view. | |
108 * | |
109 * @return \Drupal\views\ViewExecutable | |
110 * The view object. | |
111 */ | |
112 abstract protected function getView(); | |
113 | |
114 } |