X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fdevel%2Fsrc%2FController%2FEntityDebugController.php;fp=web%2Fmodules%2Fcontrib%2Fdevel%2Fsrc%2FController%2FEntityDebugController.php;h=515a9fc39d0b985195ba20db52359fa815e90351;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/devel/src/Controller/EntityDebugController.php b/web/modules/contrib/devel/src/Controller/EntityDebugController.php new file mode 100644 index 000000000..515a9fc39 --- /dev/null +++ b/web/modules/contrib/devel/src/Controller/EntityDebugController.php @@ -0,0 +1,143 @@ +dumper = $dumper; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static($container->get('devel.dumper')); + } + + /** + * Returns the entity type definition of the current entity. + * + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * A RouteMatch object. + * + * @return array + * Array of page elements to render. + */ + public function entityTypeDefinition(RouteMatchInterface $route_match) { + $output = []; + + $entity = $this->getEntityFromRouteMatch($route_match); + + if ($entity instanceof EntityInterface) { + $output = $this->dumper->exportAsRenderable($entity->getEntityType()); + } + + return $output; + } + + /** + * Returns the loaded structure of the current entity. + * + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * A RouteMatch object. + * + * @return array + * Array of page elements to render. + */ + public function entityLoad(RouteMatchInterface $route_match) { + $output = []; + + $entity = $this->getEntityFromRouteMatch($route_match); + + if ($entity instanceof EntityInterface) { + // Field definitions are lazy loaded and are populated only when needed. + // By calling ::getFieldDefinitions() we are sure that field definitions + // are populated and available in the dump output. + // @see https://www.drupal.org/node/2311557 + if($entity instanceof FieldableEntityInterface) { + $entity->getFieldDefinitions(); + } + + $output = $this->dumper->exportAsRenderable($entity); + } + + return $output; + } + + /** + * Returns the render structure of the current entity. + * + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * A RouteMatch object. + * + * @return array + * Array of page elements to render. + */ + public function entityRender(RouteMatchInterface $route_match) { + $output = []; + + $entity = $this->getEntityFromRouteMatch($route_match); + + if ($entity instanceof EntityInterface) { + $entity_type_id = $entity->getEntityTypeId(); + $view_hook = $entity_type_id . '_view'; + + $build = []; + // If module implements own {entity_type}_view() hook use it, otherwise + // fallback to the entity view builder if available. + 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; + } + + /** + * Retrieves entity from route match. + * + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The route match. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * The entity object as determined from the passed-in route match. + */ + protected function getEntityFromRouteMatch(RouteMatchInterface $route_match) { + $parameter_name = $route_match->getRouteObject()->getOption('_devel_entity_type_id'); + $entity = $route_match->getParameter($parameter_name); + return $entity; + } + +}