Version 1
[yaffs-website] / web / modules / contrib / devel / src / EntityTypeInfo.php
1 <?php
2
3 namespace Drupal\devel;
4
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;
10
11 /**
12  * Manipulates entity type information.
13  *
14  * This class contains primarily bridged hooks for compile-time or
15  * cache-clear-time hooks. Runtime hooks should be placed in EntityOperations.
16  */
17 class EntityTypeInfo implements ContainerInjectionInterface {
18
19   use StringTranslationTrait;
20
21   /**
22    * The current user.
23    *
24    * @var \Drupal\Core\Session\AccountInterface
25    */
26   protected $currentUser;
27
28   /**
29    * EntityTypeInfo constructor.
30    *
31    * @param \Drupal\Core\Session\AccountInterface $current_user
32    *   Current user.
33    */
34   public function __construct(AccountInterface $current_user) {
35     $this->currentUser = $current_user;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container) {
42     return new static(
43       $container->get('current_user')
44     );
45   }
46
47   /**
48    * Adds devel links to appropriate entity types.
49    *
50    * This is an alter hook bridge.
51    *
52    * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
53    *   The master entity type list to alter.
54    *
55    * @see hook_entity_type_alter()
56    */
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}}");
61       }
62       if ($entity_type->hasViewBuilderClass() && $entity_type->hasLinkTemplate('canonical')) {
63         $entity_type->setLinkTemplate('devel-render', "/devel/$entity_type_id/{{$entity_type_id}}/render");
64       }
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");
67       }
68     }
69   }
70
71   /**
72    * Adds devel operations on entity that supports it.
73    *
74    * @param \Drupal\Core\Entity\EntityInterface $entity
75    *   The entity on which to define an operation.
76    *
77    * @return array
78    *   An array of operation definitions.
79    *
80    * @see hook_entity_operation()
81    */
82   public function entityOperation(EntityInterface $entity) {
83     $operations = [];
84     if ($this->currentUser->hasPermission('access devel information')) {
85       if ($entity->hasLinkTemplate('devel-load')) {
86         $operations['devel'] = [
87           'title' => $this->t('Devel'),
88           'weight' => 100,
89           'url' => $entity->toUrl('devel-load'),
90         ];
91       }
92       elseif ($entity->hasLinkTemplate('devel-render')) {
93         $operations['devel'] = [
94           'title' => $this->t('Devel'),
95           'weight' => 100,
96           'url' => $entity->toUrl('devel-render'),
97         ];
98       }
99     }
100     return $operations;
101   }
102
103 }