How do I remove HOME from breadcrumb

11,086

Solution 1

Override the breadcrumb in your theme's template.php file:

/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return a string containing the breadcrumb output.
 */
function phptemplate_breadcrumb($breadcrumb) {
  if (!empty($breadcrumb)) {
    array_shift($breadcrumb); // Removes the Home item
    return '<div class="breadcrumb">'. implode(' › ', $breadcrumb) .'</div>';
  }
}

Solution 2

Here's a Drupal 7 version:

/**
 * Get rid of Home in breadcrumb trail.
 */
function <themename>_breadcrumb($variables) {
  $breadcrumb = $variables['breadcrumb'];

  if (!empty($breadcrumb)) {
    // 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>';

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

Solution 3

Hide 'Home' Breadcrumb for Drupal 8

(Replace yourtheme with your theme's actual machine name...)


Option 1: fast and simple theme preprocess

Add the following to your theme's yourtheme.theme file:

/**
 * Prepares variables for `breadcrumb.html.twig`.
 */
function yourtheme_preprocess_breadcrumb(&$variables){

  // Remove 'Home' from breadcrumb trail.
  if (count($variables['breadcrumb'])) {
    array_shift($variables['breadcrumb']);
  }
}

Or Option 2: a checkbox option in theme settings

Screenshot demonstrating what the theme settings option looks like in the Drupal Admin Appearance UI

Add the following to your theme's theme-settings.php:

<?php

/**
 * Implements hook_form_system_theme_settings_alter().
 */
function yourtheme_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {

  $form['theme_settings']['hide_home_breadcrumb'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Hide <em>Home</em> in breadcrumb trail'),
    '#default_value' => theme_get_setting('hide_home_breadcrumb', 'yourtheme'),
  );

}

Add the following to your theme's yourtheme.theme file:

/**
 * Prepares variables for `breadcrumb.html.twig`.
 */
function yourtheme_preprocess_breadcrumb(&$variables){

  // Remove 'Home' from breadcrumb trail.
  if (!empty(theme_get_setting('hide_home_breadcrumb', 'yourtheme')) && count($variables['breadcrumb'])) {
    array_shift($variables['breadcrumb']);
  }
}

If you want the Home button to be disabled by default when the theme is installed, add the following to your theme's yourtheme.settings.yml:

# Hide 'Home' in breadcrumb trail by default.
hide_home_breadcrumb: 1

If you're working with an existing site, and are using Drupal Configuration Syncing with Drupal 8, you should also add/modify the yourtheme.settings.yml file in the sync directory, and run drush cim sync.


Or Option 3: a more nuanced theme setting

In some cases, a site design might call for hiding the Home link if it is the only item in the breadcrumb trail, and when there are other items in the breadcrumb trail to leave Home in the breadcrumb trail.

Screenshot demonstrating a more nuanced theme setting with 3 radio button options to select home breadcrumb visibility options

Here's how to implement the radio buttons depicted above:

Add the following to your theme's theme-settings.php:

<?php

/**
 * Implements hook_form_system_theme_settings_alter().
 */
function yourtheme_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {

  $form['breadcrumbs'] = [
    '#type' => 'details',
    '#title' => t('Breadcrumb'),
    '#open' => TRUE,
  ];

  $form['breadcrumbs']['hide_home_breadcrumb'] = array(
    '#type' => 'radios',
    '#options' => [
      '0' => 'Show <em>Home</em> in breadcrumb trail (Drupal’s default behavior)',
      '1' => 'Remove <em>Home</em> from breadcrumb trail',
      '2' => 'Remove <em>Home</em> when it is the only breadcrumb in trail',
    ],
    '#default_value' => theme_get_setting('hide_home_breadcrumb', 'yourtheme'),
  );

}

Add the following to your theme's yourtheme.theme file:

/**
 * Prepares variables for `breadcrumb.html.twig`.
 */
function yourtheme_preprocess_breadcrumb(&$variables){

  // Remove 'Home' from breadcrumb trail based on theme settings variable.
  //
  // Possible values:
  // - 0: do not remove
  // - 1: remove
  // - 2: remove if its the only item
  $hide_home_breadcrumb = theme_get_setting('hide_home_breadcrumb', 'yourtheme');
  if ($hide_home_breadcrumb == '1' && count($variables['breadcrumb'])) {
    array_shift($variables['breadcrumb']);
  }
  elseif ($hide_home_breadcrumb == '2' && count($variables['breadcrumb']) == 1) {
    array_shift($variables['breadcrumb']);
  }
}

If you want the Home button to be disabled by default when the theme is installed, add the following to your theme's yourtheme.settings.yml:

# Remove 'Home' in breadcrumb trail if its the only item.
hide_home_breadcrumb: '2'

If you're working with an existing site, and are using Drupal Configuration Syncing with Drupal 8, you should also add/modify the yourtheme.settings.yml file in the sync directory, and run drush cim sync.

Share:
11,086
canintex
Author by

canintex

Husband, father, friend, musician, web developer &amp; sushi lover.

Updated on July 18, 2022

Comments

  • canintex
    canintex almost 2 years

    I am using Drupal 6.17 and want to get rid of "HOME" in the breadcrumb output...

    eg:

    $breadcrumb= PRODUCTS // SOFTWARE // FEATURES

    instead of

    HOME // PRODUCTS // SOFTWARE // FEATURES