0

First of all. Are

$ent = Drupal\custom_entity\Entity\MyEntity::load(1);

and

 $ent = \Drupal::entityTypeManager()->getStorage('custom_entity')->load(1);

the same?

Also I know that \Drupal::entityTypeManager()->getStorage('custom_entity')->load() is getting cached.

When I talk directly to the class's load function also?

Thanks

2

First of all. Are ... and ... the same?

Assuming your entity type class doesn't override Entity::load to do something different, yes they are the same. If you look at that method you can see it uses exactly the same op:

public static function load($id) {
  $entity_type_repository = \Drupal::service('entity_type.repository');
  $entity_type_manager = \Drupal::entityTypeManager();
  $storage = $entity_type_manager
    ->getStorage($entity_type_repository
    ->getEntityTypeFromClass(get_called_class()));
  return $storage
    ->load($id);
}

So Entity::load is simply a shortcut to loading the entity via the manager service.

When I talk directly to the class's load function also?

They're identical operations, so yes, they share the same caching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.