Созонов Алексей

Веб-разработчик

Как использовать EntityFieldQuery

09.08.2015

Пример запроса, который ищет все статьи с фотографиями.

<?php
$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'article')
  ->propertyCondition('status', 1)
  ->fieldCondition('field_news_types', 'value', 'spotlight', '=')
  ->fieldCondition('field_photo', 'fid', 'NULL', '!=')
  ->fieldCondition('field_faculty_tag', 'tid', $value)
  ->fieldCondition('field_news_publishdate', 'value', $year. '%', 'like')
  ->fieldOrderBy('field_photo', 'fid', 'DESC')
  ->range(0, 10)
  ->addMetaData('account', user_load(1)); // Run the query as user 1.

$result = $query->execute();

if (isset($result['node'])) {
  $news_items_nids = array_keys($result['node']);
  $news_items = entity_load('node', $news_items_nids);
}
?>

propertyOrderBy

$query = new EntityFieldQuery();
$query->propertyOrderBy('changed', 'DESC');

Оригинал

Пример EntityFieldQuery + Таксономия

<?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
#Drupal