| 7 node.module | node_load( |
| 4.6 node.module | node_load( |
| 4.7 node.module | node_load($param = array(), $revision = NULL, $reset = NULL) |
| 5 node.module | node_load($param = array(), $revision = NULL, $reset = NULL) |
| 6 node.module | node_load( |
| 8 node.module | node_load($nid = NULL, $reset = FALSE) |
Loads a node object from the database.
Parameters
$nid: The node ID.
$vid: The revision ID.
$reset: Whether to reset the node_load_multiple cache.
Return value
A fully-populated node object, or FALSE if the node is not found.
79 calls to node_load()
- BookTestCase::testBookDelete in modules/
book/ book.test - Tests the access for deleting top-level book nodes.
- book_admin_edit_submit in modules/
book/ book.admin.inc - Form submission handler for book_admin_edit().
- book_block_view in modules/
book/ book.module - Implements hook_block_view().
- book_export in modules/
book/ book.pages.inc - Menu callback; Generates representations of a book page and its children.
- book_export_html in modules/
book/ book.pages.inc - Generates HTML for export when invoked by book_export().
File
- modules/
node/ node.module, line 962 - The core that allows content to be submitted to the site. Modules and scripts may programmatically submit nodes using the usual form API pattern.
Code
function node_load($nid = NULL, $vid = NULL, $reset = FALSE) {
$nids = (isset($nid) ? array($nid) : array());
$conditions = (isset($vid) ? array('vid' => $vid) : array());
$node = node_load_multiple($nids, $conditions, $reset);
return $node ? reset($node) : FALSE;
}
Comments
node_load with $params
Permalinknode_load() with params is deprecated in D7 in favor of EntityFieldQuery().
If you had a code in D6 to load a node using an array of conditions to match against in the database query like this:
<?php$param = array(
'type' => 'your_node_type',
'title' => 'your node title',
'status' => 1,
);
$node = node_load($param);
?>
then in D7 you ll have to use:
<?php$query
= new EntityFieldQuery();$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'your_node_type')
->propertyCondition('title', 'your node title')
->propertyCondition('status', 1)
->range(0,1)
->execute();
if (!empty(
$entities['node'])) {$node = node_load(array_shift(array_keys($entities['node'])));
}
?>
current() instead of array_shift()
PermalinkTamerzg, thank you for very good example. It's very helpful!
Using:
<?phparray_shift(array_keys($entities['node']))
?>
gets me an notice: "Strict warning: Only variables should be passed by reference in ..."
I think It should be replaced with:
<?phpcurrent(array_keys($entities['node']))
?>
I've found a solution
here
Thanks for the awesome
PermalinkThanks for the awesome example! I just don't understand the advantage of all that when I can simply do:
$nid = db_select('node', 'n')->condition('n.type', 'your_node_type')->condition('n.title', 'your node title')->condition('n.status', 1)->fields('n', array('nid'))->execute()->fetchField();if($nid !== FALSE) { $node = node_load($nid); }
Can you enlighten me on the advantages of using EntityFieldQuery() ?
The primary benefit is that
PermalinkThe primary benefit is that you're not just selecting a node from the db. You are instead selecting entities.
For more info on this checkout http://api.drupal.org/api/drupal/includes%21entity.inc/class/EntityField...
Basically this allows another module a hook point to alter the entity.
how to use this?
PermalinkActually, i connect to a drupal6 's database named 'drupal6DB',
and i want to call the drupal6's node_load() to fetch the record from the drupal6DB,
So, how can i use the code ?
Load only with revision ID
PermalinkIf you only have an revision id you can use:
node_load(null, $revision_id);
Try menu_get_object()
PermalinkIf the node you wish to load is the current node being loaded by the page, then you can use menu_get_object() which is much less resource intensive, mainly because it is cached.
http://api.drupal.org/api/function/menu_get_object/7
Right, but... Be careful on hook_node_update()
PermalinkYou are right, menu_get_object() should be used when you want to get the node which is loaded on the current page. But it is not less resource intensive, it's even more hungry than node_load(), see this comparison. node_load() also uses caching.
When you use node_load() on hook_node_update(), you should reset node_load_multiple cache ($reset = TRUE) to get valid node data. See this comment for more information.
comment link
PermalinkSorry, comment link from above is broken. Once again: comment