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);
?>