Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
6 use Drupal\Core\Render\Element;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides helper methods to deal with building entity views in tests.
|
Chris@0
|
10 */
|
Chris@0
|
11 trait EntityViewTrait {
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Builds the renderable view of an entity.
|
Chris@0
|
15 *
|
Chris@0
|
16 * Entities postpone the composition of their renderable arrays to #pre_render
|
Chris@0
|
17 * functions in order to maximize cache efficacy. This means that the full
|
Chris@0
|
18 * renderable array for an entity is constructed in drupal_render(). Some
|
Chris@0
|
19 * tests require the complete renderable array for an entity outside of the
|
Chris@0
|
20 * drupal_render process in order to verify the presence of specific values.
|
Chris@0
|
21 * This method isolates the steps in the render process that produce an
|
Chris@0
|
22 * entity's renderable array.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
25 * The entity to prepare a renderable array for.
|
Chris@0
|
26 * @param string $view_mode
|
Chris@0
|
27 * (optional) The view mode that should be used to build the entity.
|
Chris@0
|
28 * @param null $langcode
|
Chris@0
|
29 * (optional) For which language the entity should be prepared, defaults to
|
Chris@0
|
30 * the current content language.
|
Chris@0
|
31 * @param bool $reset
|
Chris@0
|
32 * (optional) Whether to clear the cache for this entity.
|
Chris@0
|
33 * @return array
|
Chris@0
|
34 *
|
Chris@16
|
35 * @see \Drupal\Core\Render\RendererInterface::render()
|
Chris@0
|
36 */
|
Chris@0
|
37 protected function buildEntityView(EntityInterface $entity, $view_mode = 'full', $langcode = NULL, $reset = FALSE) {
|
Chris@0
|
38 $ensure_fully_built = function (&$elements) use (&$ensure_fully_built) {
|
Chris@0
|
39 // If the default values for this element have not been loaded yet, populate
|
Chris@0
|
40 // them.
|
Chris@0
|
41 if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) {
|
Chris@0
|
42 $elements += \Drupal::service('element_info')->getInfo($elements['#type']);
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 // Make any final changes to the element before it is rendered. This means
|
Chris@0
|
46 // that the $element or the children can be altered or corrected before the
|
Chris@0
|
47 // element is rendered into the final text.
|
Chris@0
|
48 if (isset($elements['#pre_render'])) {
|
Chris@0
|
49 foreach ($elements['#pre_render'] as $callable) {
|
Chris@0
|
50 $elements = call_user_func($callable, $elements);
|
Chris@0
|
51 }
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 // And recurse.
|
Chris@0
|
55 $children = Element::children($elements, TRUE);
|
Chris@0
|
56 foreach ($children as $key) {
|
Chris@0
|
57 $ensure_fully_built($elements[$key]);
|
Chris@0
|
58 }
|
Chris@0
|
59 };
|
Chris@0
|
60
|
Chris@0
|
61 $render_controller = $this->container->get('entity.manager')->getViewBuilder($entity->getEntityTypeId());
|
Chris@0
|
62 if ($reset) {
|
Chris@0
|
63 $render_controller->resetCache([$entity->id()]);
|
Chris@0
|
64 }
|
Chris@0
|
65 $build = $render_controller->view($entity, $view_mode, $langcode);
|
Chris@0
|
66 $ensure_fully_built($build);
|
Chris@0
|
67
|
Chris@0
|
68 return $build;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 }
|