2016-04-15 67 views
0

我有一個名爲Map Points的實體包,它有一個國家的分類術語參考字段。我在自定義模塊中使用EntityFieldQuery來列出所有的地圖點實體,但我需要按照國家參考字段對它們進行分組。Drupal的EntityFieldQuery分組結果的分類

這裏是我現在的代碼,它通過tid 1過濾查詢,我知道我可以重複查詢每個國家對結果進行分組,但我正在尋找一種更精細的方式來分組結果國家術語編號而無需爲每個國家編寫新的查詢。

$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

我相信我明白了這一點。這裏的成品代碼:

//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 () 
)