annotate 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
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\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@0 52 $entity_type = $this->getEntityManager()->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@0 77 $translation = $this->getEntityManager()->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@0 91 * Returns the entity manager.
Chris@0 92 *
Chris@0 93 * @return \Drupal\Core\Entity\EntityManagerInterface
Chris@0 94 * The entity manager.
Chris@0 95 */
Chris@0 96 abstract protected function getEntityManager();
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Returns the language manager.
Chris@0 100 *
Chris@0 101 * @return \Drupal\Core\Language\LanguageManagerInterface
Chris@0 102 * The language manager.
Chris@0 103 */
Chris@0 104 abstract protected function getLanguageManager();
Chris@0 105
Chris@0 106 /**
Chris@0 107 * Returns the top object of a view.
Chris@0 108 *
Chris@0 109 * @return \Drupal\views\ViewExecutable
Chris@0 110 * The view object.
Chris@0 111 */
Chris@0 112 abstract protected function getView();
Chris@0 113
Chris@0 114 }