Same filename and directory in other branches
- 8.9.x core/modules/system/src/PathBasedBreadcrumbBuilder.php
- 9 core/modules/system/src/PathBasedBreadcrumbBuilder.php
Namespace
Drupal\system
File
core/modules/system/src/PathBasedBreadcrumbBuilder.php
View source
<?php
namespace Drupal\system;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Link;
use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
class PathBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
protected $context;
protected $accessManager;
protected $router;
protected $pathProcessor;
protected $config;
protected $titleResolver;
protected $currentUser;
protected $currentPath;
protected $pathMatcher;
public function __construct(RequestContext $context, AccessManagerInterface $access_manager, RequestMatcherInterface $router, InboundPathProcessorInterface $path_processor, ConfigFactoryInterface $config_factory, TitleResolverInterface $title_resolver, AccountInterface $current_user, CurrentPathStack $current_path, PathMatcherInterface $path_matcher = NULL) {
$this->context = $context;
$this->accessManager = $access_manager;
$this->router = $router;
$this->pathProcessor = $path_processor;
$this->config = $config_factory
->get('system.site');
$this->titleResolver = $title_resolver;
$this->currentUser = $current_user;
$this->currentPath = $current_path;
$this->pathMatcher = $path_matcher ?: \Drupal::service('path.matcher');
}
public function applies(RouteMatchInterface $route_match) {
return TRUE;
}
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$links = [];
$breadcrumb
->addCacheContexts([
'url.path.parent',
'url.path.is_front',
]);
if ($this->pathMatcher
->isFrontPage()) {
return $breadcrumb;
}
$path = trim($this->context
->getPathInfo(), '/');
$path_elements = explode('/', $path);
$exclude = [];
$front = $this->config
->get('page.front');
$exclude[$front] = TRUE;
$exclude['/user'] = TRUE;
while (count($path_elements) > 1) {
array_pop($path_elements);
$route_request = $this
->getRequestForPath('/' . implode('/', $path_elements), $exclude);
if ($route_request) {
$route_match = RouteMatch::createFromRequest($route_request);
$access = $this->accessManager
->check($route_match, $this->currentUser, NULL, TRUE);
$breadcrumb = $breadcrumb
->addCacheableDependency($access);
if ($access
->isAllowed()) {
$title = $this->titleResolver
->getTitle($route_request, $route_match
->getRouteObject());
if (!isset($title)) {
$title = str_replace([
'-',
'_',
], ' ', Unicode::ucfirst(end($path_elements)));
}
$url = Url::fromRouteMatch($route_match);
$links[] = new Link($title, $url);
}
}
}
$links[] = Link::createFromRoute($this
->t('Home'), '<front>');
return $breadcrumb
->setLinks(array_reverse($links));
}
protected function getRequestForPath($path, array $exclude) {
if (!empty($exclude[$path])) {
return NULL;
}
$request = Request::create($path);
$request->headers
->set('Accept', 'text/html');
$processed = $this->pathProcessor
->processInbound($path, $request);
if (empty($processed) || !empty($exclude[$processed])) {
return NULL;
}
$this->currentPath
->setPath($processed, $request);
try {
$request->attributes
->add($this->router
->matchRequest($request));
return $request;
} catch (ParamNotConvertedException|ResourceNotFoundException|MethodNotAllowedException|AccessDeniedHttpException|NotFoundHttpException $e) {
return NULL;
}
}
}
Classes