Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a node template, rendering a product list based on a query :

$productsQuery = db_select('commerce_product', 'p');
$productsQuery->join('commerce_product_type', 't', 't.type = p.type');

$productsQuery->fields('p')
    ->fields('t')
    ->condition('p.status', 0, '>')
    ->orderBy('p.created', 'DESC')
;

$results = $productsQuery->execute()->fetchAll();

foreach ($results as $product) {
    $productNodeQuery = new EntityFieldQuery();
    $productNodeQuery->entityCondition('entity_type', 'node', '=')
        ->propertyCondition('type', $product->type)
        ->fieldCondition('field_product', 'product_id', $product->product_id, '=')
        ->range(0, 1);

    if ($node = $productNodeQuery->execute()) {
        $node = array_shift($node['node']);
        // Render the node
    }
}

It is working well, but I do not access to the different fields of the product-display node in EntityFieldQuery...

For instance, In my product-display, I have a required relation to a taxonomy term, that I would like to display for each product.

How to get this specific field ?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

An EntityFieldQuery just returns IDs, you need to load the entities manually:

$results = $query->execute();
if (!empty($results['node'])) {
  $nodes = node_load_multiple(array_keys($results['node'));
  foreach ($nodes as $node) {
    if ($items = field_get_items('node', $node, 'field_foo')) {
      // ...
    }
  }
}
share|improve this answer
    
Okay thanks, I am trying ; Just wondering, do I have to do it after the EntityFieldQuery ? Isn't it a huge leak of performances to have to execute 2 additional queries for each product ? –  Flo-Schield-Bobby May 12 at 13:46
    
Yep, welcome to Drupal :) You can only query one entity type at a time with EntityFieldQuery; you can filter by field, but it has to be a 'real' field (e.g. not a reverse entity-reference), so you can't get all the commerce products with the reverse reference to the display node in one query - it has to be done twice, and I think (API-wise at least) that's the quickest way –  Clive May 12 at 14:00
    
Okay thanks again ! The field_get_items() always returns me "false", I think I am confusing about the field name (3rd argument), but I am pretty sure it is "field_category"... How/Where can I check the name ? –  Flo-Schield-Bobby May 12 at 14:03
    
Easiest way is through the UI - if you go to Admin -> Structure -> Content Types -> Type -> Manage Fields, you can see the field name –  Clive May 12 at 14:03
    
I am affraid that my product display is a product-variation-type, more than a content-type... Could it be the reason why I can't find my type ? How to do it properly, otherwise ? –  Flo-Schield-Bobby May 12 at 14:08
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.