EntityTypeRepository.php in Drupal 8
File
core/lib/Drupal/Core/Entity/EntityTypeRepository.php
View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Entity\Exception\AmbiguousEntityClassException;
use Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class EntityTypeRepository implements EntityTypeRepositoryInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $classNameEntityTypeMap = [];
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
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) {
array_multisort($group_options, SORT_ASC, SORT_NATURAL);
}
$content = $this
->t('Content', [], [
'context' => 'Entity type group',
]);
$options = [
(string) $content => $options[(string) $content],
] + $options;
}
return $options;
}
public function getEntityTypeFromClass($class_name) {
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);
}
}
}
if ($entity_type_id) {
$this->classNameEntityTypeMap[$class_name] = $entity_type_id;
return $entity_type_id;
}
throw new NoCorrespondingEntityClassException($class_name);
}
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 = [];
}
}