7.x entity.inc EntityFieldQuery::addTag($tag)

Adds a tag to the query.

Tags are strings that mark a query so that hook_query_alter() and hook_query_TAG_alter() implementations may decide if they wish to alter the query. A query may have any number of tags, and they must be valid PHP identifiers (composed of letters, numbers, and underscores). For example, queries involving nodes that will be displayed for a user need to add the tag 'node_access', so that the node module can add access restrictions to the query.

If an entity field query has tags, it must also have an entity type specified, because the alter hook will need the entity base table.


string $tag: The tag to add.

Return value

EntityFieldQuery The called object.


includes/entity.inc, line 1150


Retrieves entities matching a given set of conditions.


public function addTag($tag) {
  $this->tags[$tag] = $tag;
  return $this;


bvanmeurs’s picture

If you would like to use the base tables in the tag handler, make sure that the base table is included in the query by, for example, specifying one of the fields:

	$query->propertyOrderBy('uid');	//Make sure that the base table is included
Lord Pachelbel’s picture

The tag DANGEROUS_ACCESS_CHECK_OPT_OUT was added in 7.15 to bypass access checks. It is in that respect similar to the "Disable SQL rewriting" option in Views.




 function MYMODULE_field_query($field) {
  $query = new EntityFieldQuery();
  return $query

This can greatly improve the performance of certain EntityFieldQuery queries* , but using this tag has security ramifications, obviously. See https://www.drupal.org/node/1597378 for more information.

* In my case, particularly any queries involving Content Access, ACL, Entity Construction Kit (ECK), and Entity reference fields. Views was creating incredibly inefficient queries that were scanning tens of thousands of rows of ACL-based permissions while the database had only a handful of actual records; "Disable SQL rewriting" fixed that. EntityFieldQuery was doing a similar thing, so using this tag solved my performance problems in my module's queries.