edgar.dueñas
Enviado por edgar.dueñas el Mié, 11/05/2016 - 11:24

Consultas con EntityFieldQuery() De esta forma se realizan las **consultas a una taxonomía** dentro de un modulo en desarrollo. <?php //Instancio el objeto para hacer consultas $query = new EntityFieldQuery(); //Consula de taxonomia para Country, donde la taxonomia es el vocabulario "country" $result = $query->entityCondition('entity_type', 'taxonomy_term') ->entityCondition('bundle', 'county') ->execute(); $relevantTerms = array(); foreach ($result['taxonomy_term'] as $term) { var_dump($term->tid); } ?> <?php // We use the machine name to ensure we have the proper vocab // that is not fixed to a specific vid on a installation. // Define this outside of the two conditions to avoid PHP notices. $term_options_list = array(); if ($range_vocab = taxonomy_vocabulary_machine_name_load('range')) { $efq = new EntityFieldQuery(); $result = $efq->entityCondition('entity_type', 'taxonomy_term') ->propertyCondition('vid', $range_vocab->vid) ->propertyOrderBy('weight') ->execute(); if (!empty($result['taxonomy_term'])) { // The results contain an array of entity stubs keyed by {term}.tid. // The stub only has {term}.tid and vocabulary_machine_name. $first_term_stub = current($result['taxonomy_term']); $first_term_tid = key($result['taxonomy_term']); // To load all terms. $terms = taxonomy_term_load_multiple(array_keys($result['taxonomy_term'])); // To generate an options list. foreach ($terms as $term) { // To hook into i18n and everything else, use entity_label(). $term_options_list[$term->tid] = entity_label('taxonomy_term', $term); // Single language, no term label alters. $term_options_list[$term->tid] = $term->name; } } } // IMPORTANT. This was for a select list that expects raw values. // If ANYTHING else is required, escape the term names, either here // or where the name is used. $safe_term_options_list = array_map('check_plain', $term_options_list); ?> <?php //get a list of nodes that match your criteria $nodeQuery = new EntityFieldQuery(); $entities = $nodeQuery->entityCondition('entity_type', 'node') // change 'food_menu_item' to target content_type ->entityCondition('bundle', 'food_menu_item') // get only nodes that are 'published' ->propertyCondition('status', 1) // replace field_food_menu with field_TAXONOMY_NAME // replace 2 with the taxonomy ID (tid) you're wanting ->fieldCondition('field_food_menu', 'tid', 2); // If multiple tids are required use this code in a loop with // one tid per fieldCondition() $restriction_tids = array(13,15); foreach ($restriction_tids as $r_tid){ $entities->fieldCondition('field_food_restrictions', 'tid', $r_tid); } // If you want to check for at least one of a list of tids replace 2 // with an array. e.g. array(2,4,5) $which_menu_tids = array(23,24); $entities->fieldCondition('field_which_menu', 'tid', $which_menu_tids, 'IN'); // Gets an array keyed by entity type, then entity id that match // your criteria. In this case : // field_food_menu term ID 2 // AND field_food_restrictions term ID 13 // AND field_food_restrictions term ID 15 // AND field_which_menu term ID 24 OR 24 $result = $entities->execute(); $nodes = array(); if (!empty($result['node'])) { $nodes = node_load_multiple(array_keys($result['node'])); } ?>
Otro ejemplo para consultar un tipo de contenido: <?php $query = new EntityFieldQuery(); //Consultanto el content_type ETA Work type $result = $query->entityCondition('entity_type', 'node') ->entityCondition('bundle', 'project_money') ->propertyCondition('status', 1) //->propertyCondition('title', $workType, '=') ->execute(); if (isset($result['node'])) { $news_items_nids = array_keys($result['node']); $news_items = entity_load('node', $news_items_nids); foreach ($news_items as $stdClass) { dpm($stdClass->field_amount["und"][0]["value"]); } } ?>
Consultando por nid y uid <?php $resultActive = $query->entityCondition('entity_type', 'node') ->entityCondition('bundle', 'project_money') ->propertyCondition('status', 1) ->propertyCondition('uid', $user->uid) //Only user current ->fieldCondition('field_project_nid', 'nid', $projectList) //$projectList is an array ->execute(); if (isset($resultActive['node'])) { $news_items_nids = array_keys($resultActive['node']); $news_items = entity_load('node', $news_items_nids); dpm($news_items); $totalProjectActive = 0; $totalProjectCompleted = 0; foreach ($news_items as $stdClass) { for ($i = 0; $i < count($project['status']); $i++) { //Allocated to Active Projects if($project['status'][$i] == "Active" && $project['list'][$i] == $stdClass->field_project_nid["und"][0]["nid"]){ //Allocated to Active Projects $totalProjectActive += $stdClass->field_amount["und"][0]["value"]; } //Allocated to Completed Projects if($project['status'][$i] == "Completed" && $project['list'][$i] == $stdClass->field_project_nid["und"][0]["nid"]) { //Allocated to Completed Projects $totalProjectCompleted += $stdClass->field_amount["und"][0]["value"]; } } } } ?>

 


Consultando todos los usuarios: IMPORTANTE: Si se desea hacer una consulta por roles, hay otra forma mas optimizada y el ejemplo esta en: http://www.pixtig.com/attrium/?q=recursos/node/209 <?php //Instancio el objeto para hacer consultas $query = new EntityFieldQuery(); //Consula de usuarios por rol $result = $query->entityCondition('entity_type', 'user') ->propertyCondition('status', 1) ->execute(); dpm($result['user']); foreach ($result['user'] as $one_user) { $user_record = user_load( $one_user->uid ); if( !empty($user_record->roles[6]) ) { dpm($user_record); } } ?>
<?php $query = new EntityFieldQuery(); $resultFactura = $query->entityCondition('entity_type', 'node') ->entityCondition('bundle', 'factura') ->propertyCondition('status', 1) ->fieldCondition('field_n_mero_de_pedimento', 'value', $arrayTitlePedimento) ->execute(); if (isset($resultFactura['node'])) { $factura_items_nids = array_keys($resultFactura['node']); return $factura_items_nids; } ?>
Mas informacion en:
https://drupal.org/node/1343708 https://drupal.org/node/1343708 https://api.drupal.org/api/drupal/includes%21entity.inc/class/EntityFieldQuery/7