The Definitive Guide to Drupal 7

How Theme Functions Are Created

Drupal core and modules usually define theme functions, but they can be defined by themes as well. hook_theme() implementations are where all the juicy information about most generic theme functions resides, including what parameters these functions accept. Theme hooks are covered in detail in the “Theme Hook Suggestions” section later in this chapter, but Listing 15–11 shows what a simple hook_theme() implementation looks like.

Listing 15–11. Example hook_theme() implementation.
  1. <?php
  2. /**
  3.  * Implements hook_theme().
  4.  */
  5. function mymodule_theme() {
  6. return array(
  7. 'my_theme_hook' => array(
  8. 'variables' => array('parameter' => NULL),
  9. ),
  10. );
  11. }

Implementations of hook_theme() let Drupal know about theme hooks. Once Drupal is aware, it will search for a theme function called theme_my_theme_hook() in this case, which might look like the code in Listing 15–12.

Listing 15–12. Example Theme Function Implementation
  1. <?php
  2. function theme_my_theme_hook($variables) {
  3. $parameter = $variables['parameter'];
  4. if (!empty($parameter)) {
  5. return '<div class="my-theme-hook">' . $parameter . '</div>';
  6. }
  7. }

You are reading content from two chapters on Theme Development from The Definitive Guide to Drupal 7, written by Jacine Luisi and published by Apress on July 19, 2011. All rights reserved.