EntityTestController.php in Drupal 8
File
core/modules/system/tests/modules/entity_test/src/Controller/EntityTestController.php
View source
<?php
namespace Drupal\entity_test\Controller;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerBase;
class EntityTestController extends ControllerBase {
public function testAdmin() {
return [];
}
public function listReferencingEntities($entity_reference_field_name, $referenced_entity_type, $referenced_entity_id) {
$referenced_entity = $this
->entityTypeManager()
->getStorage($referenced_entity_type)
->load($referenced_entity_id);
if ($referenced_entity === NULL) {
return [];
}
$query = $this
->entityTypeManager()
->getStorage('entity_test')
->getQuery()
->condition($entity_reference_field_name . '.target_id', $referenced_entity_id);
$entities = $this
->entityTypeManager()
->getStorage('entity_test')
->loadMultiple($query
->execute());
return $this
->entityTypeManager()
->getViewBuilder('entity_test')
->viewMultiple($entities, 'full');
}
public function listEntitiesAlphabetically($entity_type_id) {
$entity_type_definition = $this
->entityTypeManager()
->getDefinition($entity_type_id);
$query = $this
->entityTypeManager()
->getStorage($entity_type_id)
->getQuery();
if ($label_field = $entity_type_definition
->getKey('label')) {
$query
->sort($label_field);
}
$entities = $this
->entityTypeManager()
->getStorage($entity_type_id)
->loadMultiple($query
->execute());
$cache_tags = [];
$labels = [];
foreach ($entities as $entity) {
$labels[] = $entity
->label();
$cache_tags = Cache::mergeTags($cache_tags, $entity
->getCacheTags());
}
$cache_tags = Cache::mergeTags($cache_tags, $entity_type_definition
->getListCacheTags());
return [
'#theme' => 'item_list',
'#items' => $labels,
'#title' => $entity_type_id . ' entities',
'#cache' => [
'contexts' => $entity_type_definition
->getListCacheContexts(),
'tags' => $cache_tags,
],
];
}
public function listEntitiesEmpty($entity_type_id) {
$entity_type_definition = $this
->entityTypeManager()
->getDefinition($entity_type_id);
return [
'#theme' => 'item_list',
'#items' => [],
'#cache' => [
'contexts' => $entity_type_definition
->getListCacheContexts(),
'tags' => $entity_type_definition
->getListCacheTags(),
],
];
}
}