3 namespace Drupal\devel;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Manipulates entity type information.
14 * This class contains primarily bridged hooks for compile-time or
15 * cache-clear-time hooks. Runtime hooks should be placed in EntityOperations.
17 class EntityTypeInfo implements ContainerInjectionInterface {
19 use StringTranslationTrait;
24 * @var \Drupal\Core\Session\AccountInterface
26 protected $currentUser;
29 * EntityTypeInfo constructor.
31 * @param \Drupal\Core\Session\AccountInterface $current_user
34 public function __construct(AccountInterface $current_user) {
35 $this->currentUser = $current_user;
41 public static function create(ContainerInterface $container) {
43 $container->get('current_user')
48 * Adds devel links to appropriate entity types.
50 * This is an alter hook bridge.
52 * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
53 * The master entity type list to alter.
55 * @see hook_entity_type_alter()
57 public function entityTypeAlter(array &$entity_types) {
58 foreach ($entity_types as $entity_type_id => $entity_type) {
59 if (($entity_type->getFormClass('default') || $entity_type->getFormClass('edit')) && $entity_type->hasLinkTemplate('edit-form')) {
60 $entity_type->setLinkTemplate('devel-load', "/devel/$entity_type_id/{{$entity_type_id}}");
62 if ($entity_type->hasViewBuilderClass() && $entity_type->hasLinkTemplate('canonical')) {
63 $entity_type->setLinkTemplate('devel-render', "/devel/$entity_type_id/{{$entity_type_id}}/render");
65 if ($entity_type->hasLinkTemplate('devel-render') || $entity_type->hasLinkTemplate('devel-load')) {
66 $entity_type->setLinkTemplate('devel-definition', "/devel/$entity_type_id/{{$entity_type_id}}/definition");
72 * Adds devel operations on entity that supports it.
74 * @param \Drupal\Core\Entity\EntityInterface $entity
75 * The entity on which to define an operation.
78 * An array of operation definitions.
80 * @see hook_entity_operation()
82 public function entityOperation(EntityInterface $entity) {
84 if ($this->currentUser->hasPermission('access devel information')) {
85 if ($entity->hasLinkTemplate('devel-load')) {
86 $operations['devel'] = [
87 'title' => $this->t('Devel'),
89 'url' => $entity->toUrl('devel-load'),
92 elseif ($entity->hasLinkTemplate('devel-render')) {
93 $operations['devel'] = [
94 'title' => $this->t('Devel'),
96 'url' => $entity->toUrl('devel-render'),