Version 1
[yaffs-website] / web / modules / contrib / devel / src / Controller / EntityDebugController.php
1 <?php
2
3 namespace Drupal\devel\Controller;
4
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;
11
12 /**
13  * Controller for devel entity debug.
14  *
15  * @see \Drupal\devel\Routing\RouteSubscriber
16  * @see \Drupal\devel\Plugin\Derivative\DevelLocalTask
17  */
18 class EntityDebugController extends ControllerBase {
19
20   /**
21    * The dumper service.
22    *
23    * @var \Drupal\devel\DevelDumperManagerInterface
24    */
25   protected $dumper;
26
27   /**
28    * EntityDebugController constructor.
29    *
30    * @param \Drupal\devel\DevelDumperManagerInterface $dumper
31    *   The dumper service.
32    */
33   public function __construct(DevelDumperManagerInterface $dumper) {
34     $this->dumper = $dumper;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container) {
41     return new static($container->get('devel.dumper'));
42   }
43
44   /**
45    * Returns the entity type definition of the current entity.
46    *
47    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
48    *    A RouteMatch object.
49    *
50    * @return array
51    *    Array of page elements to render.
52    */
53   public function entityTypeDefinition(RouteMatchInterface $route_match) {
54     $output = [];
55
56     $entity = $this->getEntityFromRouteMatch($route_match);
57
58     if ($entity instanceof EntityInterface) {
59       $output = $this->dumper->exportAsRenderable($entity->getEntityType());
60     }
61
62     return $output;
63   }
64
65   /**
66    * Returns the loaded structure of the current entity.
67    *
68    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
69    *    A RouteMatch object.
70    *
71    * @return array
72    *    Array of page elements to render.
73    */
74   public function entityLoad(RouteMatchInterface $route_match) {
75     $output = [];
76
77     $entity = $this->getEntityFromRouteMatch($route_match);
78
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();
86       }
87
88       $output = $this->dumper->exportAsRenderable($entity);
89     }
90
91     return $output;
92   }
93
94   /**
95    * Returns the render structure of the current entity.
96    *
97    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
98    *    A RouteMatch object.
99    *
100    * @return array
101    *    Array of page elements to render.
102    */
103   public function entityRender(RouteMatchInterface $route_match) {
104     $output = [];
105
106     $entity = $this->getEntityFromRouteMatch($route_match);
107
108     if ($entity instanceof EntityInterface) {
109       $entity_type_id = $entity->getEntityTypeId();
110       $view_hook = $entity_type_id . '_view';
111
112       $build = [];
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);
117       }
118       elseif ($this->entityTypeManager()->hasHandler($entity_type_id, 'view_builder')) {
119         $build = $this->entityTypeManager()->getViewBuilder($entity_type_id)->view($entity);
120       }
121
122       $output = $this->dumper->exportAsRenderable($build);
123     }
124
125     return $output;
126   }
127
128   /**
129    * Retrieves entity from route match.
130    *
131    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
132    *   The route match.
133    *
134    * @return \Drupal\Core\Entity\EntityInterface|null
135    *   The entity object as determined from the passed-in route match.
136    */
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);
140     return $entity;
141   }
142
143 }