Do you want to change the "Submitted by..." line on your Drupal site? This excerpt from Byron, Berry, et al.'s Using Drupal will show you how by overriding Drupal's template.php.
Drupal gives us lots of nice variables to use, but they don’t always look the way we prefer. A common item that many people wish they could change is the “Submitted by...” line, which prints out the author name and date that a node was created. You can turn this display off and on in the theme configuration screen at Administer→Site building→Themes (admin/build/themes), under the Configure tab (admin/build/themes/settings), but you can’t change what information is actually printed out when the display is on. We’re going to change this line to instead print out “Posted on” followed by the date, as shown in Figure 11.18.
One important piece that we need in order to modify this is a
template.php file. As our
Newmarine theme doesn’t have a template.php file in it yet, we’ll create
that as well to accomplish our task:
-
If we go to a node that has submitted information showing and do the same Drupal Themer Information trick that we did back when we moved the
$breadcrumb[/inlinecode] variable, we can find out what the parent template for that “Submitted by…” line is. You will see that its parent is thenode.tpl.phpfile. If you open up that file we can see that the$submitted[/inlinecode] variable is being printed there to produce that output as highlighted below:<?php if ($page == 0) { ?><h2 class="title"><a href= "<?php print $node_url?>"><?php print $title?></a></h2><?php }; ?> <div class="content"><?php print $content?></div> <span class="strong"><strong><span class="submitted"><?php print $submitted?></span></strong></span> <div class="taxonomy"><?php print $terms?></div> To change this, we can override it in our
template.phpfile. But Newmarine doesn’t have atemplate.phpfile yet. So let’s create one and put it in ournewmarinefolder, along with our other theme files.Once we have the
template.phpfile created, we need to add a newpreprocess[/inlinecode] function to intercept the variable we wish to change. Add the following code to yourtemplate.phpfile:<?php // $Id:$ /** * Implementation of hook_preprocess_node(). */ function newmarine_preprocess_node(&$vars) { }Now we need to actually override the variable, by adding it inside the function and then putting our own value into it:
<?php // $Id:$ /** * Implementation of hook_preprocess_node(). */ function newmarine_preprocess_node(&$vars) { <span class="strong"><strong> // Change the submitted value to output like "Posted on June 12, 2008".</strong></span> <span class="emphasis"><em> $vars['submitted'] = t('Posted on ') .</em></span> <span class="emphasis"><em> format_date($vars['node']->created, 'custom', 'F j, Y');</em></span> }Once again, clear the good old cache at Administer→Site configuration→Performance (admin/settings/performance).
When you reload the page, you should see our new format for the submitted date.
Overriding a theme function is basically as simple as
copying and pasting the appropriate theme function into your theme’s
template.php file. The difficult
part is usually finding the theme function. We
are going to change the site’s breadcrumb so that it doesn’t print out
the breadcrumbs separated by » but instead by :: (double colon), to
look like those in Figure 11.19.
We’ll use our nifty Devel Themer Information tool to help us again, so go to a page with a breadcrumb trail showing. Something like Administer→Site building (admin/build) should work fine. Follow these steps:
Turn on the Themer info by checking the box. Now you can hover over the breadcrumb and then click to display the info in the Drupal Themer Information box.
-
This time, instead of looking at the “Parents info,” we want to look at the “Function called:” section. It should say
theme_breadcrumb()[/inlinecode] in big letters, as shown in Figure 11.20.
Now we know which function is creating the breadcrumbs, so we can go grab a copy of it to work with. Open up
includes/theme.incand search fortheme_breadcrumb[/inlinecode]. Copy the entiretheme_breadcrumb[/inlinecode] function. The function basically just takes an array of HTML links and uses PHP’simplode()[/inlinecode] function to concatenate the values using the»[/inlinecode] character, wrapping this in a[/inlinecode] with the class of “breadcrumb.”Open your
template.phpfile, and paste the function in. Rename it tonewmarine_breadcrumb[/inlinecode], as shown:/** * Return a themed breadcrumb trail. * * @param $breadcrumb * An array containing the breadcrumb links. * @return a string containing the breadcrumb output. */ <span class="strong"><strong>function newmarine_breadcrumb($breadcrumb) {</strong></span> if (!empty($breadcrumb)) { return '<div class="breadcrumb">'. implode(' » ', $breadcrumb) .'</div>'; } }Warning
If you forget to rename the function, when you try to use it on the site you will get a PHP error like this: “Fatal error: Cannot redeclare theme_breadcrumb().” Don’t panic! Drupal core is already using the
theme_breadcrumb[/inlinecode] function name, so you can’t use it as well. Just go back to thetemplate.phpfile and make sure to change the function name to use your theme’s name at the beginning. Reload the screen and your site will return.Now we just need to change the » to a double colon (::), as shown here:
function newmarine_breadcrumb($breadcrumb) { if (!empty($breadcrumb)) { <span class="strong"><strong>return '<div class="breadcrumb">'. implode(' :: ', $breadcrumb) .'</div>';</strong></span> } }Clear the cache and refresh the screen. Our breadcrumbs now look just the way we want them to.
With the recipes in this book, you can take full advantage of the vast collection of community-contributed modules that make the Drupal web framework useful and unique. You'll get the information you need about how to combine modules in interesting ways (with a minimum of code-wrangling) to develop a variety of community-driven websites -- including a wiki, publishing workflow site, photo gallery, product review site, online store, user group site, and more.




Help











