CCK Theming in Drupal 4.7

As I go through a test site for One Inch Frame’s update to Drupal 4.7, I’m doing something different with my wiki nodes and web link nodes. Right now, I’m using flexinode to build a custom wikinode type, and am using weblink for the large amount of links that I accumulate in my links directory, the sort of thing that I used to post to del.icio.us. The weblinks module has been discontinued in favor of the links bundle, but that promising project is still not ready, and my needs are such that that project might just be overkill. I’ve still got my eye on it, though.

With the 4.7 version, I’ve decide to take a good hard look at the Content Construction Kit, as it is basically the future of flexinode and Drupal. And it’s also really, really cool.

So I created a simple node type for wikinodes, and a type for web links. One of the things I want to do differently this time around is build in attribution to my web links, so I can give credit to the source where I found the link. So, another CCK content type for web link source is needed, and I can use the built-in ‘nodereference’ field type to link a web link to its source.

And, out of the box, it works great. I can add attribution sources and they show up in a drop-down in the web link content creation form. Super cool. The default look of this field when viewing a web link makes the attribution hotlinked to the attribution’s source node. It boggles my mind to think of some of the ways smart people are going to take advantage of the CCK, and, particularly, its nodereference field type. There’s also a userreference which does the same thing for users, for completeness.

The way CCK-created content looks is a fine default, but, theming them will be necessary in almost every case. Fortunately, the CCK download comes with a theme/ directory with documentation and samples.

So, the first thing I want to do for all my CCKreated (heh, like it? made it up myself) nodes is to remove the label. The CCK allows theming at the field level or up at the node level. To remove the labels globally for all CCKreated content, there’s some code to add to the template.php file, then your PHPTemplate-based theme will call field.tpl.php files to render all CCK fields. Just removing the printing of the label is fine here:

So, my field.tpl.php looks like this:

<div class="field field-type-<?php print strtr($field_type, '_', '-') ?> field-<?php print strtr($field_name, '_', '-') ?>">
<div class="field-items">
  <?php foreach ($items as $item) { ?>
    <div class="field-item"><?php print $item['view'] ?></div>
  <?php } ?>
</div>
</div>

Simple.

While that takes care of theming the individual fields, for my web links I want to theme the whole node. PHPTemplate allows for different themes to be applied to different content types, and CCK extends that to its own types. So, to theme a web link CCKreated node, I write a node-content-web_link.tpl.php. This will override the theming of the individual fields, and give me control over the content as a whole. As CCK exposes its fields via a $field_<fieldname> array, this, too, is pretty simple:

<div class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">
  <?php if ($picture) { print $picture; }?>
  <h2 class="headerstyle"><a href="<?php print $node_url?>"><?php print $title?></a></h2>
  <span class="submitted"><?php print $submitted?></span>
  <span class="content">
    <p class="weblink_description"><?php print $field_description[0][view] ?></p>
    <p class="weblink_url"><?php print $field_url[0][view]?></p>
    <p class="weblink_attribution">via <?php print $field_attribution[0][view]) ?></p>
  </span>
  <span class="taxonomy">Categories: <?php print $terms?></span>
  <?php if ($links) { ?><p class="internallinks">&raquo; <?php print theme('links', $node->links, '&nbsp;&middot;&nbsp;')?></p><?php }; ?>
</div>

That’s nice, and generic. I need a couple more things to make this just what I’m after:

  • The url isn’t hot. It should be linked to itself.
  • The attribution goes to the attribution node, but I’d much rather it go directly to the attribution’s site, which is contained in a URL field as part of the attribution node.

The first one is very simple, just wrap the url in an href of itself:

<p class="weblink_url"><a href="<?php print $field_url[0][view]?>"><?php print $field_url[0][view]?></a></p>

Getting to the last part will require a tiny bit of programming. What we want, instead of a link to the attribution node (which is a cck-derived content type of content-web_link_source) we want the link to go directly to the url within that node. To start, we’ll need to call a custom theme function instead of printing the $field_attribution stuff:

<p class="weblink_attribution">via <?php print theme('oif_attribution', $field_attribution[0][nid]) ?></p>

Now, we write that theme function. It goes in template.php, and, for completeness’ sake, we call it andreas03_oif_attribution(), since andreas03 is the name of the theme. It gets the nid of the attribution node passed to it::

<?php
function andreas03_oif_attribution($nid) {
  $attribution = node_load($nid);
  $output = '<a href="' . $attribution->field_url[0][value] . '">' .  $attribution->title . '</a>';
  return $output;
}
?>

And, there it is. A fully themed CCK node. Drupal is so freakin’ cool. You’ll see these new nodes when I roll out the new version of One Inch Frame, based on Drupal 4.7, which is still several weeks away. There are just so many cool things that I want to do, that are now possible.

Tagged as:

Comments

Relationship module

You might want to check out the still-evolving Relationship module — lots of RDF reference meta-data goodness for relating nodes to other content.

Post new comment

The content of this field is kept private and will not be shown publicly.