我有一个名为Map Points的实体包,它有一个国家的分类术语参考字段。我在自定义模块中使用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;
答案 0 :(得分:0)
我相信我想出来了。这是完成的代码:
//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 ( )
)