Sunday, April 30, 2017 06:54 am EDT

Changing the output of the $submitted variable using Drupal

Bri's picture

As you might already know, you can pick which content types contain the $submitted by information that Drupal includes. If you are not already aware, you can go to admin/build/themes/settings/global and choose which content types you would like to have that information. As you add new content types, revisit this page to alter the settings.

Perhaps you would like the message to be a little bit different, or maybe even have the date display differently. If that's the case, you can add a function to your template.php page inside your theme, and make these changes. You start by creating the function phptemplate_node_submitted($node) in your template.php file like so:

function phptemplate_node_submitted($node){

}

If you wanted to create a blog where you only were concerned with the date, and you wanted to exclude the "submitted by username" section, you could write something such as the following:

function phptemplate_node_submitted($node){
    return date('F j, Y', $node->created);
}

This example uses the PHP function date() to format the $node->created variable such that it will print like "January 1, 2009." The date function takes 2 parameters. The first is a string used for the formatting of the date, and the second is the timestamp. You can see a whole chart of formatting options in the documentation for the date function at php.net.

You can also choose to display the last date/time that the page was updated instead if you prefer. This might come in handy for pages that are updated frequently. Add the following code to your template.php file:

function phptemplate_node_submitted($node) {
  $time_unit = 86400; // number of seconds in 1 day => 24 hours * 60 minutes * 60 seconds
  $threshold = 1;

  if ($node->changed && (round(($node->changed - $node->created) / $time_unit) > $threshold)){ // difference between created and changed times > than threshold
    return t('Last updated on @changed. Originally submitted by !username on @created.', array(
      '@changed' => format_date($node->changed, 'medium'),
      '!username' => theme('username', $node),
      '@created' => format_date($node->created, 'small'),
    ));
  }
  else{
    return t('Submitted by !username on @datetime',
      array(
        '!username' => theme('username', $node),
        '@datetime' => format_date($node->created),
      ));
  }
}

If you want to change the output on a per content type basis, and your theme settings don't support this, you can always make changes to individual template files. For example, if you wanted to edit the Blog content type only, you could create the template node-blog.tpl.php and make a change such as the following:

<?php if ($submitted) : ?>
       
<?php endif; ?>

Comments

Anonymous's picture

Thanks!

This solved a problem had for months!

bdawg8569's picture

Thanks

This was just the information i was looking for.