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
)
)