Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / modules / entity_test / src / Controller / EntityTestController.php
1 <?php
2
3 namespace Drupal\entity_test\Controller;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Controller\ControllerBase;
7
8 /**
9  * Controller routines for entity_test routes.
10  */
11 class EntityTestController extends ControllerBase {
12
13   /**
14    * Returns an empty page.
15    *
16    * @see \Drupal\entity_test\Routing\EntityTestRoutes::routes()
17    */
18   public function testAdmin() {
19     return [];
20   }
21
22   /**
23    * List entity_test entities referencing the given entity.
24    *
25    * @param string $entity_reference_field_name
26    *   The name of the entity_reference field to use in the query.
27    * @param string $referenced_entity_type
28    *   The type of the entity being referenced.
29    * @param int $referenced_entity_id
30    *   The ID of the entity being referenced.
31    *
32    * @return array
33    *   A renderable array.
34    */
35   public function listReferencingEntities($entity_reference_field_name, $referenced_entity_type, $referenced_entity_id) {
36     // Early return if the referenced entity does not exist (or is deleted).
37     $referenced_entity = $this->entityManager()
38       ->getStorage($referenced_entity_type)
39       ->load($referenced_entity_id);
40     if ($referenced_entity === NULL) {
41       return [];
42     }
43
44     $query = $this->entityTypeManager()->getStorage('entity_test')->getQuery()
45       ->condition($entity_reference_field_name . '.target_id', $referenced_entity_id);
46     $entities = $this->entityManager()
47       ->getStorage('entity_test')
48       ->loadMultiple($query->execute());
49     return $this->entityManager()
50       ->getViewBuilder('entity_test')
51       ->viewMultiple($entities, 'full');
52   }
53
54   /**
55    * List entities of the given entity type labels, sorted alphabetically.
56    *
57    * @param string $entity_type_id
58    *   The type of the entity being listed.
59    *
60    * @return array
61    *   A renderable array.
62    */
63   public function listEntitiesAlphabetically($entity_type_id) {
64     $entity_type_definition = $this->entityManager()->getDefinition($entity_type_id);
65     $query = $this->entityTypeManager()->getStorage($entity_type_id)->getQuery();
66
67     // Sort by label field, if any.
68     if ($label_field = $entity_type_definition->getKey('label')) {
69       $query->sort($label_field);
70     }
71
72     $entities = $this->entityManager()
73       ->getStorage($entity_type_id)
74       ->loadMultiple($query->execute());
75
76     $cache_tags = [];
77     $labels = [];
78     foreach ($entities as $entity) {
79       $labels[] = $entity->label();
80       $cache_tags = Cache::mergeTags($cache_tags, $entity->getCacheTags());
81     }
82     // Always associate the list cache tag, otherwise the cached empty result
83     // wouldn't be invalidated. This would continue to show nothing matches the
84     // query, even though a newly created entity might match the query.
85     $cache_tags = Cache::mergeTags($cache_tags, $entity_type_definition->getListCacheTags());
86
87     return [
88       '#theme' => 'item_list',
89       '#items' => $labels,
90       '#title' => $entity_type_id . ' entities',
91       '#cache' => [
92         'contexts' => $entity_type_definition->getListCacheContexts(),
93         'tags' => $cache_tags,
94       ],
95     ];
96   }
97
98
99   /**
100    * Empty list of entities of the given entity type.
101    *
102    * Empty because no entities match the query. That may seem contrived, but it
103    * is an excellent way for testing whether an entity's list cache tags are
104    * working as expected.
105    *
106    * @param string $entity_type_id
107    *   The type of the entity being listed.
108    *
109    * @return array
110    *   A renderable array.
111    */
112   public function listEntitiesEmpty($entity_type_id) {
113     $entity_type_definition = $this->entityManager()->getDefinition($entity_type_id);
114     return [
115       '#theme' => 'item_list',
116       '#items' => [],
117       '#cache' => [
118         'contexts' => $entity_type_definition->getListCacheContexts(),
119         'tags' => $entity_type_definition->getListCacheTags(),
120       ],
121     ];
122   }
123
124 }