You are here

class ViewsBreadCrumbBuilder in Views breadcrumb 8

Same name and namespace in other branches
  1. 8.2 src/Breadcrumb/ViewsBreadcrumbBuilder.php \Drupal\views_breadcrumb\Breadcrumb\ViewsBreadCrumbBuilder

Hierarchy

Expanded class hierarchy of ViewsBreadCrumbBuilder

File

src/Breadcrumb/ViewsBreadcrumbBuilder.php, line 12

Namespace

Drupal\views_breadcrumb\Breadcrumb
View source
class ViewsBreadCrumbBuilder implements BreadcrumbBuilderInterface {

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $attributes) {
    $parameters = $attributes
      ->getParameters()
      ->all();
    if (!empty($parameters['view_id'])) {
      return TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  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);
    $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ViewsBreadCrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
ViewsBreadCrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build