Just looking for information here, because this looks super-weird at first glance, and there is no explanation in the function's header comment.

custom_breadcrumbs.module implements hook_preprocess(), which means that code is run for every single template-based theme call.

Is this really necessary?

Comments

MGN’s picture

Its used to (optionally) set the home breadcrumb on every page. This seems to be the best approach suggested thus far to provide this feature.

jweowu’s picture

Why is it not restricted to the 'page' template?

I can understand using hook_preprocess() as a method of getting in ahead of template_preprocess_page() (another way is to use hook_preprocess_page() and hook_theme_registry_alter() to reorder the preprocess functions), but I'm a bit confused as to why this code is run for every template called?

It would be great if there was a comment added to the code to indicate the problem that this is a solution to?

MGN’s picture

Version: 6.x-2.0-beta3 » 6.x-2.x-dev
Category: support » task

In custom_breadcrumbs.module, I think it could be restricted to the page template, since it is doing the same thing on every page.

In custom_breadcrumbsapi, it is not restricted to the page template so you can define custom breadcrumbs based on the theme template called on a particular page. For example, you can use this module to customize the breadcrumb trail on the forum topic list page, or any other page that defines a template. Even if the module sets its own breadcrumb on these pages, you can customize it using this feature.

I agree it should be better documented - in all the custom breadcrumbs modules. I'll look into changing the base custom breadcrumbs module to only fire on the page template. As long as we can set the HOME breadcrumb on every page in the site, I think this should be sufficient.

Thanks for the suggestion.

MGN’s picture

Status: Active » Postponed (maintainer needs more info)

@jweowu, I tried your suggestion to implement hook_preprocess_page() instead of hook_preprocess(), but it doesn't fire on many pages - anything under admin/*, (i.e. administration forms), for example. But I have found that I can use hook_preprocess, with an if clause only executing code if ($hook == 'page') ... . I am not sure why this works on every page, but hook_preprocess_page doesn't. Any ideas? Other suggestions for tightening up this code?

Thanks

EDIT: The following seems to work:

/**
 * Implements hook_preprocess().
 */
function custom_breadcrumbs_paths_preprocess(&$variables, $hook) {
  static $tried = FALSE;
  if ($hook == 'page' && !$tried) {
    // Only respond to the first call for this hook.
    $tried = TRUE;
    if (!custom_breadcrumbs_exclude_path()) {
      $objs = (isset($variables) && is_array($variables)) ? $variables : array();
      // Check for a match to cover module callback pages.
      if (_custom_breadcrumbs_paths_set_breadcrumb($objs)) {
        $variables['breadcrumb'] = theme('breadcrumb', drupal_get_breadcrumb());
      }
    }
  }
}
jweowu’s picture

Looking at _theme_process_registry(), it looks to me like it should have picked your hook_preprocess_page(), provided you remembered to clear the theme registry before testing it?

MGN’s picture

Status: Postponed (maintainer needs more info) » Active

Thanks. I thought I was clearing the cache, but apparently not. I just tried again and now hook_preprocess_page gets called on every page, as expected. I'll commit the changes to 6.x-2.x-dev soon. Thanks again for helping with this.

MGN’s picture

Status: Active » Fixed

Committed to 6.x-2.x-dev.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.