Drupal 7 Snippets - Load nodes
Load nodes easily using EntityFieldQuery
The EntityFieldQuery API is very useful to load nodes. This snippet allows to use it very easily:
/**
* Load nodes
*
* @param string/array $type Type of node (article, document...)
* @param integer $limit Number of nodes to load
* @param boolean $pager Display or not a pager
* @param array $conditions An array of conditions to pass to EntityFieldQuery
* @return array An array of loaded nodes or an empty array
*
*/
function my_module_load_nodes($type, $limit = 10, $pager = FALSE, $conditions = array()) {
$query = new EntityFieldQuery();
if ($pager === true) {
$query->entityCondition('entity_type', 'node')->pager($limit);
} else {
$query->entityCondition('entity_type', 'node')->range(0, $limit);
}
if (is_string($type)) {
$query->propertyCondition('type', $type);
} elseif (is_array($type)) {
$query->propertyCondition('type', $type, 'IN');
}
if (is_array($conditions)) {
foreach ($conditions as $condition) {
if (isset($condition['method']) && isset($condition['params'])) {
call_user_func_array(array($query, $condition['method']), $condition['params']);
}
}
}
$query->propertyCondition('status', 1);
$results = $query->execute();
if (!empty($results['node'])) {
return node_load_multiple(array_keys($results['node']));
}
return array();
}
How to use it:
$conditions[] = array('method' => 'propertyOrderBy', 'params' => array('sticky', 'DESC'));
$conditions[] = array('method' => 'propertyOrderBy', 'params' => array('created', 'DESC'));
$conditions[] = array('method' => 'fieldCondition', 'params' => array('my_field_name', 'tid', 'my_term_id'));
$conditions[] = array('method' => 'fieldCondition', 'params' => array('my_field_name', 'value', 'my_value'));
$nodes = my_module_load_nodes('article', 30, TRUE, $conditions);
$variables['nodes'] = $nodes;
$variables['pager'] = theme('pager');
return theme('my_theme', $variables);
Load nodes from a nodequeue list
Nodequeue module allows users to make ordered lists of nodes via the Drupal administration. This function load a list of nodes by giving its name:
/**
* Load nodes from a queue with its "machine_name"
*
* @param string $machine_name Queue machine_name
* @param integer $from The node from which to start
* @param integer $count The number of nodes to be loaded
* @return array() An array of loaded nodes or an empty array
*/
function my_module_nodequeue_load_nodes($machine_name, $from = 0, $count = 10) {
$queue = nodequeue_load_queue_by_name($machine_name);
if (!empty($queue)) {
$subqueue = current(nodequeue_load_subqueues_by_queue($queue->qid));
if (!empty($subqueue->sqid)) {
$query = db_select('node', 'n')
->fields('n', array('nid'))
->condition('nn.sqid', $subqueue->sqid)
->orderBy('nn.position', 'ASC');
$query->join('nodequeue_nodes', 'nn', 'n.nid = nn.nid');
$query->condition('n.status', 1);
$result = $query->range($from, $count)->execute()->fetchCol();
$data = array();
foreach ($result as $key => $nid) {
$data[$nid] = node_load($nid);
}
return $data;
}
}
return array();
}
How to use it:
$nodes = my_module_nodequeue_load_nodes('my_list');