Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
63
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

CHAPTER 9 THE THEME SYSTEM

Note Any parts of your modules that output HTML or XML should be done only within theme functions so they become accessible for themers to override.

Overriding with Template Files

If you’re working with a designer, telling him or her to “just go in the code and find the themable functions to override” is out of the question. Fortunately, there’s another way to make this more accessible to designer types. You can instead map themable items to their own template files. I’ll demonstrate with our handy breadcrumb example.

Before we begin, make sure that no theme function is overriding theme_breadcrumb(). So if you created a grayscale_breadcrumb() function in your theme’s template.php file in the preceding section, comment it out. Then, create a file at sites/all/themes/grayscale/breadcrumb.tpl.php. This is the new template file for breadcrumbs. Because we wanted to change the <div> tag to a <span> tag, go ahead and populate the file with the following:

<?php if (!empty($breadcrumb)): ?>

<span class="breadcrumb"><?php print implode(' ! ', $breadcrumb) ?></span> <?php endif; ?>

That’s easy enough for a designer to edit. Now you need to let Drupal know to call this template file when looking to render its breadcrumbs. To do that, rebuild the theme registry by clearing the site’s cache files. To clear the cache, visit admin/config/development/performance and click the “Clear all caches” button. While rebuilding the theme registry, Drupal will discover your breadcrumb.tpl.php file and map the breadcrumb themable item to that template file.

Now you know how to override any themable item in Drupal in a way that will make your designers happy.

Adding and Manipulating Template Variables

In this example, we’ll look at manipulating or adding variables that are being passed into page and node templates. Let’s continue with our example of using the breadcrumb trail. First, let’s modify sites/all/themes/grayscale/breadcrumb.tpl.php to use a variable called $breadcrumb_delimiter for the breadcrumb delimiter:

<?php if (!empty($breadcrumb)): ?> <span class="breadcrumb">

<?php print implode(' '. $breadcrumb_delimiter .' ', $breadcrumb) ?> </span>

<?php endif; ?>

To set the value of $breadcrumb_delimiter, one option would be in a module. We could create sites/all/modules/crumbpicker.info:

219

Download from Wow! eBook <www.wowebook.com>

CHAPTER 9 THE THEME SYSTEM

name = Breadcrumb Picker

description = Provide a character for the breadcrumb trail delimiter. package = Pro Drupal Development

core = 7.x

The module at sites/all/modules/crumbpicker.module would be tiny:

<?php

/**

*@file

*Provide a character for the breadcrumb trail delimiter.

*/

/**

* Implements $modulename_preprocess_$hook(). */

function crumbpicker_preprocess_breadcrumb(&$variables) { $variables['breadcrumb_delimiter'] = '/';

}

After enabling the module, your breadcrumb trail should look like Home / Administer / Site building. The preceding example illustrates a module setting a variable for a template file to use. But there

must be an easier way than creating a module every time a variable needs to be set. Sure enough, it’s template.php to the rescue. Let’s write a function to set the breadcrumb delimiter. Add the following to your theme’s template.php file:

/**

*Implements $themeenginename_preprocess_$hook().

*Variables we set here will be available to the breadcrumb template file.

*/

function grayscale_preprocess_breadcrumb(&$variables) { $variables['breadcrumb_delimiter'] = '#';

}

That’s easier than creating a module, and frankly, the module approach is usually best for existing modules to provide variables to templates; modules are not generally written solely for this purpose. Now, we have a module providing a variable and a function in template.php providing a variable. Which one will actually be used?

Actually, a whole hierarchy of preprocess functions run in a certain order, each one with the potential to overwrite variables that have been defined by previous preprocess functions. In the preceding example, the breadcrumb delimiter will be # because phptemplate_preprocess_breadcrumb() will be executed after crumbpicker_preprocess_breadcrumb(), and thus its variable assignment will override any previous variable assignment for $breadcrumb_delimiter.

For the theming of a breadcrumb trail using the Grayscale theme, the actual order of precedence (from first called to last called) would be:

220

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]