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.

Superfan ta stic

Home Administer

admin o My account » Create content T Administer ► Content management ' Site building o Blocks

Blocks

Configure wf Menus

Control your

Figure 11-19. The breadcrumb trail as output by our newmarine_breadcrumb() function

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

2. This time, instead of looking at the "Parents info," we want to look at the "Function called:" section. It should say theme_breadcrumb() in big letters, as shown in Figure 11-20.

Figure 11-20. Theme Developer shows us the function being used for the breadcrumbs

3. 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_bread crumb. Copy the entire theme_breadcrumb function. The function basically just takes an array of HTML links and uses PHP's implode() function to concatenate the values using the » character, wrapping this in a <div> with the class of "breadcrumb."

4. Open your template.php file, and paste the function in. Rename it to newmarine_breadcrumb, as shown: /**

* Return a themed breadcrumb trail.

* @param $breadcrumb

* An array containing the breadcrumb links.

* @return a string containing the breadcrumb output. */

function newmarine_breadcrumb($breadcrumb) {

if (!empty($breadcrumb)) { return '<div class="breadcrumb">'. implode(' » ', $breadcrumb) .'</div>';

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

5. Now we just need to change the » to a double colon (::), as shown here:

function newmarine_breadcrumb($breadcrumb) { if (!empty($breadcrumb)) { return '<div class="breadcrumb">'. implode(' :: ', $breadcrumb) .'</div>';

6. Clear the cache and refresh the screen. Our breadcrumbs now look just the way we want them to.

Was this article helpful?

0 0

Post a comment