3

I'm still confused about the entity concept in Drupal 7. I have a node type audio with an MP3 field. I'm able to use EntityFieldQuery to find all the audio nodes, but I need the URL of the MP3, which is stored in the "file_managed" table, and I don't find a way to retrieve it.

This is what I have:

// Query audio nodes
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'audio')
  ->propertyCondition('status', 1);
$result = $query->execute();

There is no URL in the $result array. At least I can't find it there; I only get the fid.

2
  • I usually use EntityFieldQuery + entity_load to get data. However, I'm stuck in the API for querying specific entity fields. For instance if I want "Field A" from all 10,000 "Foo" entities I'd use EntityFieldQuery to get the entity_stub for the 10,000 records and do a entity_load (alot of values). Is there a better way to load only specific fields and not have the overhead of fully loading an entity. I could use db_select to get a SelectQuery object but this requires explicitly passing the table name and that seems to defeat the purpose of allowing entities to resides in separate DBs than the
    – John House
    Jun 25, 2011 at 21:24
  • Question seems related to drupal.stackexchange.com/questions/18093/…
    – mikeytown2
    Jan 24, 2013 at 20:43

2 Answers 2

3

When EntityFieldQuery::count() is not invoked, what returned from EntityFieldQuery::execute() is an array having the following structure:

[entity type] => array([entities])
[entity] => array([relevant ID] => [entity object])

The relevant ID is normally the entity ID, as in the query you reported. See entity_create_stub_entity(), and entity_get_info() to understand what information the entity object contains.

To get all the fields associated with the entity you are querying, you just need to execute the following code.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'audio')
  ->propertyCondition('status', 1);
$result = $query->execute();

if (!empty($result['node'])) {
  $nodes = entity_load('node', array_keys($result['node']);
}
4
  • OK, got that. Maybe there is another way to get the related file uri with the help of the file id which I have in the node array? Sorry, if my question sounds stupid, but I'm still a novice...
    – krabbe
    Jun 4, 2011 at 14:32
  • @krabbe You could query the table containing the field data, but I would not recommend doing that, at least for two reasons: the code would be dependent from the table structure, while using the Drupal API you would not have this problem, even passing to Drupal 8 or future releases; the structure of the table could be altered by another module, or the result of the EntityFieldQuery query could be altered from a module, and directly querying the field table you could get a different result.
    – apaderno
    Jun 4, 2011 at 14:50
  • 1
    @krabbe Once you have the fid, you can use api.drupal.org/api/drupal/includes--file.inc/function/file_load/… to load the file object which will contain the uri to the file.
    – Berdir
    Jun 4, 2011 at 15:48
  • @Berdir That's exactly what I was looking for. Thank you so much.
    – krabbe
    Jun 4, 2011 at 18:30
1

EntityFieldQuery doesn't allow for adding addition field data to the query result. You can load the entities afterwards. From the documentation:

$result = $query->execute();
$entities = entity_load($my_type, array_keys($result[$my_type]));

In the above example, $mt_type is your entity type (node).

4
  • To load the node is not the issue. Problem is the url of the mp3. It's not in the result of EntityFieldQuery. I only get an array inside the node called field_mp3 with the file id.
    – krabbe
    Jun 4, 2011 at 14:00
  • 3
    It is not supposed to be.EFQ only loads entity types and id's. To load the corresponding entities, you need to use entity_load() or one of the direct load functions like node_load() as googletorp wrote.
    – Berdir
    Jun 4, 2011 at 14:02
  • And is it possible to load only one field data, without to load entire node ? Dec 22, 2011 at 9:40
  • @Fedir Anything is possible, but this is not something you want to do - It will be extremely hard to do, compared to node_load and sometimes doing node_load is more performance efficient (it can be cached) or it's just about the same load.
    – googletorp
    Dec 22, 2011 at 10:21

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .