annotate core/modules/views/src/Entity/Render/EntityFieldRenderer.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\DependencyInjection\DependencySerializationTrait;
Chris@0 6 use Drupal\Core\Entity\Entity\EntityViewDisplay;
Chris@0 7 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 8 use Drupal\Core\Entity\EntityTypeInterface;
Chris@0 9 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 10 use Drupal\views\Plugin\views\field\EntityField;
Chris@0 11 use Drupal\views\Plugin\views\query\QueryPluginBase;
Chris@0 12 use Drupal\views\ResultRow;
Chris@0 13 use Drupal\views\ViewExecutable;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Renders entity fields.
Chris@0 17 *
Chris@0 18 * This is used to build render arrays for all entity field values of a view
Chris@0 19 * result set sharing the same relationship. An entity translation renderer is
Chris@0 20 * used internally to handle entity language properly.
Chris@0 21 */
Chris@0 22 class EntityFieldRenderer extends RendererBase {
Chris@0 23 use EntityTranslationRenderTrait;
Chris@0 24 use DependencySerializationTrait;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The relationship being handled.
Chris@0 28 *
Chris@0 29 * @var string
Chris@0 30 */
Chris@0 31 protected $relationship;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * The entity manager.
Chris@0 35 *
Chris@0 36 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 37 */
Chris@0 38 protected $entityManager;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * A list of indexes of rows whose fields have already been rendered.
Chris@0 42 *
Chris@0 43 * @var int[]
Chris@0 44 */
Chris@0 45 protected $processedRows = [];
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Constructs an EntityFieldRenderer object.
Chris@0 49 *
Chris@0 50 * @param \Drupal\views\ViewExecutable $view
Chris@0 51 * The view whose fields are being rendered.
Chris@0 52 * @param string $relationship
Chris@0 53 * The relationship to be handled.
Chris@0 54 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 55 * The language manager.
Chris@0 56 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 57 * The entity type.
Chris@0 58 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 59 * The entity manager.
Chris@0 60 */
Chris@0 61 public function __construct(ViewExecutable $view, $relationship, LanguageManagerInterface $language_manager, EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager) {
Chris@0 62 parent::__construct($view, $language_manager, $entity_type);
Chris@0 63 $this->relationship = $relationship;
Chris@0 64 $this->entityManager = $entity_manager;
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * {@inheritdoc}
Chris@0 69 */
Chris@0 70 public function getCacheContexts() {
Chris@0 71 return $this->getEntityTranslationRenderer()->getCacheContexts();
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * {@inheritdoc}
Chris@0 76 */
Chris@0 77 public function getEntityTypeId() {
Chris@0 78 return $this->entityType->id();
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * {@inheritdoc}
Chris@0 83 */
Chris@0 84 protected function getEntityManager() {
Chris@0 85 return $this->entityManager;
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * {@inheritdoc}
Chris@0 90 */
Chris@0 91 protected function getLanguageManager() {
Chris@0 92 return $this->languageManager;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * {@inheritdoc}
Chris@0 97 */
Chris@0 98 protected function getView() {
Chris@0 99 return $this->view;
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * {@inheritdoc}
Chris@0 104 */
Chris@0 105 public function query(QueryPluginBase $query, $relationship = NULL) {
Chris@0 106 $this->getEntityTranslationRenderer()->query($query, $relationship);
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * Renders entity field data.
Chris@0 111 *
Chris@0 112 * @param \Drupal\views\ResultRow $row
Chris@0 113 * A single row of the query result.
Chris@0 114 * @param \Drupal\views\Plugin\views\field\EntityField $field
Chris@0 115 * (optional) A field to be rendered.
Chris@0 116 *
Chris@0 117 * @return array
Chris@0 118 * A renderable array for the entity data contained in the result row.
Chris@0 119 */
Chris@0 120 public function render(ResultRow $row, EntityField $field = NULL) {
Chris@0 121 // The method is called for each field in each result row. In order to
Chris@0 122 // leverage multiple-entity building of formatter output, we build the
Chris@0 123 // render arrays for all fields in all rows on the first call.
Chris@0 124 if (!isset($this->build)) {
Chris@0 125 $this->build = $this->buildFields($this->view->result);
Chris@0 126 }
Chris@0 127
Chris@0 128 if (isset($field)) {
Chris@0 129 $field_id = $field->options['id'];
Chris@0 130 // Pick the render array for the row / field we are being asked to render,
Chris@0 131 // and remove it from $this->build to free memory as we progress.
Chris@0 132 if (isset($this->build[$row->index][$field_id])) {
Chris@0 133 $build = $this->build[$row->index][$field_id];
Chris@0 134 unset($this->build[$row->index][$field_id]);
Chris@0 135 }
Chris@0 136 elseif (isset($this->build[$row->index])) {
Chris@0 137 // In the uncommon case where a field gets rendered several times
Chris@0 138 // (typically through direct Views API calls), the pre-computed render
Chris@0 139 // array was removed by the unset() above. We have to manually rebuild
Chris@0 140 // the render array for the row.
Chris@0 141 $build = $this->buildFields([$row])[$row->index][$field_id];
Chris@0 142 }
Chris@0 143 else {
Chris@0 144 // In case the relationship is optional, there might not be any fields
Chris@0 145 // to render for this row.
Chris@0 146 $build = [];
Chris@0 147 }
Chris@0 148 }
Chris@0 149 else {
Chris@0 150 // Same logic as above, in the case where we are being called for a whole
Chris@0 151 // row.
Chris@0 152 if (isset($this->build[$row->index])) {
Chris@0 153 $build = $this->build[$row->index];
Chris@0 154 unset($this->build[$row->index]);
Chris@0 155 }
Chris@0 156 else {
Chris@0 157 $build = $this->buildFields([$row])[$row->index];
Chris@0 158 }
Chris@0 159 }
Chris@0 160
Chris@0 161 return $build;
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * Builds the render arrays for all fields of all result rows.
Chris@0 166 *
Chris@0 167 * The output is built using EntityViewDisplay objects to leverage
Chris@0 168 * multiple-entity building and ensure a common code path with regular entity
Chris@0 169 * view.
Chris@0 170 * - Each relationship is handled by a separate EntityFieldRenderer instance,
Chris@0 171 * since it operates on its own set of entities. This also ensures different
Chris@0 172 * entity types are handled separately, as they imply different
Chris@0 173 * relationships.
Chris@0 174 * - Within each relationship, the fields to render are arranged in unique
Chris@0 175 * sets containing each field at most once (an EntityViewDisplay can
Chris@0 176 * only process a field once with given display options, but a View can
Chris@0 177 * contain the same field several times with different display options).
Chris@0 178 * - For each set of fields, entities are processed by bundle, so that
Chris@0 179 * formatters can operate on the proper field definition for the bundle.
Chris@0 180 *
Chris@0 181 * @param \Drupal\views\ResultRow[] $values
Chris@0 182 * An array of all ResultRow objects returned from the query.
Chris@0 183 *
Chris@0 184 * @return array
Chris@0 185 * A renderable array for the fields handled by this renderer.
Chris@0 186 *
Chris@0 187 * @see \Drupal\Core\Entity\Entity\EntityViewDisplay
Chris@0 188 */
Chris@0 189 protected function buildFields(array $values) {
Chris@0 190 $build = [];
Chris@0 191
Chris@0 192 if ($values && ($field_ids = $this->getRenderableFieldIds())) {
Chris@0 193 $entity_type_id = $this->getEntityTypeId();
Chris@0 194
Chris@0 195 // Collect the entities for the relationship, fetch the right translation,
Chris@0 196 // and group by bundle. For each result row, the corresponding entity can
Chris@0 197 // be obtained from any of the fields handlers, so we arbitrarily use the
Chris@0 198 // first one.
Chris@0 199 $entities_by_bundles = [];
Chris@0 200 $field = $this->view->field[current($field_ids)];
Chris@0 201 foreach ($values as $result_row) {
Chris@0 202 if ($entity = $field->getEntity($result_row)) {
Chris@0 203 $entities_by_bundles[$entity->bundle()][$result_row->index] = $this->getEntityTranslation($entity, $result_row);
Chris@0 204 }
Chris@0 205 }
Chris@0 206
Chris@0 207 // Determine unique sets of fields that can be processed by the same
Chris@0 208 // display. Fields that appear several times in the View open additional
Chris@0 209 // "overflow" displays.
Chris@0 210 $display_sets = [];
Chris@0 211 foreach ($field_ids as $field_id) {
Chris@0 212 $field = $this->view->field[$field_id];
Chris@0 213 $field_name = $field->definition['field_name'];
Chris@0 214 $index = 0;
Chris@0 215 while (isset($display_sets[$index]['field_names'][$field_name])) {
Chris@0 216 $index++;
Chris@0 217 }
Chris@0 218 $display_sets[$index]['field_names'][$field_name] = $field;
Chris@0 219 $display_sets[$index]['field_ids'][$field_id] = $field;
Chris@0 220 }
Chris@0 221
Chris@0 222 // For each set of fields, build the output by bundle.
Chris@0 223 foreach ($display_sets as $display_fields) {
Chris@0 224 foreach ($entities_by_bundles as $bundle => $bundle_entities) {
Chris@0 225 // Create the display, and configure the field display options.
Chris@0 226 $display = EntityViewDisplay::create([
Chris@0 227 'targetEntityType' => $entity_type_id,
Chris@0 228 'bundle' => $bundle,
Chris@0 229 'status' => TRUE,
Chris@0 230 ]);
Chris@0 231 foreach ($display_fields['field_ids'] as $field) {
Chris@0 232 $display->setComponent($field->definition['field_name'], [
Chris@0 233 'type' => $field->options['type'],
Chris@0 234 'settings' => $field->options['settings'],
Chris@0 235 ]);
Chris@0 236 }
Chris@0 237 // Let the display build the render array for the entities.
Chris@0 238 $display_build = $display->buildMultiple($bundle_entities);
Chris@0 239 // Collect the field render arrays and index them using our internal
Chris@0 240 // row indexes and field IDs.
Chris@0 241 foreach ($display_build as $row_index => $entity_build) {
Chris@0 242 foreach ($display_fields['field_ids'] as $field_id => $field) {
Chris@0 243 $build[$row_index][$field_id] = !empty($entity_build[$field->definition['field_name']]) ? $entity_build[$field->definition['field_name']] : [];
Chris@0 244 }
Chris@0 245 }
Chris@0 246 }
Chris@0 247 }
Chris@0 248 }
Chris@0 249
Chris@0 250 return $build;
Chris@0 251 }
Chris@0 252
Chris@0 253 /**
Chris@0 254 * Returns a list of names of entity fields to be rendered.
Chris@0 255 *
Chris@0 256 * @return string[]
Chris@0 257 * An associative array of views fields.
Chris@0 258 */
Chris@0 259 protected function getRenderableFieldIds() {
Chris@0 260 $field_ids = [];
Chris@0 261 foreach ($this->view->field as $field_id => $field) {
Chris@0 262 if ($field instanceof EntityField && $field->relationship == $this->relationship) {
Chris@0 263 $field_ids[] = $field_id;
Chris@0 264 }
Chris@0 265 }
Chris@0 266 return $field_ids;
Chris@0 267 }
Chris@0 268
Chris@0 269 }