Reformat the Node Meta Line in Drupal

One of the problems with writing a Drupal theme from scratch is the hugely cryptic output that the system generates and renders. Too much information, crammed into too little space. Some themes available for download do not address this as well.

For example, the post meta information is outputted as “Submitted by username on Sat, 09/12/2009 – 19:12”. Although the statement is rich in information, it is not visually appealing or effective and looks cryptic. The line would look much better if it were reformatted to “By username - April 15, 2009”.

Although it makes minimal difference to the general layout, these small details eventually make a world of difference to the final feel of the theme. We can override parts of Drupal's themable output to achieve this. Specifically, the node meta or submitted information. Here is a quick how-to on changing the The "Submitted by X on Y" line, or the node meta in Drupal.

The node meta, is generated by the function theme_node_submitted() and is available and the $submitted variable. We can override this by placing a node-submitted.tpl.php file.

Navigate to your theme's directory and create a file called node-submitted.tpl.php. This file now works as an override for the theme_node_submitted() function. (Edit it if it already exists.)
Paste the following code into the file:

<?php
$op = "By ";
$op .= theme(username,$node);
$op .=" on the ";
$op .= format_date($node->created, $type = 'custom', $format = 'jS of F Y', $timezone = NULL, $langcode = NULL);
echo $op; ?>

This code declares a variable $op, which will be used to build up the output line. We initialize the variable with the string “By ”. The “.=” operator is used to append to the end of the string. The function theme(username, $node) is a part of the Drupal API. We use it here to extract the username of the author of the node from the $node variable.

The format_date() function is also supplied via the API. It is used to format a UNIX timestamp into a more human readable pattern. $node->created supplies the UNIX timestamp of the creation date of the post. The parameter $type can be set to “small”, “medium”, “large” or “custom”. When set to any of the first three, Drupal will format the time the chosen inbuilt format and the parameter $format can be left blank. Choosing custom will require us to supply a proper PHP date format via the $format variable. In the example above we have set a “1st January 2009” format. The remaining variables can be set to null for most proposes.

Play around with the $op variable and format_date function to get the output you desire.

We went from here:
Cryptic Node Meta screen-shot Drupal

To here:
Refined Node meta screenshot Drupal

Links:

Note: Another method would be creating a phptemplate_node_submitted() function in our template.php

Subject: Thanks for the information.

Thanks for the information. This will help new drupal users.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options