1

A call to field_attach_load should be loading node titles, but they are not being loaded. Perhaps I am missing something simple, since this my first Drupal hook.

This is the code I am using to query all the published Event nodes and their associated fields.

$query = new EntityFieldQuery();
$results = $query->entityCondition('entity_type', 'node')
  ->propertyCondition('type', 'event')
  ->propertyCondition('status', NODE_PUBLISHED)
  ->execute();


  // Attach the field values to the nodes.
  $nodes = $results['node'];

  field_attach_load('node', $nodes, FIELD_LOAD_CURRENT);
  var_dump($nodes);

Unfortunately, this code doesn't work for me.

How do I get the list of nodes of a specific content type and their titles?

4
  • Take a step back, what information do you need? If you are using EntityFieldQuery, why would you want to use field_attach_load? The point of EntityFieldQuery is to get out of the slow drupal loading process. What are you trying to achieve?
    – iLLin
    Commented Feb 20, 2017 at 14:04
  • I want a list of "event" node ids and their corresponding title. That is my end goal.
    – Robin
    Commented Feb 21, 2017 at 1:28
  • 1
    May check this link out, has good stuff in it. drupal.stackexchange.com/questions/18093/…
    – iLLin
    Commented Feb 21, 2017 at 18:06
  • Also, could you use views to display this?
    – iLLin
    Commented Feb 21, 2017 at 18:06

3 Answers 3

0

PLease try this- This will load all published nodes of type event.

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'event')
->propertyCondition('status', 1);

$result = $query->execute();

if (!empty($result['node'])) {
$nids = array_keys($result['node']);

foreach ($nids as $nid) {
$node = node_load($nid, NULL, TRUE);
// do something awesome
 }
}
2
  • Was I misusing field_attach_load?
    – Robin
    Commented Feb 20, 2017 at 13:06
  • No that was not a misuse, actually you need to debug your code for checking out results. Commented Feb 20, 2017 at 13:48
0

From the field_attach_load documentation page:

$entities: An array of entities for which to load fields, keyed by entity ID. Each entity needs to have its 'bundle', 'id' and (if applicable) 'revision' keys filled in.

While your array has the required id and is keyed by id, it is missing the bundle for those nodes (in your case 'event').

So you'd either manually add this key to every array item, or use the answer provided by 'Rahul Mishra'. While abit more hard on the database, it highly depends on your use case (row count, frequency, etc.).

1
  • An entity defines which keys contain the entity bundle in hook_entity_info(). field_attach_load() and every Drupal core functions handling entities don't assume the bundle name is contained in the bundle key, so there isn't any need to add a bundle key in the array returned from EntityFieldQuery::execute().
    – avpaderno
    Commented May 3, 2019 at 12:45
0

field_attach_load() doesn't load the node title, as that isn't an entity field. It loads the body field and every field created with the Field UI module.

In a Drupal 7 test site, I created two Article nodes and one Page node. I then executed the following code.

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

if (!empty($result['nodes'])) {
  $nodes = $result['node'];
  field_attach_load('node', $nodes);
}

echo '<pre>', print_r($nodes, TRUE), '</pre>';

The output has been the following one.

Array
(
    [1] => stdClass Object
        (
            [nid] => 1
            [vid] => 1
            [type] => article
            [body] => Array
                (
                    [und] => Array
                        (
                            [0] => Array
                                (
                                    [value] => Test 1, before test 2.
                                    [summary] => 
                                    [format] => filtered_html
                                    [safe_value] => Test 1, before test 2.
                                    [safe_summary] => 
                                )
                        )
                )
            [field_tags] => Array
                (
                )
            [field_image] => Array
                (
                )
        )
    [2] => stdClass Object
        (
            [nid] => 2
            [vid] => 2
            [type] => article
            [body] => Array
                (
                    [und] => Array
                        (
                            [0] => Array
                                (
                                    [value] => Test 2: the test before Test 3 and Test 4.
                                    [summary] => 
                                    [format] => filtered_html
                                    [safe_value] => Test 2: the test before Test 3 and Test 4.
                                    [safe_summary] => 
                                )
                        )
                )
            [field_tags] => Array
                (
                )
            [field_image] => Array
                (
                )
        )
)

The code to get the node title should be similar to the following one.

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

if (!empty($result['nodes'])) {
  $nids = array_keys($result['node']);
  $nodes = node_load_multiple($nids);
}

In my test site, I get the following output.

Array
(
    [1] => stdClass Object
        (
            [vid] => 1
            [uid] => 1
            [title] => Test 1
            [log] => 
            [status] => 1
            [comment] => 2
            [promote] => 1
            [sticky] => 0
            [nid] => 1
            [type] => article
            [language] => und
            [created] => 1556873868
            [changed] => 1556873868
            [tnid] => 0
            [translate] => 0
            [revision_timestamp] => 1556873868
            [revision_uid] => 1
            [body] => Array
                (
                    [und] => Array
                        (
                            [0] => Array
                                (
                                    [value] => Test 1, before test 2.
                                    [summary] => 
                                    [format] => filtered_html
                                    [safe_value] => Test 1, before test 2.
                                    [safe_summary] => 
                                )
                        )
                )
            [field_tags] => Array
                (
                )
            [field_image] => Array
                (
                )
            [rdf_mapping] => Array
                (
                    [field_image] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => og:image
                                    [1] => rdfs:seeAlso
                                )
                            [type] => rel
                        )
                    [field_tags] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:subject
                                )
                            [type] => rel
                        )
                    [rdftype] => Array
                        (
                            [0] => sioc:Item
                            [1] => foaf:Document
                        )
                    [title] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:title
                                )
                        )
                    [created] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:date
                                    [1] => dc:created
                                )
                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )
                    [changed] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:modified
                                )
                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )
                    [body] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => content:encoded
                                )
                        )
                    [uid] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:has_creator
                                )
                            [type] => rel
                        )
                    [name] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => foaf:name
                                )
                        )
                    [comment_count] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:num_replies
                                )
                            [datatype] => xsd:integer
                        )
                    [last_activity] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:last_activity_date
                                )
                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )
                )
            [cid] => 0
            [last_comment_timestamp] => 1556873868
            [last_comment_name] => 
            [last_comment_uid] => 1
            [comment_count] => 0
            [name] => admin
            [picture] => 0
            [data] => b:0;
        )
    [2] => stdClass Object
        (
            [vid] => 2
            [uid] => 1
            [title] => Test 2
            [log] => 
            [status] => 1
            [comment] => 2
            [promote] => 1
            [sticky] => 0
            [nid] => 2
            [type] => article
            [language] => und
            [created] => 1556873931
            [changed] => 1556873931
            [tnid] => 0
            [translate] => 0
            [revision_timestamp] => 1556873931
            [revision_uid] => 1
            [body] => Array
                (
                    [und] => Array
                        (
                            [0] => Array
                                (
                                    [value] => Test 2: the test before Test 3 and Test 4.
                                    [summary] => 
                                    [format] => filtered_html
                                    [safe_value] => Test 2: the test before Test 3 and Test 4.
                                    [safe_summary] => 
                                )
                        )
                )
            [field_tags] => Array
                (
                )
            [field_image] => Array
                (
                )
            [rdf_mapping] => Array
                (
                    [field_image] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => og:image
                                    [1] => rdfs:seeAlso
                                )
                            [type] => rel
                        )
                    [field_tags] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:subject
                                )
                            [type] => rel
                        )
                    [rdftype] => Array
                        (
                            [0] => sioc:Item
                            [1] => foaf:Document
                        )
                    [title] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:title
                                )
                        )
                    [created] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:date
                                    [1] => dc:created
                                )
                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )
                    [changed] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:modified
                                )
                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )
                    [body] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => content:encoded
                                )
                        )
                    [uid] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:has_creator
                                )
                            [type] => rel
                        )
                    [name] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => foaf:name
                                )
                        )
                    [comment_count] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:num_replies
                                )
                            [datatype] => xsd:integer
                        )
                    [last_activity] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:last_activity_date
                                )
                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )
                )
            [cid] => 0
            [last_comment_timestamp] => 1556873931
            [last_comment_name] => 
            [last_comment_uid] => 1
            [comment_count] => 0
            [name] => admin
            [picture] => 0
            [data] => b:0;
        )
)

As side note, for nodes, the following snippets return the same value, in $result['node'].

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

In my test site, in both the cases, echo '<pre>', print_r($result['node'], TRUE), '</pre>'; outputs the following.

Array
(
    [1] => stdClass Object
        (
            [nid] => 1
            [vid] => 1
            [type] => article
        )
    [2] => stdClass Object
        (
            [nid] => 2
            [vid] => 2
            [type] => article
        )
)

The second snippet is always valid for any entity type (and it is then preferable); the first snippet works only for those entities for which the entity type is stored in a type database field.

For nodes, don't use the following code.

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

It would throw the following exception.

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node.bundle' in 'where clause': SELECT node.nid AS entity_id, node.vid AS revision_id, node.type AS bundle, :entity_type AS entity_type FROM {node} node WHERE (node.bundle = :db_condition_placeholder_0) AND (node.status = :db_condition_placeholder_1) ; Array ( [:db_condition_placeholder_0] => article [:db_condition_placeholder_1] => 1 [:entity_type] => node )

Don't even use the following code.

$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
  ->entityCondition('type', 'article')
  ->propertyCondition('status', NODE_PUBLISHED)
  ->execute();

It would return every node (entity), independently from its content type (bundle). In my test site, the content of $result['node'] with that code is the following.

Array
(
    [1] => stdClass Object
        (
            [nid] => 1
            [vid] => 1
            [type] => article
        )
    [2] => stdClass Object
        (
            [nid] => 2
            [vid] => 2
            [type] => article
        )
    [3] => stdClass Object
        (
            [nid] => 3
            [vid] => 3
            [type] => page
        )
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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