The Definitive Guide to Drupal 7

Exercise

Let's Override a Theme Function

Here is a theme function called theme_more_link(). It is used to print a link to additional content in blocks. To find the code for the theme function, take a look at http://api.drupal.org/api/function/theme_more_link/7.

  1. Copy and paste the original theme function code into template.php.
    1. <?php
    2. /**
    3.  * Returns HTML for a "more" link, like those used in blocks.
    4.  *
    5.  * @param $variables
    6.  * An associative array containing:
    7.  * - url: The url of the main page.
    8.  * - title: A descriptive verb for the link, like 'Read more'.
    9.  */
    10. function theme_more_link($variables) {
    11. return '<div class="more-link">' . l(t('More'), $variables['url'], array('attributes' => array('title' => $variables['title']))) . '</div>';
    12. }
  2. Change the beginning of the function name to your theme’s name, save it, and clear the site cache.
    1. <?php
    2. /**
    3.  * Returns HTML for a "more" link, like those used in blocks.
    4.  *
    5.  * @param $variables
    6.  * An associative array containing:
    7.  * - url: The url of the main page.
    8.  * - title: A descriptive verb for the link, like 'Read more'.
    9.  */
    10. function dgd7_more_link($variables) {
    11. return '<div class="more-link">' . l(t('More'), $variables['url'], array('attributes' => array('title' => $variables['title']))) . '</div>';
    12. }
  3. Drupal will now use your version of the theme function, so make changes!
    1. <?php
    2. /**
    3.  * Overrides theme_more_link().
    4.  * - Changed the text from "More" to "Show me More"
    5.  * - Changed the class from "more-link" to "more"
    6.  */
    7. function dgd7_more_link($variables) {
    8. return '<div class="more">' . l(t('Show me MORE!'), $variables['url'], array('attributes' => array('title' => $variables['title']))) . '</div>';
    9. }

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.