3 namespace Drupal\entity_test\Controller;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Controller\ControllerBase;
9 * Controller routines for entity_test routes.
11 class EntityTestController extends ControllerBase {
14 * Returns an empty page.
16 * @see \Drupal\entity_test\Routing\EntityTestRoutes::routes()
18 public function testAdmin() {
23 * List entity_test entities referencing the given entity.
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.
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) {
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');
55 * List entities of the given entity type labels, sorted alphabetically.
57 * @param string $entity_type_id
58 * The type of the entity being listed.
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();
67 // Sort by label field, if any.
68 if ($label_field = $entity_type_definition->getKey('label')) {
69 $query->sort($label_field);
72 $entities = $this->entityManager()
73 ->getStorage($entity_type_id)
74 ->loadMultiple($query->execute());
78 foreach ($entities as $entity) {
79 $labels[] = $entity->label();
80 $cache_tags = Cache::mergeTags($cache_tags, $entity->getCacheTags());
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());
88 '#theme' => 'item_list',
90 '#title' => $entity_type_id . ' entities',
92 'contexts' => $entity_type_definition->getListCacheContexts(),
93 'tags' => $cache_tags,
100 * Empty list of entities of the given entity type.
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.
106 * @param string $entity_type_id
107 * The type of the entity being listed.
110 * A renderable array.
112 public function listEntitiesEmpty($entity_type_id) {
113 $entity_type_definition = $this->entityManager()->getDefinition($entity_type_id);
115 '#theme' => 'item_list',
118 'contexts' => $entity_type_definition->getListCacheContexts(),
119 'tags' => $entity_type_definition->getListCacheTags(),