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

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon