EntityTypeRepository.php 2.9 KB

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