Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityTypeRepository.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Drupal\Core\Entity\Exception\AmbiguousEntityClassException;
6 use Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8
9 /**
10  * Provides helper methods for loading entity types.
11  *
12  * @see \Drupal\Core\Entity\EntityTypeManagerInterface
13  */
14 class EntityTypeRepository implements EntityTypeRepositoryInterface {
15
16   use StringTranslationTrait;
17
18   /**
19    * The entity type manager.
20    *
21    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22    */
23   protected $entityTypeManager;
24
25   /**
26    * Contains cached mappings of class names to entity types.
27    *
28    * @var array
29    */
30   protected $classNameEntityTypeMap = [];
31
32   /**
33    * Constructs a new EntityTypeRepository.
34    *
35    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
36    *   The entity type manager.
37    */
38   public function __construct(EntityTypeManagerInterface $entity_type_manager) {
39     $this->entityTypeManager = $entity_type_manager;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getEntityTypeLabels($group = FALSE) {
46     $options = [];
47     $definitions = $this->entityTypeManager->getDefinitions();
48
49     foreach ($definitions as $entity_type_id => $definition) {
50       if ($group) {
51         $options[(string) $definition->getGroupLabel()][$entity_type_id] = $definition->getLabel();
52       }
53       else {
54         $options[$entity_type_id] = $definition->getLabel();
55       }
56     }
57
58     if ($group) {
59       foreach ($options as &$group_options) {
60         // Sort the list alphabetically by group label.
61         array_multisort($group_options, SORT_ASC, SORT_NATURAL);
62       }
63
64       // Make sure that the 'Content' group is situated at the top.
65       $content = $this->t('Content', [], ['context' => 'Entity type group']);
66       $options = [(string) $content => $options[(string) $content]] + $options;
67     }
68
69     return $options;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function getEntityTypeFromClass($class_name) {
76     // Check the already calculated classes first.
77     if (isset($this->classNameEntityTypeMap[$class_name])) {
78       return $this->classNameEntityTypeMap[$class_name];
79     }
80
81     $same_class = 0;
82     $entity_type_id = NULL;
83     foreach ($this->entityTypeManager->getDefinitions() as $entity_type) {
84       if ($entity_type->getOriginalClass() == $class_name  || $entity_type->getClass() == $class_name) {
85         $entity_type_id = $entity_type->id();
86         if ($same_class++) {
87           throw new AmbiguousEntityClassException($class_name);
88         }
89       }
90     }
91
92     // Return the matching entity type ID if there is one.
93     if ($entity_type_id) {
94       $this->classNameEntityTypeMap[$class_name] = $entity_type_id;
95       return $entity_type_id;
96     }
97
98     throw new NoCorrespondingEntityClassException($class_name);
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function clearCachedDefinitions() {
105     $this->classNameEntityTypeMap = [];
106   }
107
108 }