You are here

Da Basics: Calling the Node

So Drupal's automagical in so many ways and you can let it work its magic and come up with a pretty darn good site. But you're not satisfied with said outcome; you want to go beyond the pretty darn good and do something special, or some function specifically needed or something that came to you in a dream.

And so you have to do what Drupal normally does in places in which Drupal ain't doing it.

It's helpful, in fact, to know more or less how Drupal does its basic functionality. Even if you don't use them, these code pieces can help you understand how Drupal works.

The following things appear on just about every website I work on. In fact, they're so dear to me that I sometimes use these rather than rely on Drupal core (which is a big of a heresy but there you have it). I call these code pieces "the basics".

The Node Basics

Say you want a page made up of several Drupal nodes -- maybe nodes of a particular type -- and you don't want to rely on taxonomy or any of that.

Here's how Drupal calls its nodes for a type called "blog" in a page setting:

$sql=db_query("SELECT * from node where type='blog'");
while ($anode=db_fetch_object($sql)) {
$output = node_view(node_load(array('nid' => $anode->nid)),$teaser=TRUE);
print $output;
}

This will produce fully displayed nodes with a teaser (if you don't want a teaser you just do $teaser=FALSE or just leave it out.

But say you have like 50 nodes you want to display and you need to put in that handy little "pager" system Drupal has (a pretty wonderful one at that, I'd say).

Here's that code -- same type.

$sqlcount=("SELECT COUNT(*) from node where type='blog'");
$listlength=10;
$sql=pager_query("SELECT * from node where type='blog'",$listlength,0,$sqlcount);
while ($anode=db_fetch_object($sql)) {
$output = node_view(node_load(array('nid' => $anode->nid)),$teaser=TRUE);
print $output;
}
echo theme('pager', $listlength, 0);
?>

I think it may be time to break stuff down and so I did the page one so I could break down everything at once since it includes the simple code in it.

$sqlcount is making a basic php count call -- counting the number of nodes of type blog.
$listlength is the length of the list you want on every page.

That stuff's done only to get feel for how many nodes are in play and how many you want to display. :-)

The heavy-duty sql call, this one:

$sql=pager_query("SELECT * from node where type='blog'",$listlength,0,$sqlcount);

is a normal db_query function with some added drupal goodies in it. It does the selection and it also passes to drupal core the all the info it needs to make a division (how many there are and how many on a page). That zero in the middle is so you can assign values to different pager instances on a page -- I ignore it and just put zero in there to not complicate my life.

Then you have a few drupal ditties:

while ($anode=db_fetch_object($sql)) {
$output = node_view(node_load(array('nid' => $anode->nid)),$teaser=TRUE);
print $output;
}

?>

which is the loop we use to turn the sql call into an object; you can also use db_fetch_array but Drupal uses objects much more efficiently so when you have access to one, use it.

And in that loop is Drupal's powerhouse for nodes:

$output = node_view(node_load(array('nid' => $anode->nid)),$teaser=TRUE);
print $output;
}

?>

Can any music be as sweet. The cousins of node display, node_view and node_load, work together to get everything connected with a node and I mean everything. If your type is a cck type with like 40 fields, these guys will pull all that info for you and display it. All of it.

These functions are completely amendable to styling through a customized template so you don't have to put any style code at all here. Call it and do the template the way you want for that type and Voila!!!!

And then you close the loop, as they say

and THEN you finish off with your pager's footer: the thing that shows the page numbers, which one you're on and gives all kinds of back and forth options. It's sweet.

echo theme('pager', $listlength, 0);
?>

But where's the page function??? Well, drupals pager function is actually written into the above line. The theme function takes a first argument that defines what it is actually theming and pager is one allowed argument. When it sees that it calls the function

theme_pager

which does all the work. And then it allows you establish the listlength you want theme_pager to divide and display by.

Basic and that's the way Drupal does it. You can include this code in any page you want, any function you want or anyplace you want on a site and it will return what's needed.