comparison core/lib/Drupal/Core/Entity/EntityViewBuilder.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children c2387f117808
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
9 use Drupal\Core\Field\FieldItemInterface; 9 use Drupal\Core\Field\FieldItemInterface;
10 use Drupal\Core\Field\FieldItemListInterface; 10 use Drupal\Core\Field\FieldItemListInterface;
11 use Drupal\Core\Language\LanguageManagerInterface; 11 use Drupal\Core\Language\LanguageManagerInterface;
12 use Drupal\Core\Render\Element; 12 use Drupal\Core\Render\Element;
13 use Drupal\Core\Theme\Registry; 13 use Drupal\Core\Theme\Registry;
14 use Drupal\Core\TypedData\TranslatableInterface; 14 use Drupal\Core\TypedData\TranslatableInterface as TranslatableDataInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface; 15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 16
17 /** 17 /**
18 * Base class for entity view builders. 18 * Base class for entity view builders.
19 * 19 *
187 $view_mode, 187 $view_mode,
188 ], 188 ],
189 'bin' => $this->cacheBin, 189 'bin' => $this->cacheBin,
190 ]; 190 ];
191 191
192 if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) { 192 if ($entity instanceof TranslatableDataInterface && count($entity->getTranslationLanguages()) > 1) {
193 $build['#cache']['keys'][] = $entity->language()->getId(); 193 $build['#cache']['keys'][] = $entity->language()->getId();
194 } 194 }
195 } 195 }
196 196
197 return $build; 197 return $build;
267 $display = $displays[$entity->bundle()]; 267 $display = $displays[$entity->bundle()];
268 268
269 $this->moduleHandler()->invokeAll($view_hook, [&$build_list[$key], $entity, $display, $view_mode]); 269 $this->moduleHandler()->invokeAll($view_hook, [&$build_list[$key], $entity, $display, $view_mode]);
270 $this->moduleHandler()->invokeAll('entity_view', [&$build_list[$key], $entity, $display, $view_mode]); 270 $this->moduleHandler()->invokeAll('entity_view', [&$build_list[$key], $entity, $display, $view_mode]);
271 271
272 $this->addContextualLinks($build_list[$key], $entity);
272 $this->alterBuild($build_list[$key], $entity, $display, $view_mode); 273 $this->alterBuild($build_list[$key], $entity, $display, $view_mode);
273 274
274 // Assign the weights configured in the display. 275 // Assign the weights configured in the display.
275 // @todo: Once https://www.drupal.org/node/1875974 provides the missing 276 // @todo: Once https://www.drupal.org/node/1875974 provides the missing
276 // API, only do it for 'extra fields', since other components have 277 // API, only do it for 'extra fields', since other components have
318 // Let the displays build their render arrays. 319 // Let the displays build their render arrays.
319 foreach ($entities_by_bundle as $bundle => $bundle_entities) { 320 foreach ($entities_by_bundle as $bundle => $bundle_entities) {
320 $display_build = $displays[$bundle]->buildMultiple($bundle_entities); 321 $display_build = $displays[$bundle]->buildMultiple($bundle_entities);
321 foreach ($bundle_entities as $id => $entity) { 322 foreach ($bundle_entities as $id => $entity) {
322 $build[$id] += $display_build[$id]; 323 $build[$id] += $display_build[$id];
324 }
325 }
326 }
327
328 /**
329 * Add contextual links.
330 *
331 * @param array $build
332 * The render array that is being created.
333 * @param \Drupal\Core\Entity\EntityInterface $entity
334 * The entity to be prepared.
335 */
336 protected function addContextualLinks(array &$build, EntityInterface $entity) {
337 if ($entity->isNew()) {
338 return;
339 }
340 $key = $entity->getEntityTypeId();
341 $rel = 'canonical';
342 if ($entity instanceof ContentEntityInterface && !$entity->isDefaultRevision()) {
343 $rel = 'revision';
344 $key .= '_revision';
345 }
346 if ($entity->hasLinkTemplate($rel)) {
347 $build['#contextual_links'][$key] = [
348 'route_parameters' => $entity->toUrl($rel)->getRouteParameters(),
349 ];
350 if ($entity instanceof EntityChangedInterface) {
351 $build['#contextual_links'][$key]['metadata'] = [
352 'changed' => $entity->getChangedTime(),
353 ];
323 } 354 }
324 } 355 }
325 } 356 }
326 357
327 /** 358 /**