0

I have a module I'm trying to update to Drupal 11.

Here's the code:

  public function processItem($data) {
    $entity_type = $this->entityTypeManager->getDefinition($data['entity_type']);
    $storage = $this->entityTypeManager->getStorage($data['entity_type']);
    $is_revisionable = $entity_type->isRevisionable();
    if ($is_revisionable) {
      $entity = $storage->loadRevision($data['entity_id']);
    }

However, Methods ::loadRevision() and ::deleteRevision() in classes \Drupal\Core\Entity\EntityStorageInterface, \Drupal\Core\Config\Entity\ConfigEntityStorage and \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage are deprecated, so I need to switch from EntityStorageInterface to RevisionableStorageInterface.

That change record could use a simple example; how can I get the revisionable storage? There doesn't seem to be a way to get it from entityTypeManager, and I couldn't find a service to inject to make it available.

1 Answer 1

3

You don't need to change custom code. The storage you have loaded from entityTypeManager has this interface if it is for a content entity type:

$storage = $this->entityTypeManager->getStorage($entity_type);
/** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
$entity = $storage->loadRevision($revision_id);

You can use the full interface to have your IDE autocomplete all storage methods of a content entity:

/** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
$storage = $this->entityTypeManager->getStorage('paragraph');
$entity = $storage->loadRevision($revision_id);

In the rare case that you have mixed entity types (content and config) you can check the storage before applying the method to avoid an exception:

if ($storage instanceof RevisionableStorageInterface) {
  $entity = $storage->loadRevision($revision_id);
2
  • I don't get it. I am loading a paragraph entity with $this->entityTypeManager->getStorage('paragraph')->loadRevision($revision_id) and get exactly same deprecated warning. What is the fix here?
    – belinea
    Commented Jun 11 at 21:07
  • Split the code line and type hint $storage with PHPDoc @var.
    – 4uk4
    Commented Jun 12 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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