7

In drupal 7 you can do this to load an entity revision using

entity_load($entity_type, $ids = FALSE, $conditions = array(), $reset = FALSE)

like this:

$old_revision = entity_load('node', array(), array('vid' => 123));

However now $conditions is being deprecated. Pretty much wherever you go you see people saying that you should use EntityFieldQuery instead of conditions, however that doesn't help in this case. You can use age() to query revisions using EntityFieldQuery however it doesn't give you fully loaded node results so you then still have to load the entity and how do you load that revision of that entity without the conditions.

1 Answer 1

19

Currently the preferred way is to get the storage instance (EntityStorageInterface) for the entity type from the EntityTypeManager and then use its loadRevision() function. For example, you could do this for a node:

$node_revision = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadRevision($node_revision_id);

Previously to this you would get the storage instance from the EntityManager like this:

$node_revision = \Drupal::entityManager()
  ->getStorage('node')
  ->loadRevision($node_revision_id);

This still currently works however it has been deprecated in favour of the first method. The change record relating to this is at https://www.drupal.org/node/2549139

The old way, which still currently works in Drupal 8 but is also deprecated, is the function entity_revision_load(), which is also available in drupal 7 via entity_revision_load() in the Entity API module.

This function replaces the use of conditions to load old entity revisions. You can see the history of this change in the Drupal core issue about Remove deprecated $conditions support from entity controller.

The way you would use this function as per the original question is:

$old_revision = entity_revision_load('node', 123);
4
  • 2
    Yes, this is the way to do it. There's also loadRevision() on the entity storage interface (this is a wrapper for that).
    – Berdir
    Commented Jun 6, 2014 at 15:58
  • 1
    Thanks for the info. I have now updated the answer with the current preferred method using the EntityTypeManager.
    – rooby
    Commented Jun 14, 2016 at 1:05
  • 1
    But how do you get the revision id in the first place? e.g. Let's say i have only an entity id, and i want to fetch the 10 latest revisions for it. What's the prefered way to build the revision query? Commented Oct 11, 2017 at 15:37
  • 2
    Answered my own question: entityStorageInterface::getQuery will create the query that i need. ::allRevisions() will cause the query to query both the base table and entity tables. Conditions apply as expected, and return value is an array indexed by vid. Commented Oct 11, 2017 at 15:54

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.