1

How do I construct an entity condition so that it loads current revision of all nodes? This is my attempt.

$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'xxxxx')  
->fieldCondition('xxx_module','tid', $tid,'=')  
->age(FIELD_LOAD_CURRENT)
->execute();

Instead of loading the current revision, it loads the published revision.

2
  • By current revision of a node you mean the latest?
    – Djouuuuh
    Commented Aug 14, 2014 at 13:56
  • I mean the draft one.
    – KMC
    Commented Aug 14, 2014 at 15:12

1 Answer 1

0

By default, EntityFieldQuery checks the currently logged-in user has the permission to access the queried entities. This is could be undesired, especially when the queried entity is not shown to the users, for example when a module just needs to get a list of entities to delete them.

To avoid EntityFieldQuery checks the currently logged-in user has the permission to access the entity, you need to use the following method:

  • addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT') for Drupal 7.15 or higher version
  • addMetaData('account', user_load(1)) for previous versions

Assuming you are using any version higher than 7.15, you code should be the following one.

query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'xxxxx')  
  ->fieldCondition('xxx_module','tid', $tid,'=')  
  ->age(FIELD_LOAD_CURRENT)
  ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')
->execute();

There could be a module that alter the query done from EntityFieldQuery and doesn't respect that tag; in that case, the query could still not return an entity revision that is not published.

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.