Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\views\Entity\Render;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
6 use Drupal\Core\TypedData\TranslatableInterface;
|
Chris@0
|
7 use Drupal\views\Plugin\views\PluginBase;
|
Chris@0
|
8 use Drupal\views\ResultRow;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Trait used to instantiate the view's entity translation renderer.
|
Chris@0
|
12 */
|
Chris@0
|
13 trait EntityTranslationRenderTrait {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * The renderer to be used to render the entity row.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var \Drupal\views\Entity\Render\EntityTranslationRendererBase
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $entityTranslationRenderer;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Returns the current renderer.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @return \Drupal\views\Entity\Render\EntityTranslationRendererBase
|
Chris@0
|
26 * The configured renderer.
|
Chris@0
|
27 */
|
Chris@0
|
28 protected function getEntityTranslationRenderer() {
|
Chris@0
|
29 if (!isset($this->entityTranslationRenderer)) {
|
Chris@0
|
30 $view = $this->getView();
|
Chris@0
|
31 $rendering_language = $view->display_handler->getOption('rendering_language');
|
Chris@0
|
32 $langcode = NULL;
|
Chris@0
|
33 $dynamic_renderers = [
|
Chris@0
|
34 '***LANGUAGE_entity_translation***' => 'TranslationLanguageRenderer',
|
Chris@0
|
35 '***LANGUAGE_entity_default***' => 'DefaultLanguageRenderer',
|
Chris@0
|
36 ];
|
Chris@0
|
37 if (isset($dynamic_renderers[$rendering_language])) {
|
Chris@0
|
38 // Dynamic language set based on result rows or instance defaults.
|
Chris@0
|
39 $renderer = $dynamic_renderers[$rendering_language];
|
Chris@0
|
40 }
|
Chris@0
|
41 else {
|
Chris@0
|
42 if (strpos($rendering_language, '***LANGUAGE_') !== FALSE) {
|
Chris@0
|
43 $langcode = PluginBase::queryLanguageSubstitutions()[$rendering_language];
|
Chris@0
|
44 }
|
Chris@0
|
45 else {
|
Chris@0
|
46 // Specific langcode set.
|
Chris@0
|
47 $langcode = $rendering_language;
|
Chris@0
|
48 }
|
Chris@0
|
49 $renderer = 'ConfigurableLanguageRenderer';
|
Chris@0
|
50 }
|
Chris@0
|
51 $class = '\Drupal\views\Entity\Render\\' . $renderer;
|
Chris@18
|
52 $entity_type = $this->getEntityTypeManager()->getDefinition($this->getEntityTypeId());
|
Chris@0
|
53 $this->entityTranslationRenderer = new $class($view, $this->getLanguageManager(), $entity_type, $langcode);
|
Chris@0
|
54 }
|
Chris@0
|
55 return $this->entityTranslationRenderer;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Returns the entity translation matching the configured row language.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
62 * The entity object the field value being processed is attached to.
|
Chris@0
|
63 * @param \Drupal\views\ResultRow $row
|
Chris@0
|
64 * The result row the field value being processed belongs to.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @return \Drupal\Core\Entity\FieldableEntityInterface
|
Chris@0
|
67 * The entity translation object for the specified row.
|
Chris@0
|
68 */
|
Chris@0
|
69 public function getEntityTranslation(EntityInterface $entity, ResultRow $row) {
|
Chris@0
|
70 // We assume the same language should be used for all entity fields
|
Chris@0
|
71 // belonging to a single row, even if they are attached to different entity
|
Chris@0
|
72 // types. Below we apply language fallback to ensure a valid value is always
|
Chris@0
|
73 // picked.
|
Chris@0
|
74 $translation = $entity;
|
Chris@0
|
75 if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) {
|
Chris@0
|
76 $langcode = $this->getEntityTranslationRenderer()->getLangcode($row);
|
Chris@18
|
77 $translation = $this->getEntityRepository()->getTranslationFromContext($entity, $langcode);
|
Chris@0
|
78 }
|
Chris@0
|
79 return $translation;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Returns the entity type identifier.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return string
|
Chris@0
|
86 * The entity type identifier.
|
Chris@0
|
87 */
|
Chris@0
|
88 abstract public function getEntityTypeId();
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@18
|
91 * Returns the entity type manager.
|
Chris@0
|
92 *
|
Chris@18
|
93 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@18
|
94 * The entity type manager.
|
Chris@0
|
95 */
|
Chris@18
|
96 protected function getEntityTypeManager() {
|
Chris@18
|
97 @trigger_error('Classes that use EntityTranslationRenderTrait must provide a getEntityTypeManager() method since drupal:8.7.0. This implementation will become abstract before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
|
Chris@18
|
98 return \Drupal::entityTypeManager();
|
Chris@18
|
99 }
|
Chris@18
|
100
|
Chris@18
|
101 /**
|
Chris@18
|
102 * Returns the entity repository.
|
Chris@18
|
103 *
|
Chris@18
|
104 * @return \Drupal\Core\Entity\EntityRepositoryInterface
|
Chris@18
|
105 * The entity repository.
|
Chris@18
|
106 */
|
Chris@18
|
107 protected function getEntityRepository() {
|
Chris@18
|
108 @trigger_error('Classes that use EntityTranslationRenderTrait must provide a getEntityRepository() method since drupal:8.7.0. This implementation will become abstract before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
|
Chris@18
|
109 return \Drupal::service('entity.repository');
|
Chris@18
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * Returns the language manager.
|
Chris@0
|
114 *
|
Chris@0
|
115 * @return \Drupal\Core\Language\LanguageManagerInterface
|
Chris@0
|
116 * The language manager.
|
Chris@0
|
117 */
|
Chris@0
|
118 abstract protected function getLanguageManager();
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * Returns the top object of a view.
|
Chris@0
|
122 *
|
Chris@0
|
123 * @return \Drupal\views\ViewExecutable
|
Chris@0
|
124 * The view object.
|
Chris@0
|
125 */
|
Chris@0
|
126 abstract protected function getView();
|
Chris@0
|
127
|
Chris@0
|
128 }
|