Sometimes we have a path where we don't want any breadcrumb to show up.
So, to hide / suppress / disable the breadcrumb for a specific path / route / page.

(issue text intentionally contains search keywords)

Use cases

This sounds like one specific problem, but actually it depends what exactly we want to achieve:

  • Do we want to just hide the breadcrumb / make it visually disappear, or do we want to prevent breadcrumb and trail generation altogether? E.g. some functionality depends on the crumbs trail being generated, even if the breadcrumb should be invisible in the end.
    • Or maybe we really just want to hide the breadcrumb for human visitors, but machines (Google) still should see it?
  • Are we interested in a solution specific to Crumbs, or should this work for any Drupal site? E.g. if you write a contrib module, you generally cannot assume that the site will have Crumbs installed.
    • Or maybe the breadcrumb was already suppressed, and Crumbs made it show up again? Now we would be talking about some kind of regression that you get by installing Crumbs.
  • Do we want a reusable solution, that will work on any site, or should this be just for this specific site? Is it ok to do this in your custom theme, or do you want it in a module? Or configure it with the UI?

Depending on the first and second question, I already imagine some folks who will just hide the breadcrumb with CSS. Everyone knows someone who would do this. Right?

Possible solutions

Some of them existing, some proposed.
Some proven, some yet to be evaluated.

Hide with CSS

Did I just write that? Seriously?

"Disable breadcrumbs" module?

https://www.drupal.org/project/disable_breadcrumbs
Looks nice. I am currently not sure if this is compatible with Crumbs.
Please evaluate, and share your experience in the comments!

drupal_set_breadcrumb(array())

(currently not supported)

In Drupal core it is possible to call drupal_set_breadcrumb(array()) with an empty array, to disable the breadcrumb.
Typically one would call this from the page callback.

Does Crumbs support this?
There is some stub code in Crumbs that was meant for this purpose, but unfortunately it is currently not active.

In class crumbs_CurrentPageInfo:

  protected function breadcrumbSuppressed() {
    // @todo Make this work!
    return FALSE;
    $existing_breadcrumb = drupal_get_breadcrumb();
    // If the existing breadcrumb is empty, that means a module has
    // intentionally removed it. Honor that, and stop here.
    return empty($existing_breadcrumb);
  }

I checked the history, and found an experimental commit that would change this to:

   protected function breadcrumbSuppressed() {
     // @todo Find the corresponding issue on drupal.org.
     // This has to be drupal_set_breadcrumb(), not drupal_get_breadcrumb(), to
     // avoid menu_get_active_breadcrumb() from being called.
     $existing_breadcrumb = drupal_set_breadcrumb();
     // If the existing breadcrumb is empty, that means a module has
     // intentionally removed it. Honor that, and stop here.
     return isset($existing_breadcrumb) && empty($existing_breadcrumb);
   }

I'd say this deserves a separate issue, where this code change (with some tweaking) can be committed.

hook_menu_breadcrumb_alter()

This hook is officially supported with Crumbs!
A module can implement this hook, set the breadcrumb to an empty array, and it will disappear.

function MYMODULE_menu_breadcrumb_alter(&$active_trail, $item) {
  if ($item['path'] === 'mypath/%') {
    $active_trail = array();
  }
}

hook_preprocess_page()

Yes, this is another hook you can use to suppress the breadcrumb for a specific page.
But this only works if your implementation runs AFTER crumbs_preprocess_page(). You get this for free when implementing in a theme.

// Implement in a module or theme
function MYTHEME_preprocess_page(&$variables) {
  if (...) {
    // I think it would be '', but maybe NULL works as well?
    $vars['breadcrumb'] = '';
  }
}

hook_crumbs_plugins() (don't!)

It is possible to write a Crumbs plugin that almost achieves the same as disabling a breadcrumb. However, this is NOT the reliable and recommended way to do it.

function MYMODULE_crumbs_plugins($api) {
  $api->routeParentPath('mypath/%', 'suppress_mypath', FALSE);
}

The FALSE for the parent path means that the parent is the front page.
This will make the breadcrumb disappear ONLY IF the "shortest visible breadcrumb" display setting is 3 segments.
Otherwise, the breadcrumb would be "Home » My page", so it would NOT achieve the desired outcome.

Comments

donquixote created an issue.

rooby’s picture

Currently I don't believe that https://www.drupal.org/project/disable_breadcrumbs works with this module.

Maybe just integrating with that module would be enough.

donquixote’s picture

@rooby:
This modules uses drupal_set_breadcrumb(array());.
http://cgit.drupalcode.org/disable_breadcrumbs/tree/disable_breadcrumbs....

So it is one of the options I discuss in the issue summary.