Customization of “Submitted by” Node Field

When a user posts a new node content, by default there’s data below the title that provides info on the user who posted the content and the date on which it was posted. For example:

Submitted by Admin on Sat, 11/09/2013 – 21:05

Personally it took me a while to understand how this field could be customized for example to “Posted by Admin on 24 Mar 2014”. The customization involves:

  • template.php
  • a pre-processor function (which is placed in the template.php)
  • PHP date() function – it would be great if you had deeper understanding of this function. Just head over to PHP.NET for more info on this function.

In this case, I used Bartik as my template; the default template for any Drupal 7 installation. The pre-processor function used is the theme pre-processor for the node i.e.

function bartik_preprocess_node(&$variables) {

}

All pre-processor functions are housed either in the template.php file or in a module. Our customization process is quite small so there’s no need of creating a module (why create many files when we can write a few lines of code to accomplish our goal).

The following figure displays the default submitted info.

submitted_info_before

Customization of “Submitted by” was once accomplished through theme_node_submitted in Drupal 6 but in Drupal 7 you use the above pre-processor for the node. Our complete pre-preprocessor function will appear as follows:

[php]
function bartik_preprocess_node(&$vars) {
if (variable_get(‘node_submitted_’ . $vars[‘node’]->type, TRUE)) {
$date = format_date($vars[‘node’]->created, ‘custom’,’dS l, F Y’);
$vars[‘submitted’] = t(‘Posted on !datetime’, array(‘!datetime’ => $date));
}
}
[/php]

$date variable stores the date format function i.e. format_date() which has three parameters:

  • The first parameter, $vars[‘node’]->created is a UNIX time stamp for the when the node was created. This needs to be formated
  • The second parameter is the format to use, in this case, custom. The format can otherwise be short, medium or long
  • The third parameter is the PHP date format derived from date() function. This parameter is optional but comes in use  only when the second parameter i.e. format is custom

!datetime is a placeholder for the array value of $date while the array member variable $vars[‘submitted’] stores the submitted info and is called by one of the .tpl.php template files as print $submitted e.g. node.tpl.php. For example:

[php]
<?php if ($display_submitted): ?>
<span class="submitted">
<?php print $user_picture; ?>
<?php print $submitted; ?>
</span>
<?php endif; ?>
[/php]

Once the changes have been saved, the new submitted info should display as follows:

submitted_info_after

And that’s it. The power of Drupal’s flexibility.

References:

  1. https://www.drupal.org/node/1075692
  2. https://api.drupal.org/api/drupal/includes%21common.inc/function/format_date/7
  3. http://www.php.net//manual/en/function.date.php
  4. http://stackoverflow.com/questions/10592047/drupal-7-change-submitted-by-format
  5. http://www.seascapewebdesign.com/blog/how-customize-submitted-text-drupal-7
Please follow and like us:
0

Leave a Reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

June 19, 2014

Posted In: Information Technology

Tags: , ,

Leave a Comment