1

Okay, I've spent two days banging my head against the wall, so I'm finally going out for help...

I have a custom content type "eod_report" with a CCK field called "rfqs_received" which stores an integer.

I need to access the value of "rfqs_received" (previous 5 entries per user), but all I get is a list of the nodes themselves. What I want is an array of the queried field values.

How can I get access to the field values? Any help is MUCH appreciated.

    <?php

global $user;
$uid = $user->uid;


$query = new EntityFieldQuery();
$entities = $query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'eod_report')
  ->propertyCondition('uid', $uid)
  ->propertyCondition('status', 1);

  //->fieldCondition('field_rfqs_received', 'value', '6', '<');



$result = $query->execute();
$nids = array_keys($result['node']);
$nodes = node_load_multiple($nids);
$output = node_view_multiple($nodes);

print render($output);
?>

The answer below solved it. To clarify, this was a portion of an attempt to create a custom action that would calculate the average of field values for the last five pieces of content created by a user. The action occurs when content is saved/updated and saves values to the current user's profile. It's a bit sloppy, I think, but it works. I'm still learning... Here's the full code of the action:

    <?php

global $user;
$uid = $user->uid;

// query the database to get the previous 5 eod_reports
$query = new EntityFieldQuery();
$entities = $query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'eod_report')
  ->propertyCondition('uid', 50)
  ->propertyCondition('status', 1)
  ->propertyOrderBy('created', 'DESC')
  ->range(0,5);

$result = $query->execute();

// Collect the $nids of the nodes returned by the query

$nids = array_keys($result['node']);

// Declare an empty array to store the rfq results
$rfqs = array();
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
  // Get the field items
  $items = field_get_items('node', $node, 'field_rfqs_received');

  // loop through items, extract rfq values and store to array.
  foreach ($items as $item) {
    // Add to the output array.
   $rfqs[] = field_view_value('node', $node, 'field_rfqs_received', $item);
  }
}

// Declare an empty array to store the order results
$orders = array();
foreach ($nodes as $node) {
    // Get the field items
    $oitems = field_get_items('node', $node, 'field_orders_received');

    // Loop through items, extract order values and store to array.
    foreach ($oitems as $oitem) {
    $orders[] = field_view_value('node', $node, 'field_orders_received', $oitem);
    }
}

// Prepare the loop to sum the rfq results

$rfqSum = 0;
$i = 0;
// Loop through the results and collect a sum
while ($i < 5) {
    $rfqSum = $rfqSum + render($rfqs[$i]);
    $i = $i + 1;
}

// Prepare the loop to sum the order results

$orderSum = 0;
$x = 0;
// Loop through the order array to collect sum
while ($x < 5) {
    $orderSum = $orderSum + render($orders[$x]);
    $x = $x + 1;
}



// Print the Sums

print "<h3>RFQ Sum: " . $rfqSum . "</h3>";
print "<h3>Order Sum: " . $orderSum . "</h3>";

// Calculate the average

$rfqAvg = $rfqSum/5;
$orderAvg = $orderSum/5;

// Print the average
print "<h4>RFQ Avg: " . $rfqAvg . "</h4>";
print "<h4>Order Avg: " . $orderAvg . "</h4>"; 

// collect the current user information and store the calculated value in the user->field


$existing = user_load($uid);
$edit = (array) $existing; 
$edit['field_rfq_average']['und'][0]['value'] = $rfqAvg;
$edit['field_order_average']['und'][0]['value'] = $orderAvg;
user_save($existing, $edit);


?>

1 Answer 1

0

EntityFieldQuerys are for querying entities, with the option to filter on their field values. They don't have the capacity to return any field data.

What you might be looking for is something like this, using field_get_items() and field_view_value()

...
$output = [];
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
  // Get the field items
  $items = field_get_items('node', $node, 'field_rfqs_received');

  // Truncate to the necessary number (your logic will probably change here)
  foreach (array_slice($items, 0, 5) as $item) {
    // Add to the output array.
    $output[] = field_view_value('node', $node, 'field_rfqs_received', $item);
  }
}

print render($output);
1
  • That's totally it! Thank you so much!!! I'll update my post to reflect the working code.
    – Craig
    Commented Aug 13, 2013 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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