n Add the current title to the breadcrumb in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

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
            );
        }
    }
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook