Code snippet that can be used to add the current title to the breadcrumb in Drupal 8.
Example in node pages:
/**
* @param $variables
*/
function THEMENAME_preprocess_breadcrumb(&$variables){
if(($node = \Drupal::routeMatch()->getParameter('node')) && $variables['breadcrumb']){
// Adding the a divider of between home an the title of the page.
$variables['breadcrumb'][] = array(
'text' => '>'
);
// Adding the title of the page in the breadcrumb
$variables['breadcrumb'][] = array(
'text' => $node->getTitle(),
'url' => $node->URL()
);
}
}
Example in views pages:
/**
* @param $variables
*/
function THEMENAME_preprocess_breadcrumb(&$variables)
{
$route = \Drupal::routeMatch()->getRouteObject();
$request = \Drupal::request();
if ($route) {
$view_id = $route->getDefault('view_id');
if (!empty($view_id) && $view_id =='VIEW_ID') {
$page_title = \Drupal::service('title_resolver')->getTitle($request, $route);
$variables['breadcrumb'][] = array(
'text' => $page_title
);
}
}
}