EntityViewBuilder.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Component\Utility\Crypt;
  4. use Drupal\Core\Cache\Cache;
  5. use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
  6. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  7. use Drupal\Core\Field\FieldItemInterface;
  8. use Drupal\Core\Field\FieldItemListInterface;
  9. use Drupal\Core\Language\LanguageManagerInterface;
  10. use Drupal\Core\Render\Element;
  11. use Drupal\Core\Theme\Registry;
  12. use Drupal\Core\TypedData\TranslatableInterface as TranslatableDataInterface;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. /**
  15. * Base class for entity view builders.
  16. *
  17. * @ingroup entity_api
  18. */
  19. class EntityViewBuilder extends EntityHandlerBase implements EntityHandlerInterface, EntityViewBuilderInterface {
  20. /**
  21. * The type of entities for which this view builder is instantiated.
  22. *
  23. * @var string
  24. */
  25. protected $entityTypeId;
  26. /**
  27. * Information about the entity type.
  28. *
  29. * @var \Drupal\Core\Entity\EntityTypeInterface
  30. */
  31. protected $entityType;
  32. /**
  33. * The entity manager service.
  34. *
  35. * @var \Drupal\Core\Entity\EntityManagerInterface
  36. */
  37. protected $entityManager;
  38. /**
  39. * The cache bin used to store the render cache.
  40. *
  41. * @var string
  42. */
  43. protected $cacheBin = 'render';
  44. /**
  45. * The language manager.
  46. *
  47. * @var \Drupal\Core\Language\LanguageManagerInterface
  48. */
  49. protected $languageManager;
  50. /**
  51. * The theme registry.
  52. *
  53. * @var \Drupal\Core\Theme\Registry
  54. */
  55. protected $themeRegistry;
  56. /**
  57. * The EntityViewDisplay objects created for individual field rendering.
  58. *
  59. * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface[]
  60. *
  61. * @see \Drupal\Core\Entity\EntityViewBuilder::getSingleFieldDisplay()
  62. */
  63. protected $singleFieldDisplays;
  64. /**
  65. * Constructs a new EntityViewBuilder.
  66. *
  67. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  68. * The entity type definition.
  69. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  70. * The entity manager service.
  71. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  72. * The language manager.
  73. * @param \Drupal\Core\Theme\Registry $theme_registry
  74. * The theme registry.
  75. */
  76. public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, Registry $theme_registry = NULL) {
  77. $this->entityTypeId = $entity_type->id();
  78. $this->entityType = $entity_type;
  79. $this->entityManager = $entity_manager;
  80. $this->languageManager = $language_manager;
  81. $this->themeRegistry = $theme_registry ?: \Drupal::service('theme.registry');
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  87. return new static(
  88. $entity_type,
  89. $container->get('entity.manager'),
  90. $container->get('language_manager'),
  91. $container->get('theme.registry')
  92. );
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
  98. $build_list = $this->viewMultiple([$entity], $view_mode, $langcode);
  99. // The default ::buildMultiple() #pre_render callback won't run, because we
  100. // extract a child element of the default renderable array. Thus we must
  101. // assign an alternative #pre_render callback that applies the necessary
  102. // transformations and then still calls ::buildMultiple().
  103. $build = $build_list[0];
  104. $build['#pre_render'][] = [$this, 'build'];
  105. return $build;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
  111. $build_list = [
  112. '#sorted' => TRUE,
  113. '#pre_render' => [[$this, 'buildMultiple']],
  114. ];
  115. $weight = 0;
  116. foreach ($entities as $key => $entity) {
  117. // Ensure that from now on we are dealing with the proper translation
  118. // object.
  119. $entity = $this->entityManager->getTranslationFromContext($entity, $langcode);
  120. // Set build defaults.
  121. $build_list[$key] = $this->getBuildDefaults($entity, $view_mode);
  122. $entityType = $this->entityTypeId;
  123. $this->moduleHandler()->alter([$entityType . '_build_defaults', 'entity_build_defaults'], $build_list[$key], $entity, $view_mode);
  124. $build_list[$key]['#weight'] = $weight++;
  125. }
  126. return $build_list;
  127. }
  128. /**
  129. * Provides entity-specific defaults to the build process.
  130. *
  131. * @param \Drupal\Core\Entity\EntityInterface $entity
  132. * The entity for which the defaults should be provided.
  133. * @param string $view_mode
  134. * The view mode that should be used.
  135. *
  136. * @return array
  137. */
  138. protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
  139. // Allow modules to change the view mode.
  140. $context = [];
  141. $this->moduleHandler()->alter('entity_view_mode', $view_mode, $entity, $context);
  142. $build = [
  143. "#{$this->entityTypeId}" => $entity,
  144. '#view_mode' => $view_mode,
  145. // Collect cache defaults for this entity.
  146. '#cache' => [
  147. 'tags' => Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags()),
  148. 'contexts' => $entity->getCacheContexts(),
  149. 'max-age' => $entity->getCacheMaxAge(),
  150. ],
  151. ];
  152. // Add the default #theme key if a template exists for it.
  153. if ($this->themeRegistry->getRuntime()->has($this->entityTypeId)) {
  154. $build['#theme'] = $this->entityTypeId;
  155. }
  156. // Cache the rendered output if permitted by the view mode and global entity
  157. // type configuration.
  158. if ($this->isViewModeCacheable($view_mode) && !$entity->isNew() && $entity->isDefaultRevision() && $this->entityType->isRenderCacheable()) {
  159. $build['#cache'] += [
  160. 'keys' => [
  161. 'entity_view',
  162. $this->entityTypeId,
  163. $entity->id(),
  164. $view_mode,
  165. ],
  166. 'bin' => $this->cacheBin,
  167. ];
  168. if ($entity instanceof TranslatableDataInterface && count($entity->getTranslationLanguages()) > 1) {
  169. $build['#cache']['keys'][] = $entity->language()->getId();
  170. }
  171. }
  172. return $build;
  173. }
  174. /**
  175. * Builds an entity's view; augments entity defaults.
  176. *
  177. * This function is assigned as a #pre_render callback in ::view().
  178. *
  179. * It transforms the renderable array for a single entity to the same
  180. * structure as if we were rendering multiple entities, and then calls the
  181. * default ::buildMultiple() #pre_render callback.
  182. *
  183. * @param array $build
  184. * A renderable array containing build information and context for an entity
  185. * view.
  186. *
  187. * @return array
  188. * The updated renderable array.
  189. *
  190. * @see \Drupal\Core\Render\RendererInterface::render()
  191. */
  192. public function build(array $build) {
  193. $build_list = [$build];
  194. $build_list = $this->buildMultiple($build_list);
  195. return $build_list[0];
  196. }
  197. /**
  198. * Builds multiple entities' views; augments entity defaults.
  199. *
  200. * This function is assigned as a #pre_render callback in ::viewMultiple().
  201. *
  202. * By delaying the building of an entity until the #pre_render processing in
  203. * drupal_render(), the processing cost of assembling an entity's renderable
  204. * array is saved on cache-hit requests.
  205. *
  206. * @param array $build_list
  207. * A renderable array containing build information and context for an
  208. * entity view.
  209. *
  210. * @return array
  211. * The updated renderable array.
  212. *
  213. * @see \Drupal\Core\Render\RendererInterface::render()
  214. */
  215. public function buildMultiple(array $build_list) {
  216. // Build the view modes and display objects.
  217. $view_modes = [];
  218. $entity_type_key = "#{$this->entityTypeId}";
  219. $view_hook = "{$this->entityTypeId}_view";
  220. // Find the keys for the ContentEntities in the build; Store entities for
  221. // rendering by view_mode.
  222. $children = Element::children($build_list);
  223. foreach ($children as $key) {
  224. if (isset($build_list[$key][$entity_type_key])) {
  225. $entity = $build_list[$key][$entity_type_key];
  226. if ($entity instanceof FieldableEntityInterface) {
  227. $view_modes[$build_list[$key]['#view_mode']][$key] = $entity;
  228. }
  229. }
  230. }
  231. // Build content for the displays represented by the entities.
  232. foreach ($view_modes as $view_mode => $view_mode_entities) {
  233. $displays = EntityViewDisplay::collectRenderDisplays($view_mode_entities, $view_mode);
  234. $this->buildComponents($build_list, $view_mode_entities, $displays, $view_mode);
  235. foreach (array_keys($view_mode_entities) as $key) {
  236. // Allow for alterations while building, before rendering.
  237. $entity = $build_list[$key][$entity_type_key];
  238. $display = $displays[$entity->bundle()];
  239. $this->moduleHandler()->invokeAll($view_hook, [&$build_list[$key], $entity, $display, $view_mode]);
  240. $this->moduleHandler()->invokeAll('entity_view', [&$build_list[$key], $entity, $display, $view_mode]);
  241. $this->addContextualLinks($build_list[$key], $entity);
  242. $this->alterBuild($build_list[$key], $entity, $display, $view_mode);
  243. // Assign the weights configured in the display.
  244. // @todo: Once https://www.drupal.org/node/1875974 provides the missing
  245. // API, only do it for 'extra fields', since other components have
  246. // been taken care of in EntityViewDisplay::buildMultiple().
  247. foreach ($display->getComponents() as $name => $options) {
  248. if (isset($build_list[$key][$name])) {
  249. $build_list[$key][$name]['#weight'] = $options['weight'];
  250. }
  251. }
  252. // Allow modules to modify the render array.
  253. $this->moduleHandler()->alter([$view_hook, 'entity_view'], $build_list[$key], $entity, $display);
  254. }
  255. }
  256. return $build_list;
  257. }
  258. /**
  259. * {@inheritdoc}
  260. */
  261. public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
  262. $entities_by_bundle = [];
  263. foreach ($entities as $id => $entity) {
  264. // Initialize the field item attributes for the fields being displayed.
  265. // The entity can include fields that are not displayed, and the display
  266. // can include components that are not fields, so we want to act on the
  267. // intersection. However, the entity can have many more fields than are
  268. // displayed, so we avoid the cost of calling $entity->getProperties()
  269. // by iterating the intersection as follows.
  270. foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) {
  271. if ($entity->hasField($name)) {
  272. foreach ($entity->get($name) as $item) {
  273. $item->_attributes = [];
  274. }
  275. }
  276. }
  277. // Group the entities by bundle.
  278. $entities_by_bundle[$entity->bundle()][$id] = $entity;
  279. }
  280. // Invoke hook_entity_prepare_view().
  281. $this->moduleHandler()->invokeAll('entity_prepare_view', [$this->entityTypeId, $entities, $displays, $view_mode]);
  282. // Let the displays build their render arrays.
  283. foreach ($entities_by_bundle as $bundle => $bundle_entities) {
  284. $display_build = $displays[$bundle]->buildMultiple($bundle_entities);
  285. foreach ($bundle_entities as $id => $entity) {
  286. $build[$id] += $display_build[$id];
  287. }
  288. }
  289. }
  290. /**
  291. * Add contextual links.
  292. *
  293. * @param array $build
  294. * The render array that is being created.
  295. * @param \Drupal\Core\Entity\EntityInterface $entity
  296. * The entity to be prepared.
  297. */
  298. protected function addContextualLinks(array &$build, EntityInterface $entity) {
  299. if ($entity->isNew()) {
  300. return;
  301. }
  302. $key = $entity->getEntityTypeId();
  303. $rel = 'canonical';
  304. if ($entity instanceof ContentEntityInterface && !$entity->isDefaultRevision()) {
  305. $rel = 'revision';
  306. $key .= '_revision';
  307. }
  308. if ($entity->hasLinkTemplate($rel)) {
  309. $build['#contextual_links'][$key] = [
  310. 'route_parameters' => $entity->toUrl($rel)->getRouteParameters(),
  311. ];
  312. if ($entity instanceof EntityChangedInterface) {
  313. $build['#contextual_links'][$key]['metadata'] = [
  314. 'changed' => $entity->getChangedTime(),
  315. ];
  316. }
  317. }
  318. }
  319. /**
  320. * Specific per-entity building.
  321. *
  322. * @param array $build
  323. * The render array that is being created.
  324. * @param \Drupal\Core\Entity\EntityInterface $entity
  325. * The entity to be prepared.
  326. * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
  327. * The entity view display holding the display options configured for the
  328. * entity components.
  329. * @param string $view_mode
  330. * The view mode that should be used to prepare the entity.
  331. */
  332. protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {}
  333. /**
  334. * {@inheritdoc}
  335. */
  336. public function getCacheTags() {
  337. return [$this->entityTypeId . '_view'];
  338. }
  339. /**
  340. * {@inheritdoc}
  341. */
  342. public function resetCache(array $entities = NULL) {
  343. // If no set of specific entities is provided, invalidate the entity view
  344. // builder's cache tag. This will invalidate all entities rendered by this
  345. // view builder.
  346. // Otherwise, if a set of specific entities is provided, invalidate those
  347. // specific entities only, plus their list cache tags, because any lists in
  348. // which these entities are rendered, must be invalidated as well. However,
  349. // even in this case, we might invalidate more cache items than necessary.
  350. // When we have a way to invalidate only those cache items that have both
  351. // the individual entity's cache tag and the view builder's cache tag, we'll
  352. // be able to optimize this further.
  353. if (isset($entities)) {
  354. $tags = [];
  355. foreach ($entities as $entity) {
  356. $tags = Cache::mergeTags($tags, $entity->getCacheTags());
  357. $tags = Cache::mergeTags($tags, $entity->getEntityType()->getListCacheTags());
  358. }
  359. Cache::invalidateTags($tags);
  360. }
  361. else {
  362. Cache::invalidateTags($this->getCacheTags());
  363. }
  364. }
  365. /**
  366. * Determines whether the view mode is cacheable.
  367. *
  368. * @param string $view_mode
  369. * Name of the view mode that should be rendered.
  370. *
  371. * @return bool
  372. * TRUE if the view mode can be cached, FALSE otherwise.
  373. */
  374. protected function isViewModeCacheable($view_mode) {
  375. if ($view_mode == 'default') {
  376. // The 'default' is not an actual view mode.
  377. return TRUE;
  378. }
  379. $view_modes_info = $this->entityManager->getViewModes($this->entityTypeId);
  380. return !empty($view_modes_info[$view_mode]['cache']);
  381. }
  382. /**
  383. * {@inheritdoc}
  384. */
  385. public function viewField(FieldItemListInterface $items, $display_options = []) {
  386. $entity = $items->getEntity();
  387. $field_name = $items->getFieldDefinition()->getName();
  388. $display = $this->getSingleFieldDisplay($entity, $field_name, $display_options);
  389. $output = [];
  390. $build = $display->build($entity);
  391. if (isset($build[$field_name])) {
  392. $output = $build[$field_name];
  393. }
  394. return $output;
  395. }
  396. /**
  397. * {@inheritdoc}
  398. */
  399. public function viewFieldItem(FieldItemInterface $item, $display = []) {
  400. $entity = $item->getEntity();
  401. $field_name = $item->getFieldDefinition()->getName();
  402. // Clone the entity since we are going to modify field values.
  403. $clone = clone $entity;
  404. // Push the item as the single value for the field, and defer to viewField()
  405. // to build the render array for the whole list.
  406. $clone->{$field_name}->setValue([$item->getValue()]);
  407. $elements = $this->viewField($clone->{$field_name}, $display);
  408. // Extract the part of the render array we need.
  409. $output = isset($elements[0]) ? $elements[0] : [];
  410. if (isset($elements['#access'])) {
  411. $output['#access'] = $elements['#access'];
  412. }
  413. return $output;
  414. }
  415. /**
  416. * Gets an EntityViewDisplay for rendering an individual field.
  417. *
  418. * @param \Drupal\Core\Entity\EntityInterface $entity
  419. * The entity.
  420. * @param string $field_name
  421. * The field name.
  422. * @param string|array $display_options
  423. * The display options passed to the viewField() method.
  424. *
  425. * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
  426. */
  427. protected function getSingleFieldDisplay($entity, $field_name, $display_options) {
  428. if (is_string($display_options)) {
  429. // View mode: use the Display configured for the view mode.
  430. $view_mode = $display_options;
  431. $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
  432. // Hide all fields except the current one.
  433. foreach (array_keys($entity->getFieldDefinitions()) as $name) {
  434. if ($name != $field_name) {
  435. $display->removeComponent($name);
  436. }
  437. }
  438. }
  439. else {
  440. // Array of custom display options: use a runtime Display for the
  441. // '_custom' view mode. Persist the displays created, to reduce the number
  442. // of objects (displays and formatter plugins) created when rendering a
  443. // series of fields individually for cases such as views tables.
  444. $entity_type_id = $entity->getEntityTypeId();
  445. $bundle = $entity->bundle();
  446. $key = $entity_type_id . ':' . $bundle . ':' . $field_name . ':' . Crypt::hashBase64(serialize($display_options));
  447. if (!isset($this->singleFieldDisplays[$key])) {
  448. $this->singleFieldDisplays[$key] = EntityViewDisplay::create([
  449. 'targetEntityType' => $entity_type_id,
  450. 'bundle' => $bundle,
  451. 'status' => TRUE,
  452. ])->setComponent($field_name, $display_options);
  453. }
  454. $display = $this->singleFieldDisplays[$key];
  455. }
  456. return $display;
  457. }
  458. }