0

I'm using EntityFieldQuery in an attempt to query for specific field values (dates in an event db) then perform some analysis on them.

I've gotten far enough to correctly query and return specific nodes as a result but am getting stuck on field_get_items - I can't get the actual field values out. It appears I am not passing the necessary parameters of the bundle to the function.

Where am I going wrong?

Drupal throws this error on the last line of code starting w $output "EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7734 of /home4/xxxxx/public_html/xxxxxx/xxxxxx/includes/common.inc)."

Here's my code:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'event')
  ->propertyCondition('status', 1)
  ->fieldCondition('field_date', 'value', 'NULL', '!=');
$result = $query->execute();

if (isset($result['node'])) {
  $stories = $result['node'];

  // Get all fields attached to a given node type
  $fields = field_info_instances('node', 'event');

  // Get id of body field
  $field_id = $fields['field_date']['field_id'];

   //echo $field_id... testing, works;

  // Attach a field of selected id only to get value for it
echo field_view_field('node', $stories, 'field_date');

  //Get values of our node field
  $output = field_get_items('node', $stories, 'field_date');
}

I get an error on the last step

2

A couple of things...first one is you haven't loaded the nodes. If you change

$stories = $result['node'];

to

$stories = node_load_multiple(array_keys($result['node']));

You'll have the fully loaded node objects available.

The other two things are both related; field_view_field() and field_view_value() both accept a single entity; currently you're trying to pass an array, which it won't like.

A simple loop will fix that:

foreach ($stories as $story) {
  $output[] = field_view_field('node', $story, 'field_date');
}

And similar for field_get_items().

  • Thank you for the suggestion, I tried it and still receive the same "Missing bundle property on entity of type node" error. Is it possible that I'm not declaring the EntityFieldQuery correctly? This content type definitely exists, and I am able to get a result, just not access the field values. – Phil Jul 23 '14 at 17:52
  • I edited about a minute ago, any chance you haven't seen that yet? – Clive Jul 23 '14 at 17:53
0

If you want to use my sandbox you can use EFQE to directly get field data.

$query = new EntityFieldQueryExtra();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'event')
  ->propertyCondition('status', 1)
  // Use inner join so the != NULL condition can be skipped.
  //->fieldCondition('field_date', 'value', 'NULL', '!=');
  ->getFieldValue('field_date', 'value', 'INNER');
$result = $query->executeEntityBuild();

if (isset($result['nodes'])) {
  $nodes = $result['nodes'];
  foreach ($nodes as $node) {
    echo $node->field_date['und'][0]['value'];
  }
}

No entity load was done in the above query. By using executeEntityBuild it will format the results to look like it was loaded via entity load and its placed under 'nodes'. 'node' will contain the data direct from the query. If you use $query->execute then 'nodes' will not be built.

  • Thanks for this, it's actually much easier to understand from a non-drupal dev's perspective and I may use it instead! I believe that @clive did get the specific answer to the question I posted however. – Phil Jul 23 '14 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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