Tag Archives: EntityFieldQuery

Drupal 7 – Select node by language using EntityFieldQuery

Here is an example to select entities by the current language in EntityFieldQuery.

// Get the current language
global $language;

// Setup the EntityFieldQuery
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', '<CONTENT TYPE>') // ex. article
  ->propertyCondition('status', 1) // published nodes
  ->propertyCondition('language', $language->language, '='); // filter by current language

// Execute the query
$result = $query->execute();

 

Done =)

Drupal 7 – Customize the exposed filter selection list

Yesterday we have talked about how to use selection list for selecting Entity reference in Views exposed filter.
Drupal 7 – Selection list for Entity reference in Views exposed filter

Now i want to have more control on the selection list. For example, i have a content type called competition and i want to limit the number of options in the Entity reference selection list. In this case, we have to create a custom module and alter the views_exposed_form.
Continue reading Drupal 7 – Customize the exposed filter selection list

Drupal 7 – Get taxonomy terms by EntityFieldQuery

The EntityFieldQuery is not limited to node, it can be used to retrieve any entity types in Drupal 7. Here comes an example about getting taxonomy terms with EntityFieldQuery.

I would like to retrieve all terms with of a specific vocabulary which has vid = 1 with the descending weight order.
Continue reading Drupal 7 – Get taxonomy terms by EntityFieldQuery

Drupal 7 – Get mulitple nodes using EntityFieldQuery and node_load_multiple()

We tried to get specific nodes using EntityFieldQuery.
Drupal 7 – Get specific nodes using EntityFieldQuery

It returns a list of node ids which fulfill the query conditions. The next step is usually the retrieval of the nodes by the node ids. This can be done by the following example using node_load_multiple().
Continue reading Drupal 7 – Get mulitple nodes using EntityFieldQuery and node_load_multiple()

Drupal 7 – Set a date fieldCondition in EntityFieldQuery

We can select nodes by conditions using EntityFieldQuery.
Drupal 7 – Get specific nodes using EntityFieldQuery

After I have installed the Date module, I could create a content type with a date field called field_expiry_date. When i setup this field, there are 3 date options in the field type.

  • Date
  • Date (ISO format)
  • Date (Unix timestamp)

Continue reading Drupal 7 – Set a date fieldCondition in EntityFieldQuery

Drupal 7 – Order EntityFieldQuery by random using hook_query_TAG_alter()

The EntityFieldQuery does not support any method to select the entities by random. A simple workaround is using the query tag and implement the hook_query_TAG_alter().

Add addTag() in your EntityFieldQuery object.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', '<node type>')
  ->propertyCondition('status', 1)
  ->addTag('random')
  ->range(0, 1);

Continue reading Drupal 7 – Order EntityFieldQuery by random using hook_query_TAG_alter()