annotate core/modules/views/src/Entity/Render/EntityFieldRenderer.php @ 19:fa3358dc1485 tip

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