public function EntityTypeRepository::getEntityTypeFromClass

public EntityTypeRepository::getEntityTypeFromClass($class_name)

Gets the entity type ID based on the class that is called on.

Compares the class this is called on against the known entity classes and returns the entity type ID of a direct match or a subclass as fallback, to support entity type definitions that were altered.

Parameters

string $class_name: Class name to use for searching the entity type ID.

Return value

string The entity type ID.

Throws

\Drupal\Core\Entity\Exception\AmbiguousEntityClassException Thrown when multiple subclasses correspond to the called class.

\Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException Thrown when no entity class corresponds to the called class.

Overrides EntityTypeRepositoryInterface::getEntityTypeFromClass

See also

\Drupal\Core\Entity\Entity::load()

\Drupal\Core\Entity\Entity::loadMultiple()

File

core/lib/Drupal/Core/Entity/EntityTypeRepository.php, line 75

Class

EntityTypeRepository
Provides helper methods for loading entity types.

Namespace

Drupal\Core\Entity

Code

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);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityTypeRepository.php/function/EntityTypeRepository::getEntityTypeFromClass/8.1.x