node_load
- 4.6
node_load($conditions, $revision = NULL, $reset = NULL)
- 4.7 – 6
node_load($param = array(), $revision = NULL, $reset = NULL)
- 7
node_load($nid = NULL, $vid = NULL, $reset = FALSE)
Load a node object from the database.
Parameters
$param
Either the nid of the node or an array of conditions to match against in the database query
$revision
Which numbered revision to load. Defaults to the current version.
$reset
Whether to reset the internal node_load cache.
Return value
A fully-populated node object.
- batch_example_op_1 in developer/examples/batch_example.module
- Batch operation for batch 1 : lode a node...
- batch_example_op_2 in developer/examples/batch_example.module
- Batch operation for batch 2 : load all nodes, 5 by five
This is a multipart operation, using the
- blogapi_blogger_edit_post in modules/blogapi/blogapi.module
- Blogging API callback. Modifies the specified blog node.
- blogapi_blogger_get_post in modules/blogapi/blogapi.module
- Blogging API callback. Returns a specified blog node.
- blogapi_mt_get_post_categories in modules/blogapi/blogapi.module
- Blogging API callback. Returns a list of the taxonomy terms that are
assigned to a particular node.
- blogapi_mt_publish_post in modules/blogapi/blogapi.module
- Blogging API callback. Publishes the given node
- blogapi_mt_set_post_categories in modules/blogapi/blogapi.module
- Blogging API callback. Assigns taxonomy terms to a particular node.
- blog_form in modules/blog/blog.module
- Implementation of hook_form().
- blog_page_last in modules/blog/blog.pages.inc
- Menu callback; displays a Drupal page containing recent blog entries of all users.
- blog_page_user in modules/blog/blog.pages.inc
- Menu callback; displays a Drupal page containing recent blog entries of a given user.
- book_admin_edit_submit in modules/book/book.admin.inc
- Handle submission of the book administrative page form.
- book_export_html in modules/book/book.pages.inc
- This function is called by book_export() to generate HTML for export.
- book_export_traverse in modules/book/book.module
- Traverse the book tree to build printable or exportable output.
- book_form_node_delete_confirm_alter in modules/book/book.module
- Form altering function for the confirm form for a single node deletion.
- book_nodeapi in modules/book/book.module
- Implementation of hook_nodeapi().
- comment_form in modules/comment/comment.module
- Generate the basic commenting form, for appending to a node or display on a separate page.
- comment_form_add_preview in modules/comment/comment.module
- Form builder; Generate and validate a comment preview form.
- comment_form_submit in modules/comment/comment.module
- Process comment form submissions; prepare the comment, store it, and set a redirection target.
- comment_links in modules/comment/comment.module
- Build command links for a comment (e.g.\ edit, reply, delete) with respect to the current user's access permissions.
- comment_validate in modules/comment/comment.module
- Validate comment data.
- hook_search in developer/hooks/core.php
- Define a custom search routine.
- hook_update_index in developer/hooks/core.php
- Update Drupal's full-text index for this module.
- node_access_rebuild in modules/node/node.module
- Rebuild the node access database. This is occasionally needed by modules
that make system-wide changes to access levels.
- node_delete in modules/node/node.module
- Delete a node.
- node_feed in modules/node/node.module
- A generic function for generating RSS feeds from a set of nodes.
- node_help in modules/node/node.module
- Implementation of hook_help().
- node_page_default in modules/node/node.module
- Menu callback; Generate a listing of promoted nodes.
- node_search in modules/node/node.module
- Implementation of hook_search().
- poll_block in modules/poll/poll.module
- Implementation of hook_block().
- poll_cancel in modules/poll/poll.module
- Submit callback for poll_cancel_form
- profile_block in modules/profile/profile.module
- Implementation of hook_block().
- statistics_node_tracker in modules/statistics/statistics.pages.inc
- system_message_action in modules/system/system.module
- A configurable Drupal action. Sends a message to the current user's screen.
- system_send_email_action in modules/system/system.module
- Implementation of a configurable Drupal action. Sends an email.
- taxonomy_render_nodes in modules/taxonomy/taxonomy.module
- Accepts the result of a pager_query() call, such as that performed by
taxonomy_select_nodes(), and formats each node along with a pager.
- translation_nodeapi in modules/translation/translation.module
- Implementation of hook_nodeapi().
- translation_node_overview in modules/translation/translation.pages.inc
- Overview page for a node's translations.
- translation_path_get_translations in modules/translation/translation.module
- Return paths of all translations of a node, based on
its Drupal path.
- upload_file_download in modules/upload/upload.module
- Implementation of hook_file_download().
- _node_access_rebuild_batch_operation in modules/node/node.module
- Batch operation for node_access_rebuild_batch.
- _node_index_node in modules/node/node.module
- Index a single node.
- _node_mass_update_helper in modules/node/node.admin.inc
- Node Mass Update - helper function.
- _node_revision_access in modules/node/node.module
- _trigger_normalize_comment_context in modules/trigger/trigger.module
- When an action is called in a context that does not match its type,
the object that the action expects must be retrieved. For example, when
an action that works on nodes is called during the comment hook, the
node object is not available since the...
- _trigger_normalize_user_context in modules/trigger/trigger.module
- When an action is called in a context that does not match its type,
the object that the action expects must be retrieved. For example, when
an action that works on nodes is called during the user hook, the
node object is not available since the user...
Code
modules/node/node.module, line 696
<?php
function node_load($param = array(), $revision = NULL, $reset = NULL) {
static $nodes = array();
if ($reset) {
$nodes = array();
}
$cachable = ($revision == NULL);
$arguments = array();
if (is_numeric($param)) {
if ($cachable) {
if (isset($nodes[$param])) {
return is_object($nodes[$param]) ? drupal_clone($nodes[$param]) : $nodes[$param];
}
}
$cond = 'n.nid = %d';
$arguments[] = $param;
}
elseif (is_array($param)) {
foreach ($param as $key => $value) {
$cond[] = 'n.'. db_escape_table($key) ." = '%s'";
$arguments[] = $value;
}
$cond = implode(' AND ', $cond);
}
else {
return FALSE;
}
$fields = drupal_schema_fields_sql('node', 'n');
$fields = array_merge($fields, drupal_schema_fields_sql('node_revisions', 'r'));
$fields = array_merge($fields, array('u.name', 'u.picture', 'u.data'));
$fields = array_diff($fields, array('n.vid', 'n.title', 'r.nid'));
$fields = implode(', ', $fields);
$fields = str_replace('r.timestamp', 'r.timestamp AS revision_timestamp', $fields);
$fields = str_replace('r.uid', 'r.uid AS revision_uid', $fields);
if ($revision) {
array_unshift($arguments, $revision);
$node = db_fetch_object(db_query('SELECT '. $fields .' FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = %d WHERE '. $cond, $arguments));
}
else {
$node = db_fetch_object(db_query('SELECT '. $fields .' FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE '. $cond, $arguments));
}
if ($node && $node->nid) {
if ($extra = node_invoke($node, 'load')) {
foreach ($extra as $key => $value) {
$node->$key = $value;
}
}
if ($extra = node_invoke_nodeapi($node, 'load')) {
foreach ($extra as $key => $value) {
$node->$key = $value;
}
}
if ($cachable) {
$nodes[$node->nid] = is_object($node) ? drupal_clone($node) : $node;
}
}
return $node;
}
?>
Login or
register to post comments
Return = False if nid is not valid
Note that this function returns a (bool) FALSE if the node is not found.
Loading lots of nodes
If you are loading a *lot* of nodes with node_load(), make sure to use the $reset parameter so that every node isn't kept in the function's static cache (and increasing memory usage):
<?phpnode_load($nid, NULL, TRUE);
?>
Specifically, what that does
Specifically, what that does is clear node_load()'s cache before adding the node to be loaded to it. That means that there will always be at least one loaded node in the cache if you load nodes this way. If you want to clear the cache for good, try:
<?phpnode_load(FALSE, NULL, TRUE);
?>
…which will return FALSE. (The first parameter could also be NULL, or anything else that isn't an array, number, or numeric string.)