Drupal 8 - Add Current Page in Breadcrumb

Drupal 8 development

Introduction

Breadcrumb navigation is a core feature in Drupal 8. But by default, breadcrumb do not show current page. In this tutorial we will add current page in the breadcrumb navigation in our theme. This can be achieved by editing our theme file THEME-NAME.theme

Step 1

Open THEME-NAME.theme file of your theme in any text editor.

Step 2

Add following code in THEME-NAME.theme

/**
* Add current page to breadcrumb
*/
function THEME-NAME_preprocess_breadcrumb(&$variables) {
  if (($node = \Drupal::routeMatch()->getParameter('node')) && $variables['breadcrumb']) {
    $variables['breadcrumb'][] = array(
      'text' => $node->getTitle(),
      // comment below line if you do not want to link current page.
      'url'  => $node->URL(),
    );
  }
}

Done!!!

Note:
Change THEME-NAME to your actual theme name in above code.