Skip to content

Commit

Permalink
fix breadcrumb
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinimone committed Jan 9, 2020
1 parent cbf9966 commit e453ceb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
11 changes: 6 additions & 5 deletions src/Controller/BreadcrumbController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\breadcrumb_builder\Controller;

use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Controller\ControllerBase;
use Drupal\breadcrumb_builder\BreadcrumbsManager;
use Drupal\breadcrumb_builder\BreadcrumbResult;
Expand All @@ -27,15 +28,15 @@ public static function create(ContainerInterface $container) {
$container->get('breadcrumb_builder.route_match_builder')
);
}

public function build(Request $request) {
$path = $request->query->get('path');

$route_match = $this->routeMatchBuilder->getRouteMatchForPath($path);
$result = $this->breadcrumbManager->build($route_match);
$result = $route_match ? $this->breadcrumbManager->build($route_match) : NULL;

$response = CacheableJsonResponse::create($result->getResult());
$response->addCacheableDependency($result);
$response->addCacheableDependency($result);

return $response;
}
Expand Down
11 changes: 5 additions & 6 deletions src/PathBreadcrumbBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public function __construct(BreadcrumbsManager $breadcrumbManager, TitleResolver
$this->pathValidator = $pathValidator;
$this->routeMatchBuilder = $routeMatchBuilder;
}

/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
return TRUE;
}
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -62,16 +62,15 @@ public function build(RouteMatchInterface $route_match) {
}
}
}

$route_request = $this->routeMatchBuilder->getRequestForPath('/' . implode('/', $path_elements), $exclude);

if ($route_request) {
$route_match = RouteMatch::createFromRequest($route_request);

$title = $this->getTitle($route_request, $route_match, end($path_elements));

$url = Url::fromRouteMatch($route_match)->toString(TRUE);
$result->addCacheableDependency($url);

$result->addResultItem([
'url' => $url->getGeneratedUrl(),
'title' => $title,
Expand All @@ -91,4 +90,4 @@ protected function getTitle($request, $route_match, $path_element) {

return $title;
}
}
}
9 changes: 7 additions & 2 deletions src/RouteMatchBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace Drupal\breadcrumb_builder;

use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\Routing\RouteMatch;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RouterInterface;

class RouteMatchBuilder {
Expand All @@ -29,6 +33,7 @@ public function getRequestForPath($path, array $exclude = []) {
// Find the system path by resolving aliases, language prefix, etc.
$processed = $this->pathProcessor->processInbound($path, $request);


// Attempt to match this path to provide a fully built request.
try {
$request->attributes->add($this->router->matchRequest($request));
Expand All @@ -50,6 +55,6 @@ public function getRequestForPath($path, array $exclude = []) {

public function getRouteMatchForPath($path, array $exclude = []) {
$request = $this->getRequestForPath($path, $exclude);
return RouteMatch::createFromRequest($request);
return $request ? RouteMatch::createFromRequest($request) : NULL;
}
}
}

0 comments on commit e453ceb

Please sign in to comment.