Drupal EntityFieldQuery按分类法分组结果

Drupal EntityFieldQuery按分类法分组结果,drupal,drupal-7,drupal-taxonomy,Drupal,Drupal 7,Drupal Taxonomy,我有一个名为MapPoints的实体包,它有一个国家的分类术语参考字段。我在自定义模块中使用EntityFieldQuery列出所有地图点实体,但我需要按国家/地区参考字段对它们进行分组 这是我现在拥有的代码,它按tid 1过滤查询,我知道我可以对每个国家重复查询以对结果进行分组,但我正在寻找一种更精确的方法,以按国家术语id对结果进行分组,而不必为每个国家编写新的查询 $query = new EntityFieldQuery(); $query ->entityConditio

我有一个名为MapPoints的实体包,它有一个国家的分类术语参考字段。我在自定义模块中使用EntityFieldQuery列出所有地图点实体,但我需要按国家/地区参考字段对它们进行分组

这是我现在拥有的代码,它按tid 1过滤查询,我知道我可以对每个国家重复查询以对结果进行分组,但我正在寻找一种更精确的方法,以按国家术语id对结果进行分组,而不必为每个国家编写新的查询

$query = new EntityFieldQuery();
$query
    ->entityCondition('entity_type', 'givepower')
    ->entityCondition('bundle', 'map_points')
    ->propertyCondition('type', 'map_points', '=')
    ->fieldCondition('field_map_point_country', 'tid', '1');

$result = $query->execute();

// set empty map point id array
$map_point_ids = array();
// loop through all givepower entities
foreach($result['givepower'] as $record) {
    $map_point_ids[] = $record->id;
}
// load entities
$map_point = entity_load('givepower', $map_point_ids);
// set entity view
$entities = entity_view('givepower', $map_point);


return $entities;

我想我知道了。以下是完成的代码:

//load recent_work_countries taxonomy
$vocabulary = taxonomy_vocabulary_machine_name_load('recent_work_countries');

// load all terms in the recent_work_countries taxonomy
$terms = entity_load('taxonomy_term', FALSE, array('vid' => $vocabulary->vid));

// set empty countries array
$countries = array();

// loop through each recent_work_countries term
foreach($terms as $tid => $value) {

    // set country term name and term id(tid) vars
    $country_name = $terms[$tid]->name;
    $country_tid = $terms[$tid]->tid;

    // add each country to the counties array
    $countries[$country_name] = array();

    // create new query to grab all map point entities with the current recent_work_countries tid
    $query = new EntityFieldQuery();
    $query
        ->entityCondition('entity_type', 'givepower')
        ->entityCondition('bundle', 'map_points')
        ->propertyCondition('type', 'map_points', '=')
        ->fieldCondition('field_map_point_country', 'tid', $country_tid);

    $result = $query->execute();

    // set empty tab id array
    $map_point_ids = array();
    // loop through all givepower entities that have the map_point_ids
    foreach($result['givepower'] as $record) {
        $map_point_ids[] = $record->id;
    }
    // load entities
    $map_point = entity_load('givepower', $map_point_ids);
    // set entity view
    $entities = entity_view('givepower', $map_point);

    // loop through each entities results
    foreach($entities['givepower'] as $eid => $e_val) {
        // add entity result data to countries array
        $countries[$country_name][] = array(
                'title' => $e_val['field_map_point_title']['#items'][0]['safe_value'],
                'description' => $e_val['field_map_point_description']['#items'][0]['safe_value'],
                'latitude' => $e_val['field_map_point_latitude']['#items'][0]['safe_value'],
                'longitude' => $e_val['field_map_point_longitute']['#items'][0]['safe_value'],
        );
    }
}
这将创建一个如下所示的数组:

Array ( 
 [Kenya] => Array ( 
  [0] => Array ( 
    [title] => Test Map Point 1 
    [description] => lorum ipsum
    [latitude] => 1.765404 
    [longitude] => 40.079880 ) 
  [1] => Array ( 
    [title] => Test Map Point 1
    [description] => Lorum ipsum 
    [latitude] => 0.633657 
    [longitude] => 37.350050 ) ) 
 [Haiti] => Array ( 
  [0] => Array ( 
    [title] => GA University 
    [description] => lorum ipsum 
    [latitude] => 18.574420 
    [longitude] => -72.310981 ) ) 
 [Nepal] => Array ( ) 
 [Ghana] => Array ( ) 
)