Entity Field Query

Entity JS

Entity JS allows for Javascript layer access to some commonly used entity-related functions such as EntityFieldQuery.

This is achieved through Drupal menu callbacks and jQuery $.ajax calls.

Note: jQuery's jqXHR Object uses the .done() method that requires later versions of jQuery (1.5+) than what currently ships with Drupal 7. In order to use these examples, we recommend installing jQuery Update. For more on .done(), review the .promise() method jQuery API docs.

entity_create

Create an entity by providing a type and an array of arguments. Shown printing response below.

values = {
    "name": "username",
    "mail": "username@example.com",
}
entity_create('user', values).done(function(data) { console.log(data); });

Uses a $.post callback URL: /entity_create/[entity_type]

entity_render_view

Return a rendered entity using the a view mode. Shown printing HTML to console below.

entity_render_view('node', 1, 'default').done(function(data) { console.log(data); });
Uses a $.get callback URL: /entity_js_drupal_render_entity_view/[entity_type]/[entity_id]/[view_mode]

Read more

Querying to get a list of taxonomy terms or nodes using EntityFieldQuery

This belongs inside your module code.

You can get a collection of nodes matching particular parameters by using EntityFieldQuery, instead of writing an SQL statement. I had a hard time getting all the syntax together, so here are a couple of simple samples:

<?php
 
// fetch the taxonomy terms inside a particular vocabulary
 
$taxonomyQuery = new EntityFieldQuery();
 
$taxonomyTerms = $taxonomyQuery->entityCondition('entity_type', 'taxonomy_term')
    ->
propertyCondition('vid', 2) //change 2 to any vocabulary ID
   
->propertyOrderBy('weight')
    ->
execute();
  foreach(
$taxonomyTerms['taxonomy_term'] as $term) {
   
$relevantTerms[] = $term->tid;
  }

 
// $relevantTerms will now have the terms of your target vocabulary
?>
Read more

How to use EntityFieldQuery

EntityFieldQuery is a class, new to Drupal 7, that allows retrieval of a set of entities based on specified conditions. It allows finding of entities based on entity properties, field values, and other generic entity metadata. The syntax is really compact and easy to follow, as well. And, best of all, it's core Drupal; no additional modules are necessary to use it.

http://api.drupal.org/api/drupal/includes--entity.inc/class/EntityFieldQ... and the test cases in modules\simpletest\tests\entity_query.test are the authoritative documentation for EntityFieldQuery. The class itself "EntityFieldQuery" is in includes/entity.inc. This article is just jumpstart type documentation.

Creating A Query

Here is a basic query looking for all articles with a photo that are tagged as a particular faculty member and published this year. In the last 5 lines of the code below, the $result variable is populated with an associative array with the first key being the entity type and the second key being the entity id (e.g., $result['node'][12322] = partial node data). Note the $result won't have the 'node' key when it's empty, thus the check using isset, this is explained here.

Read more
Subscribe with RSS Syndicate content
nobody click here