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

Comments

Add new comment

Restricted HTML

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

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

Web Posts

Search

Page Facebook