Archive for the 'Drupal' Category

Drupal Templates By Node ID

By default, Drupal will let you define a template called node-bork.tpl.php, which is a template for all nodes of type “bork.” That’s pretty handy, but what if you want a template for a specific node id? What I want to be able to do is define a template called node-4.tpl.php, which would be the template for the node with node id 4. Here’s how.

In your template.php file, add this:

function phptemplate_preprocess_node(&$vars) {
  $vars['template_files'][] = 'node-'. $vars['nid'];
}

Content Management Strategies for Drupal

Rather than copy/paste code, I’ll share some theories.

When I started working with Drupal, the way you created/edited content was awful. I could not understand how “users” were supposed to understand how to do it. It was so confusing. Eventually I figured out a better way to do it.

Out of the box, users are given a menu that says “Create Content.” From here, they can create content items. That’s cool. If you want to edit a page, you navigate to that page and click the “edit” tab. This works for very simple sites, but nothing is ever really that simple. What we were doing was giving users administrative privileges. (Bear with me.) Then we told them, to edit a page go to Administer, Content Management, Content. On that screen, click one of the many drop downs and pick the type of content you want to edit. Then wade through the filtered list to find the “page” you want to edit. At the time it was the only way we knew to edit content.

This was terribly confusing. Even for me as a developer it seemed like a hassle. Users could not understand it, could not follow a list of instructions like that when what they wanted to do was really pretty simple. And then I started getting questions from our users who were poking around in the administrative menu and finding things they should not be finding.

So what I started doing was setting up content management views, and a menu to keep them all organized. I set up a view for the “story” content type. I label it “View/Edit News Items” and put it in the “Manage Content” menu. The view has things like title, maybe a teaser or author, updated date, a view link and an edit link. Under the link for the view in the menu, I add a “Create News Item” link that allows them to create a story from right there. I’ll repeat this group for whatever content types I need to. Some websites have a “location” content type, so I’ll build a view for that, and give them a create link. Some websites have blog entries, job opportunities, quotes. You get the idea.

This makes more sense to users. When they log in they see, clearly, how to get to the content that they want to edit. It’s organized. It’s quick. You don’t even really have to “train” people who to edit content. It’s right there.

And to give you another example… Our website has a document listing all of our clients. How does anybody edit it? Well, if you didn’t know that the list of clients was stored in a document, you wouldn’t even know what to do. But if you did, then you’d have to have administrative privileges, and you’d go to Administer, Content Management, Content, switch the content type to “Client Listing,” and edit the resulting document. With the new system, you log in, you don’t need admin privileges, you see a link in the “Manage Content” menu that clearly says “View/Edit Client Listing.” You click that and edit the document. You’re done. You’re happy.

image_import_zip For Drupal 6

I’m working on a project where we have a ton of images that we’ll need to import into image galleries in Drupal 6. There are a bunch of nice modules to do this, but they expect the end user to have the correct JRE installed, or fail to function with .htaccess authentication. We needed something simpler.

There’s a module called “image_import_zip” which allows you to upload a zip file full of images without using FTP. It extracts them, and then you can use “image_import” to turn them all into image nodes. Which sounds nice, except that “image_import_zip” is only for Drupal 5, and the people responsible tell you to go use something Flashy for Drupal 6.

So I went ahead and hacked the module to work with Drupal 6. Enjoy.

Drupal Get Image Path

I’m still learning how Drupal (6) puts things together. We have a situation where we have a page that has an image attached to it. When we view the page, we need the full path to the image that’s attached. You would think it would be pretty simple. You would expect that somewhere in the $node object is a url for the image that is attached. But that’s just not the case.

Drupal calls the page a node. It also calls the image a node. You have a table that says “this node is attached to that node” by ids. Specificially, image_attach.nid is your page, and image_attach.iid is the node id of the image. Then you have an image table, which lists the images by node id, with a size, and a file id. (Confused yet?) We use image_attach.iid and the size we’re looking for to get image.fid. And then there’s one last table, which is files, which lists by id, and has the actual filepath. We can use image.fid to get the files.filepath we want.

So I wrote a function to do all of that nonsense for me. You pass it the node id of the page which has the image attached to it, and it returns the path/to/the/attached/image/thank/you/very/much.jpg.

function lookupImagePath($nid, $thumbnail = false) {
 
	if ($thumbnail) {
		$size = 'thumbnail';
	} else {
		$size = '_original';
	}
 
	$sql  = 'SELECT f.filepath ';
	$sql .= 'FROM image_attach a, image i, files f ';
	$sql .= 'WHERE a.iid = i.nid ';
	$sql .= 'AND i.fid = f.fid ';
	$sql .= 'AND a.nid = ' . $nid . ' ';
	$sql .= 'AND i.image_size = "' . $size . '" ';
 
	$res = db_query($sql);
	$row = db_fetch_array($res);
	$filepath = $row['filepath'];
 
	return $filepath;
 
} // end lookupImagePath

Template for Node By Node ID

I’m working in Drupal 6 and we have a page template page.tpl.php, which handles most everything. It has a bunch of stuff for the header, and it handle the sidebar on the left, and the content in the middle of the page. The content in the middle of the page comes from whatever node you’re viewing, and it gets run through node.tpl.php first. This formats the node for output. Thing is, I have a particular node (with node ID 77) that I want to be formatted differently.

Drupal says you can make a new template called page-node-77.tpl.php and format the node content accordingly. But I don’t want to have two templates that display the same stuff (like the header and the sidebar on the left). All I want to do is change the node template. What I want to be able to do is make a new file called node-77.tpl.php and have that be used instead of node.tpl.php.

After much research, I find that includes/theme.inc has the function template_preprocess_node, which is where I want to put my code. At the end of this function is

$variables['template_files'][] = 'node-'. $node->type;

This allows us to define template files for nodes by type, but not by ID. So add

$variables['template_files'][] = 'node-'. $node->nid;

and you’re all set.