2016-04-15 6 views
0

Ich habe ein Entitätsbündel namens Map Points, die eine Taxonomie Begriff Referenzfeld von Ländern hat. Ich verwende EntityFieldQuery in einem benutzerdefinierten Modul, um alle Map-Point-Entitäten aufzulisten, aber ich muss sie nach dem Länderreferenzfeld gruppieren.Drupal EntityFieldQuery Gruppe Ergebnisse nach Taxonomie

Hier ist der Code, den ich gerade habe, der die Abfrage von tid 1 filtert und ich weiß, ich kann die Abfrage für jedes Land wiederholen, um die Ergebnisse zu gruppieren, aber ich suche nach einer verfeinerten Möglichkeit, die Ergebnisse zu gruppieren Länderbegriffs-IDs, ohne dass für jedes Land eine neue Abfrage geschrieben werden muss.

$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; 

Antwort

0

Ich glaube, dass ich das herausgefunden habe. Hier ist der fertige Code:

//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'], 
     ); 
    } 
} 

Dies wird ein Array erstellen, die wie folgt aussieht:

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