Customizing the display of a node

Customizing the display of a node

Here is a 5-step process to have full control over the output of a node. This method lets you stay at the theme/display level and gives you full control over output without having to implement any hooks in a module. You'll often find that you can get the desired output without resorting to any PHP template or HTML coding using the first two steps.

First: Edit the order of fields

From the content type edit page, click on the Manage fields tab. The URL is admin/content/node-type/[mynodetype]/fields
From here, drag and drop the fields in the order you want them displayed.

Second: Modify node display settings

From the content type edit page, click on the display tab. The URL is admin/content/node-type/[mynode-type]/display

From here, you can choose from a variety of settings including choosing not to display fields, modifying the field label and selecting from a variety of displays that the field's CCK widget supports. You can customize both full node and teaser display.

Display Fields
Imagefield (shown above) supports a wide variety of display options including imagecache presets and lightboxing. No custom themeing required!

Third: Modify CCK field output

You can modify the HTML output for CCK fields by creating a tpl file. (In practice I find this very useful when I have node reference fields that need to be dereferenced but need to show only some fields of the dereferenced node. Step #2 allows for dereferencing and displaying teasers and full nodes.)

To modify CCK field output, copy content-field.tpl.php from sites/all/modules/cck/themes to your theme's base directory. Create and customize content-field-field_myfieldname.tpl.php for each CCK field you want to theme.

In your content-field-field_myfieldname.tpl.php file you have access to:

  1. /**
  2.  * @file content-field.tpl.php
  3.  * Default theme implementation to display the value of a field.
  4.  *
  5.  * Available variables:
  6.  * - $node: The node object.
  7.  * - $field: The field array.
  8.  * - $items: An array of values for each item in the field array.
  9.  * - $teaser: Whether this is displayed as a teaser.
  10.  * - $page: Whether this is displayed as a page.
  11.  * - $field_name: The field name.
  12.  * - $field_type: The field type.
  13.  * - $field_name_css: The css-compatible field name.
  14.  * - $field_type_css: The css-compatible field type.
  15.  * - $label: The item label.
  16.  * - $label_display: Position of label display, inline, above, or hidden.
  17.  * - $field_empty: Whether the field has any valid value.
  18.  *
  19.  * Each $item in $items contains:
  20.  * - 'view' - the themed view for that item
  21.  *
  22.  * @see template_preprocess_field()
  23.  */

Fourth: Modify Node Template

Create a file called node-mynodetype.tpl.php. This file overrides node.tpl.php.
If you use Zen, your node-mynodetype.tpl.php file will have access to the following. If you do not use Zen, you have have access to a subset of the following:

  1. /**
  2.  * @file node.tpl.php
  3.  *
  4.  * Theme implementation to display a node.
  5.  *
  6.  * Available variables:
  7.  * - $title: the (sanitized) title of the node.
  8.  * - $content: Node body or teaser depending on $teaser flag.
  9.  * - $picture: The authors picture of the node output from
  10.  *   theme_user_picture().
  11.  * - $date: Formatted creation date (use $created to reformat with
  12.  *   format_date()).
  13.  * - $links: Themed links like "Read more", "Add new comment", etc. output
  14.  *   from theme_links().
  15.  * - $name: Themed username of node author output from theme_user().
  16.  * - $node_url: Direct url of the current node.
  17.  * - $terms: the themed list of taxonomy term links output from theme_links().
  18.  * - $submitted: themed submission information output from
  19.  *   theme_node_submitted().
  20.  *
  21.  * Other variables:
  22.  * - $node: Full node object. Contains data that may not be safe.
  23.  * - $type: Node type, i.e. story, page, blog, etc.
  24.  * - $comment_count: Number of comments attached to the node.
  25.  * - $uid: User ID of the node author.
  26.  * - $created: Time the node was published formatted in Unix timestamp.
  27.  * - $zebra: Outputs either "even" or "odd". Useful for zebra striping in
  28.  *   teaser listings.
  29.  * - $id: Position of the node. Increments each time it's output.
  30.  *
  31.  * Node status variables:
  32.  * - $teaser: Flag for the teaser state.
  33.  * - $page: Flag for the full page state.
  34.  * - $promote: Flag for front page promotion state.
  35.  * - $sticky: Flags for sticky post setting.
  36.  * - $status: Flag for published status.
  37.  * - $comment: State of comment settings for the node.
  38.  * - $readmore: Flags true if the teaser content of the node cannot hold the
  39.  *   main body content.
  40.  * - $is_front: Flags true when presented in the front page.
  41.  * - $logged_in: Flags true when the current user is a logged-in member.
  42.  * - $is_admin: Flags true when the current user is an administrator.
  43.  *
  44.  * @see template_preprocess()
  45.  * @see template_preprocess_node()
  46.  */

Final: Add CSS and JS as needed

Use drupal_add_css() and drupal_add_js() in node-mynodedtype.tpl.php to insert CSS and JavaScript.