3 namespace Drupal\devel\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\devel\DevelDumperManagerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Controller for devel entity debug.
15 * @see \Drupal\devel\Routing\RouteSubscriber
16 * @see \Drupal\devel\Plugin\Derivative\DevelLocalTask
18 class EntityDebugController extends ControllerBase {
23 * @var \Drupal\devel\DevelDumperManagerInterface
28 * EntityDebugController constructor.
30 * @param \Drupal\devel\DevelDumperManagerInterface $dumper
33 public function __construct(DevelDumperManagerInterface $dumper) {
34 $this->dumper = $dumper;
40 public static function create(ContainerInterface $container) {
41 return new static($container->get('devel.dumper'));
45 * Returns the entity type definition of the current entity.
47 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
48 * A RouteMatch object.
51 * Array of page elements to render.
53 public function entityTypeDefinition(RouteMatchInterface $route_match) {
56 $entity = $this->getEntityFromRouteMatch($route_match);
58 if ($entity instanceof EntityInterface) {
59 $output = $this->dumper->exportAsRenderable($entity->getEntityType());
66 * Returns the loaded structure of the current entity.
68 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
69 * A RouteMatch object.
72 * Array of page elements to render.
74 public function entityLoad(RouteMatchInterface $route_match) {
77 $entity = $this->getEntityFromRouteMatch($route_match);
79 if ($entity instanceof EntityInterface) {
80 // Field definitions are lazy loaded and are populated only when needed.
81 // By calling ::getFieldDefinitions() we are sure that field definitions
82 // are populated and available in the dump output.
83 // @see https://www.drupal.org/node/2311557
84 if($entity instanceof FieldableEntityInterface) {
85 $entity->getFieldDefinitions();
88 $output = $this->dumper->exportAsRenderable($entity);
95 * Returns the render structure of the current entity.
97 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
98 * A RouteMatch object.
101 * Array of page elements to render.
103 public function entityRender(RouteMatchInterface $route_match) {
106 $entity = $this->getEntityFromRouteMatch($route_match);
108 if ($entity instanceof EntityInterface) {
109 $entity_type_id = $entity->getEntityTypeId();
110 $view_hook = $entity_type_id . '_view';
113 // If module implements own {entity_type}_view() hook use it, otherwise
114 // fallback to the entity view builder if available.
115 if (function_exists($view_hook)) {
116 $build = $view_hook($entity);
118 elseif ($this->entityTypeManager()->hasHandler($entity_type_id, 'view_builder')) {
119 $build = $this->entityTypeManager()->getViewBuilder($entity_type_id)->view($entity);
122 $output = $this->dumper->exportAsRenderable($build);
129 * Retrieves entity from route match.
131 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
134 * @return \Drupal\Core\Entity\EntityInterface|null
135 * The entity object as determined from the passed-in route match.
137 protected function getEntityFromRouteMatch(RouteMatchInterface $route_match) {
138 $parameter_name = $route_match->getRouteObject()->getOption('_devel_entity_type_id');
139 $entity = $route_match->getParameter($parameter_name);