5

Suppose, we have an entity "song", that has an entity reference field, called "authors":

enter image description here

I added a filter for this field as a group of checkboxes:

enter image description here

After the user submits the form with checkboxes group, it must be used inside WHERE condition for the list of songs. So, I added the following code:

//query to get the list of entities
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'song');

if(!is_null($form_state['input']['field_authors'][LANGUAGE_NONE]))
{
    $query->fieldCondition('field_authors', /* what to put here? */, 
            $form_state['input']['field_authors'][LANGUAGE_NONE], 'in');
}

I don't understand, what value to put into column parameter. I tried target_id, but it doesn't work. Here is the description of field_data_field_authors table:

DESCRIBE field_data_field_authors;
+-------------------------+------------------+------+-----+---------+-------+
| Field                   | Type             | Null | Key | Default | Extra |
+-------------------------+------------------+------+-----+---------+-------+
| entity_type             | varchar(128)     | NO   | PRI |         |       |
| bundle                  | varchar(128)     | NO   | MUL |         |       |
| deleted                 | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id               | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id             | int(10) unsigned | YES  | MUL | NULL    |       |
| language                | varchar(32)      | NO   | PRI |         |       |
| delta                   | int(10) unsigned | NO   | PRI | NULL    |       |
| field_authors_target_id | int(10) unsigned | NO   | MUL | NULL    |       |
+-------------------------+------------------+------+-----+---------+-------+
  • 2
    Take the database column name for the value (field_authors_target_id), remove the name of the field (field_authors), and you're left with the column name (target_id). That recipe will work for any type of field. If it's not working, your problem is somewhere else (e.g. $form_state['input']['field_authors'][LANGUAGE_NONE] doesn't contain what you're expecting) – Clive Aug 20 '14 at 14:07
  • It takes the field name of the table that we need to compare. So you can use 'field_authors_target_id' – Ashish T Aug 20 '14 at 14:18
3

You need to use 'target_id'

Your code should by this:

//query to get the list of entities
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'song');

if(!is_null($form_state['input']['field_authors'][LANGUAGE_NONE]))
{
    $query->fieldCondition('field_authors', 'target_id', 
            $form_state['input']['field_authors'][LANGUAGE_NONE], 'in');
}

You can found a good explanation here:

How to use EntityFieldQuery with a simple text field?

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.