EntityDebugController.php
Namespace
Drupal\devel\Controller
File
-
src/Controller/EntityDebugController.php
View source
<?php
namespace Drupal\devel\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\devel\DevelDumperManagerInterface;
use Drupal\path_alias\PathAliasStorage;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityDebugController extends ControllerBase {
protected DevelDumperManagerInterface $dumper;
protected TranslationManager $translationManager;
protected PathAliasStorage $aliasStorage;
public static function create(ContainerInterface $container) : static {
$entityTypeManager = $container->get('entity_type.manager');
$instance = parent::create($container);
$instance->dumper = $container->get('devel.dumper');
$instance->entityTypeManager = $entityTypeManager;
$instance->translationManager = $container->get('string_translation');
$instance->aliasStorage = $entityTypeManager->getStorage('path_alias');
return $instance;
}
public function entityTypeDefinition(RouteMatchInterface $route_match) : array {
$entity = $this->getEntityFromRouteMatch($route_match);
if (!$entity instanceof EntityInterface) {
return [];
}
return $this->dumper
->exportAsRenderable($entity->getEntityType());
}
public function entityLoad(RouteMatchInterface $route_match) : array {
$output = [];
$entity = $this->getEntityWithFieldDefinitions($route_match);
if ($entity instanceof EntityInterface) {
if ($entity instanceof FieldableEntityInterface) {
$entity->getFieldDefinitions();
}
$output = $this->dumper
->exportAsRenderable($entity);
}
return $output;
}
public function entityLoadWithReferences(RouteMatchInterface $route_match) : array {
$entity = $this->getEntityWithFieldDefinitions($route_match);
if (!$entity instanceof EntityInterface) {
return [];
}
return $this->dumper
->exportAsRenderable($entity, NULL, NULL, TRUE);
}
public function entityRender(RouteMatchInterface $route_match) : array {
$output = [];
$entity = $this->getEntityFromRouteMatch($route_match);
if ($entity instanceof EntityInterface) {
$entity_type_id = $entity->getEntityTypeId();
$view_hook = $entity_type_id . '_view';
$build = [];
if (function_exists($view_hook)) {
$build = $view_hook($entity);
}
elseif ($this->entityTypeManager
->hasHandler($entity_type_id, 'view_builder')) {
$build = $this->entityTypeManager
->getViewBuilder($entity_type_id)
->view($entity);
}
$output = $this->dumper
->exportAsRenderable($build);
}
return $output;
}
public function pathAliases(RouteMatchInterface $route_match) : array {
$entity = $this->getEntityFromRouteMatch($route_match);
if ($entity === NULL) {
return [];
}
$path = sprintf('/%s/%s', $entity->getEntityTypeId(), $entity->id());
$aliases = $this->aliasStorage
->loadByProperties([
'path' => $path,
]);
$aliasCount = count($aliases);
if ($aliasCount > 0) {
$message = $this->translationManager
->formatPlural($aliasCount, 'Found 1 alias with path "@path."', 'Found @count aliases with path "@path".', [
'@path' => $path,
]);
}
else {
$message = $this->t('Found no aliases with path "@path".', [
'@path' => $path,
]);
}
$build['header'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $message,
];
$build['aliases'] = [];
foreach ($aliases as $alias) {
$build['aliases'][] = $this->dumper
->exportAsRenderable($alias);
}
return $build;
}
protected function getEntityFromRouteMatch(RouteMatchInterface $route_match) {
$parameter_name = $route_match->getRouteObject()
->getOption('_devel_entity_type_id');
return $route_match->getParameter($parameter_name);
}
protected function getEntityWithFieldDefinitions(RouteMatchInterface $route_match) : ?EntityInterface {
$entity = $this->getEntityFromRouteMatch($route_match);
if (!$entity instanceof EntityInterface) {
return NULL;
}
if ($entity instanceof FieldableEntityInterface) {
$entity->getFieldDefinitions();
}
return $entity;
}
}
Classes