When we first decided to start enlightening the nerdlings of the world, we realized pretty quickly that Drupal was the best choice for us. For about a week, we flirted with the idea of just setting up a simple Wordpress blog, but it didn't take long before we figured out it simply didn't fit our needs.
That's not to say Wordpress isn't an outstanding product. On the contrary, I use it for my own personal blog (http://justinstanley.net) and have nothing but great things to say about it in that context. But once you get a few steps past straight-bloggin', WP doesn't cut it.
So on the seventh day, we migrated our first few posts over to Drupal, and it was good.
Thing about Drupal is, though, that its greatest strength is also its biggest weakness. It's a wonderfully powerful CMS that you can use in about as many different ways as you can imagine... if you can only figure out how.
Getting the thing installed and running is a piece of cake, but then what? Well, in our case, the answer was pretty simple: cut Jeremy loose and let him have it 'er. You, however, might not have that option.
And that, my friends, is why we're here today: to help you get the most out of Drupal.
This here'll be the first of many articles that'll break down how we use Drupal at Nerdliness.com to deliver the goods. Jeremy and I will kick down the knowledge we've gained while getting this place up and running and try to help you avoid some of the pitfalls we discovered along the way. And if you have any questions of your own, feel free to send them to AskANerd@nerdliness.com.
So let's get this party started. One of the first things we wanted to do when we started working on this site was to figure out a way to style the latest entry on the front page differently than the older posts. For instance, you might notice that the teaser for the newest post on the front page is longer than any of the others. Well, there isn't a way to do that by default. No simple menu option or anything in the Drupal admin pages that give you that ability. Obviously, though, there is a way.
In your theme directory, you'll have a file called node.tpl.php, and Drupal uses that file to format the different nodes you create by default. You can, however, create different versions of that node file for different types of nodes. For example, on our site, we've used CCK to create a new content type called an "article" that includes fields that aren't in the default types (like our subheading and the References section). To handle just that content type, we've created a file called node-article.tpl.php in our theme directory, and Drupal knows that it has to use that file for any "article"-type nodes it publishes. You can have those individual files for each type, or you can just use the node.tpl.php file for all of them. Your call.
Ok, so we needed to find some way to get Drupal to figure out when a particular node was the most recent. Do do that, we added a little PHP to the top of that node-article.tpl.php file:
<?php $maxnodeid = db_result(db_query("SELECT MAX(nid) FROM node where type = 'article' and status = 1 and promote = 1 order by created desc")); ?>
Essentially, we're using two built-in Drupal functions to find the nid value for the node in our table that's active, been promoted to the front page, has the latest creation date, and was created using our "article" type. We then assign that number to the PHP variable, $maxnodeid.
You PHP-types out there might be wondering why I bothered using the built-in Drupal functions to make the database call instead of the PHP functions (like mysql_connect and mysql_fetch_assoc). In my case, it's laziness (and, yeah, security). Notice that we didn't have to specify any connection information? No server name, no user name, no password? Those are defined already in your Drupal config, so no need to include them again here.
Also, this method is database independent. If, for some reason, I decide to swith from MySQL to PostgreSQL in the future, I won't have to adjust my theme files to account for the difference.
So now that I know the nid for the newest article, I just need to compare that value to the nid of any articles Drupal's processing. Drupal stores that information in $node->nid, so you just need a little more PHP to compare the current $node->nid value to the $maxnodeid value you found earlier. In our case, that part looks like this:
<?php if ($node->nid == $maxnodeid) { ?>
All we're doing is saying that if the nid of the current node is the maxnodeid (and that SHOULD be our most recent article), then slap that <div class='toparticle'> tag in front of it. That way, our newest article is in a class by itself that we (i.e. Jeremy) can style differently using CSS. We add another similar snippet later to add the closing tag:
<?php if ($node->nid == $maxnodeid && $page == 0) { ?>
<?php } ?>
As for the different teaser lengths, that's pretty simple. First thing you need to do is go into the Drupal admin pages (Administer-->Content Management-->Post Settings) and adjust the Length of trimmed posts setting to the maximum value you'd like to use for a teaser. For instance, in our case, we want the first post's teaser to be as long as possible (2000 characters).
At this point, all the teasers will be the same length, so we had to make some more adjustments to our node-article.tpl.php file to trim down the other teasers. Again, adding a little PHP to the file makes it simple:
<?php if ($teaser) { if ($node->nid == $maxnodeid && $is_front) { print strip_tags(substr($node->field_article_body[0]['view'], 0, 2000)."...",'<p>,<a>,<pre>,<blockquote>'); } else { print strip_tags(substr($node->field_article_body[0]['view'], 0, 600)."...",'<p>,<blockquote>,<pre>,<a>'); } ?>
All we're doing is saying that, if this particular node's nid is the maxnodeid we found earlier AND if this is being viewed on the front page, then do what's in the first part. If not, do the second.
Those PHP statements are pretty simple. Basically, the PRINT function will do just that: print something. In this case, we're telling it to print out the value of the first XX characters from $node->field_article_body[0]['view'], after stripping out all but the listed HTML tags, and where XX is either 2000 or 600, depending on the conditions we already talked about (is it the newest and is this the front page?).
Keep in mind that you probably won't be using the exact variable "$node->field_article_body". Remember, we used CCK to create a custom content type called "article," and that article type has a text field called article_body we use to hold the main part of our article. Your mileage will vary, and you'll need to adjust that to match your own setup.
If you'd like to see our full node-article.tpl.php file, you can download it at http://www.nerdliness.com/files/node-article.tpl.php.txt
Hope this helps. I know it can be a little confusing, especially since we're using CCK, but if you play around a bit I'm sure you, too, will find new ways to break Drupal. As always, feel free to ask if you have any questions.


