class EntityTypeRepository in Drupal 8
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/Entity/EntityTypeRepository.php \Drupal\Core\Entity\EntityTypeRepository
- 10 core/lib/Drupal/Core/Entity/EntityTypeRepository.php \Drupal\Core\Entity\EntityTypeRepository
Provides helper methods for loading entity types.
Hierarchy
- class \Drupal\Core\Entity\EntityTypeRepository implements EntityTypeRepositoryInterface uses StringTranslationTrait
Expanded class hierarchy of EntityTypeRepository
See also
\Drupal\Core\Entity\EntityTypeManagerInterface
1 file declares its use of EntityTypeRepository
- EntityTypeRepositoryTest.php in core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityTypeRepositoryTest.php
1 string reference to 'EntityTypeRepository'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses EntityTypeRepository
File
- core/
lib/ Drupal/ Core/ Entity/ EntityTypeRepository.php, line 14
Namespace
Drupal\Core\EntityView source
class EntityTypeRepository implements EntityTypeRepositoryInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Contains cached mappings of class names to entity types.
*
* @var array
*/
protected $classNameEntityTypeMap = [];
/**
* Constructs a new EntityTypeRepository.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function getEntityTypeLabels($group = FALSE) {
$options = [];
$definitions = $this->entityTypeManager
->getDefinitions();
foreach ($definitions as $entity_type_id => $definition) {
if ($group) {
$options[(string) $definition
->getGroupLabel()][$entity_type_id] = $definition
->getLabel();
}
else {
$options[$entity_type_id] = $definition
->getLabel();
}
}
if ($group) {
foreach ($options as &$group_options) {
// Sort the list alphabetically by group label.
array_multisort($group_options, SORT_ASC, SORT_NATURAL);
}
// Make sure that the 'Content' group is situated at the top.
$content = $this
->t('Content', [], [
'context' => 'Entity type group',
]);
$options = [
(string) $content => $options[(string) $content],
] + $options;
}
return $options;
}
/**
* {@inheritdoc}
*/
public function getEntityTypeFromClass($class_name) {
// Check the already calculated classes first.
if (isset($this->classNameEntityTypeMap[$class_name])) {
return $this->classNameEntityTypeMap[$class_name];
}
$same_class = 0;
$entity_type_id = NULL;
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type) {
if ($entity_type
->getOriginalClass() == $class_name || $entity_type
->getClass() == $class_name) {
$entity_type_id = $entity_type
->id();
if ($same_class++) {
throw new AmbiguousEntityClassException($class_name);
}
}
}
// Return the matching entity type ID if there is one.
if ($entity_type_id) {
$this->classNameEntityTypeMap[$class_name] = $entity_type_id;
return $entity_type_id;
}
throw new NoCorrespondingEntityClassException($class_name);
}
/**
* {@inheritdoc}
*/
public function clearCachedDefinitions() {
@trigger_error(__METHOD__ . ' is deprecated in drupal:8.0.0 and is removed in drupal:9.0.0', E_USER_DEPRECATED);
$this->classNameEntityTypeMap = [];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityTypeRepository:: |
protected | property | Contains cached mappings of class names to entity types. | |
EntityTypeRepository:: |
protected | property | The entity type manager. | |
EntityTypeRepository:: |
public | function |
Clear the static cache. Overrides EntityTypeRepositoryInterface:: |
|
EntityTypeRepository:: |
public | function |
Gets the entity type ID based on the class that is called on. Overrides EntityTypeRepositoryInterface:: |
|
EntityTypeRepository:: |
public | function |
Builds a list of entity type labels suitable for a Form API options list. Overrides EntityTypeRepositoryInterface:: |
|
EntityTypeRepository:: |
public | function | Constructs a new EntityTypeRepository. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |