2
  • I have a webform that I want to grade, like a quiz. (No, not with the Quiz module).
  • I have a content type called Answer Key with a Reference field called field_answer_quiz.
  • Upon webform submission, I want to:

    1. Look through all nodes of type Answer Key
    2. Find the one with a field_answer_quiz value == the current webform
    3. Use node_load() or something to pull out content

Here's a Q&A that makes it seem possible: how to extract values of field collection using Entityfieldquery?

But here's a comment that makes it seem impossible:

If that's what you want, then this is the correct way and EntityField does not help you. What EntityFieldQuery does is load entity IDs of entities based on their properties and fields. You could for example use it if you want to load the node(s) that has a specific version value by first loading the field collection item and then loading the node that references the field collection item. You're doing the opposite, you have the node and want the version value. To get field values, you always need to load the entities.

So, which is it? Is it going to be possible for me to use EntityFieldQuery to do this? I'd really like to avoid having to load a ton of nodes just to shuffle through them.

This is the code I am using.

function webformta_webform_validation_validate($validator_name, $items, $components, $rule) {
$arg = arg();
$nid = $arg[1];
dsm('The NID of the current node is '.$nid);
$query = new EntityFieldQuery;
$result = $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('status', 1)
    ->propertyCondition('type', 'quiz_answer_key')     
    ->fieldCondition('field_answer_quiz_nid', 'value', $nid, '=')
    ->execute();
    $results = $query->ordered_results;
  dsm($results);

It causes the following error:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_answer_quiz0.field_answer_quiz_value' in 'where clause': SELECT [...] (error is really really long)

  • Is your field_answer_quiz field attached to the node directly or is it attached to a field collection field type? – Clive May 31 '12 at 13:44
  • I don't know what this means. How would I figure this out? – beth May 31 '12 at 14:06
  • You'd have to set up a field collection yourself so chances are if you're not sure what one is, you haven't. You can ignore the first link you posted as that specifically relates to field collections. Have you come across the How to use EntityFieldQuery page at all? It does a great job of explaining how they work and gives code examples. @Berdir's right though, EntityFieldQuery was pretty much designed for exactly what you're trying to do – Clive May 31 '12 at 14:17
  • 1
    Yes I have seen that documentation, and I used it to create an EntityFieldQuery (inside a hook this time!) that is capable of returning the NIDs of all nodes of type "Answer Key". It's using fieldCondition to get into my custom reference field that's not working, so I was questioning whether I was attempting the wrong method. I'll edit the question to include my code so you can see. – beth May 31 '12 at 14:29
  • 1
    have a look on that field in devel, its almost surely not value. probably nid or so – mojzis May 31 '12 at 14:48
4

Yes, it is possible.

The second question is different, what you are describing is exactly what EFQ can do.

Something like this should work:

->fieldCondition('field_answer_quiz', 'nid', $nid)

The column names in the field tables are a combination of the field name and the column name, you need to split that up like the above example.

  • How can it be done then? For example, when I use field_info_fields() to try to figure out the correct column to use, I don't find the referenced node's value anywhere in the array. (Although yes I realize that field_info_fields() is a function not directly related to the EntityFieldQuery class.) – beth May 31 '12 at 14:00

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.