/* drucafe */

Tuesday, September 30, 2014

Helpful node API functions for showing node programmatically from scratch

Here is the Drupal 7 node API page with plenty of functions.  I found some of those functions more useful than others, while writing code which displays the nodes programmatically from scratch.



Function call Return value Description
node_access($op, $node, $account = NULL) TRUE if the operation may be performed, FALSE otherwise. Determines whether the current user may perform the operation on the node.
node_build_content($node, $view_mode = 'full', $langcode = NULL) Changes the $node->content Builds a structured array representing the node's content.
node_load($nid = NULL, $vid = NULL, $reset = FALSE) A fully-populated node object, or FALSE if the node is not found. Loads a node object from the database.
node_load_multiple($nids = array(), $conditions = array(), $reset = FALSE) An array of node objects indexed by nid. Loads node entities from the database.
node_page_view($node) A page array suitable for use by drupal_render(). Displays a single node.
node_show($node, $message = FALSE) A $page element suitable for use by drupal_render(). Generates an array which displays a node detail page.
node_title_list($result, $title = NULL) A renderable array containing a list of linked node titles fetched from $result, or FALSE if there are no rows in $result. Gathers a listing of links to nodes.
node_view($node, $view_mode = 'full', $langcode = NULL) An array as expected by drupal_render(). Generates an array for rendering the given node.
node_view_multiple($nodes, $view_mode = 'teaser', $weight = 0, $langcode = NULL) An array in the format expected by drupal_render(). Constructs a drupal_render() style array from an array of loaded nodes.
hook_node_view_alter(&$build, $context1) Changes $build Alter the results of node_view().
hook_node_view($node, $view_mode) Changes $node Act on a node that is being assembled before rendering.

this function ... calls this function
node_page_view node_show
node_show node_view_multiple
node_view_multiple node_view
node_view node_build_content
hook_node_view_alter
node_build_content hook_node_view
node_load node_load_multiple
node_load_multiple
node_access
node_title_list

Possible call sequence for programmatically displaying a node with given id

Inside mymodule_menu callback:
$node = node_load($nid);
$render = node_show($node);
return drupal_render($render);

No comments:

Post a Comment