Jump to content

Using Drupal's template.php for Overrides

0
  adfm's Photo
Posted May 17 2010 04:05 PM

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.


Overriding a Template Variable

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.

Figure 11.18. Changing the text of the “Submitted by…” line to “Posted on…”

Attached Image

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:

  1. 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 the node.tpl.php file. 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>
    
  2. To change this, we can override it in our template.php file. But Newmarine doesn’t have a template.php file yet. So let’s create one and put it in our newmarine folder, along with our other theme files.

  3. Once we have the template.php file created, we need to add a new preprocess[/inlinecode] function to intercept the variable we wish to change. Add the following code to your template.php file:

    <?php
    
    // $Id:$
    
    
    
    /**
    
     * Implementation of hook_preprocess_node().
    
     */
    
    function newmarine_preprocess_node(&amp;$vars) {
    
     
    
    }
    
  4. 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(&amp;$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>
    
    }
    
  5. Once again, clear the good old cache at Administer→Site configuration→Performance (admin/settings/performance).

  6. When you reload the page, you should see our new format for the submitted date.

Overriding a Theme Function

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.

Figure 11.19. The breadcrumb trail as output by our newmarine_breadcrumb() function

Attached Image

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:

  1. 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.

  1. 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.

Figure 11.20. Theme Developer shows us the function being used for the breadcrumbs

Attached Image

  1. 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.inc and search for theme_breadcrumb[/inlinecode]. Copy the entire theme_breadcrumb[/inlinecode] function. The function basically just takes an array of HTML links and uses PHP’s implode()[/inlinecode] function to concatenate the values using the »[/inlinecode] character, wrapping this in a

    [/inlinecode] with the class of “breadcrumb.”

  2. Open your template.php file, and paste the function in. Rename it to newmarine_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(' &#187; ', $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 the template.php file 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.

  1. 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>
    
     }
    
    }
    
  2. Clear the cache and refresh the screen. Our breadcrumbs now look just the way we want them to.

Cover of Using Drupal
Learn more about this topic from Using Drupal. 

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.

Learn More Read Now on Safari


Tags:
0 Subscribe


1 Reply

0
  nasirg's Photo
Posted Jan 16 2012 03:11 AM

hey I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post. Contact some state or county agencies in your state to get in contact with some facilities that may be places of interest to you thanks.
<a href="http://www.homesandgardenings.com/baby-products.html">new baby products</a>
<a href="http://www.homesandgardenings.com/baby-products.html">buy baby products</a>