You are here

public function ViewsBreadCrumbBuilder::build in Views breadcrumb 8.2

Same name and namespace in other branches
  1. 8 src/Breadcrumb/ViewsBreadcrumbBuilder.php \Drupal\views_breadcrumb\Breadcrumb\ViewsBreadCrumbBuilder::build()

Builds the breadcrumb.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.

Return value

\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.

Overrides BreadcrumbBuilderInterface::build

File

src/Breadcrumb/ViewsBreadcrumbBuilder.php, line 26

Class

ViewsBreadCrumbBuilder

Namespace

Drupal\views_breadcrumb\Breadcrumb

Code

public function build(RouteMatchInterface $route_match) {
  $breadcrumb = new Breadcrumb();
  $breadcrumb
    ->addLink(Link::createFromRoute('Home', '<front>'));
  $path = \Drupal::service('path.current')
    ->getPath();
  $url_object = \Drupal::service('path.validator')
    ->getUrlIfValid($path);
  $route_name = $url_object
    ->getRouteName();
  $query = \Drupal::database()
    ->select('menu_tree', 'm');
  $query
    ->fields('m');
  $query
    ->condition('m.route_name', $route_name);
  $query
    ->condition('m.provider', 'views');
  $result = $query
    ->execute()
    ->fetchAssoc();
  $depth = $result['depth'];
  if (empty($result)) {
    return $breadcrumb;
  }
  for ($i = 1; $i < $depth; $i++) {
    $query = \Drupal::database()
      ->select('menu_tree', 'm');
    $query
      ->fields('m');
    $query
      ->condition('m.mlid', $result['p' . $i]);
    $result1 = $query
      ->execute()
      ->fetchAssoc();
    $breadcrumb
      ->addLink(Link::createFromRoute(unserialize($result1['title']), $result1['route_name']));
  }
  return $breadcrumb;
}