1

I have 2 functions in template.php: The first attaches current page title to breadcrumbs; the second sets the title to 'Completion' when the user opens /scores/completion.

function lwt_breadcrumb($variables) {
    $breadcrumb = $variables['breadcrumb'];
    if (!empty($breadcrumb)) {
        // Adding the title of the current page to the breadcrumb.
        $breadcrumb[] = drupal_get_title();

        // Provide a navigational heading to give context for breadcrumb links to
        // screen-reader users. Make the heading invisible with .element-invisible.
        $output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';

        $output .= '<div class="breadcrumb">' . implode(' » ', $breadcrumb) . '</div>';
        return $output;
    }
}

function lwt_preprocess_page(&$vars) {
      $path = drupal_get_path_alias();
      if ($path == 'scores/completion') {
            $vars['title'] = 'Completion';
      }
}

The breadcrumb is still scores. How do I fix it?

screenshot

2
  • Is the page title changing? I.e. is it JUST the breadcrumb that's broken, or is your preprocess not firing at all?
    – Chapabu
    Nov 13, 2012 at 12:18
  • The page title is changing. Preprocess is firing.
    – user4035
    Nov 13, 2012 at 13:42

1 Answer 1

3

drupal_get_title() gets the title from a PHP static variable, which is different from the variable $variables['title'] passed to the template files.

Your template hook would work if it uses the following code.

function lwt_preprocess_page(&$vars) {
  $path = drupal_get_path_alias();
  if ($path == 'scores/completion') {
    // -1-
    $vars['title'] = t('Completion');
    drupal_set_title($vars['title']);
    // -1-
  }
}

What happens is the following:

  • lwt_preprocess_page() is invoked, and (through drupal_set_title()) sets the static variable containing the value returned from drupal_get_title().
  • template_process_page() is invoked, and it calls the theme function for the breadcrumbs; in your case that theme function is lwt_breadcrumb(), which calls drupal_get_title(), obtaining the same value you set in lwt_preprocess_page().

In lwt_preprocess_page() you could also replace the code between // -1- with the following one.

    unset($vars['title']);
    drupal_set_title(t('Completion'));

The reason also this code works is that template_process_page() first checks if $variables['title'] is set; if it is not set, it calls drupal_get_title().

  if (!isset($variables['title'])) {
    $variables['title'] = drupal_get_title();
  }

References

  • theme(), for the order in which theme template hooks are executed
  • drupal_static(), for understanding how the page title is stored in a static variable accessible to any function
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.