Commit 02f0e68a authored by chr.fritsch's avatar chr.fritsch Committed by borisson_

Issue #3031581 by mtodor, chr.fritsch, borisson_: Provide JavaScript API for facet widgets

parent ece1a1ca
......@@ -7,6 +7,7 @@ use Drupal\Core\Url;
use Drupal\facets\FacetInterface;
use Drupal\facets\UrlProcessor\UrlProcessorPluginBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
/**
* Query string URL processor.
......@@ -57,17 +58,38 @@ class QueryString extends UrlProcessorPluginBase {
$this->urlAlias = $facet->getUrlAlias();
$request = $this->request;
if ($facet->getFacetSource()->getPath()) {
$request = Request::create($facet->getFacetSource()->getPath());
$facet_path = $facet->getFacetSource()->getPath();
if ($facet_path) {
$request = Request::create($facet_path);
$request->attributes->set('_format', $this->request->get('_format'));
}
// Grab any route params from the original request.
$routeParameters = Url::createFromRequest($this->request)
->getRouteParameters();
// Try to grab any route params from the original request.
// In case of request path not having a matching route, Url generator will
// fail with
try {
$routeParameters = Url::createFromRequest($this->request)
->getRouteParameters();
$requestUrl = Url::createFromRequest($request);
}
catch (ResourceNotFoundException $e) {
$routeParameters = [];
// Bypass exception if no path available.
// Should be unreachable in default FacetSource implementations,
// but you never know.
if (!$facet_path) {
throw $e;
}
$requestUrl = Url::fromUserInput($facet_path, [
'query' => [
'_format' => $this->request->get('_format'),
]
]);
}
// Create a request url.
$requestUrl = Url::createFromRequest($request);
$requestUrl->setOption('attributes', ['rel' => 'nofollow']);
/** @var \Drupal\facets\Result\ResultInterface[] $results */
......
......@@ -18,6 +18,7 @@ use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
/**
* Unit test for processor.
......@@ -355,6 +356,39 @@ class QueryStringTest extends UnitTestCase {
}
}
/**
* Tests that unrouted paths can be handled properly.
*/
public function testUnroutedPath() {
// Override router.
$router = $this->getMockBuilder(TestRouterInterface::class)
->disableOriginalConstructor()
->getMock();
$router->expects($this->any())
->method('matchRequest')
->willThrowException(new ResourceNotFoundException());
// Get the container from the setUp method and change it with the
// implementation created here, that has the route parameters.
$container = \Drupal::getContainer();
$container->set('router.no_access_checks', $router);
\Drupal::setContainer($container);
// Create facet.
$facet = new Facet([], 'facets_facet');
$facet->setFieldIdentifier('test');
$facet->setUrlAlias('test');
$facet->setFacetSourceId('facet_source__dummy');
$this->processor = new QueryString(['facet' => $facet], 'query_string', [], new Request(), $this->entityManager);
$results = $this->processor->buildUrls($facet, $this->originalResults);
foreach ($results as $result) {
$this->assertEquals('base:test', $result->getUrl()->getUri());
}
}
/**
* Sets up a container.
*/
......@@ -375,7 +409,7 @@ class QueryStringTest extends UnitTestCase {
->disableOriginalConstructor()
->getMock();
$fsi->method('getPath')
->willReturn('test');
->willReturn('/test');
$manager = $this->getMockBuilder(FacetSourcePluginManager::class)
->disableOriginalConstructor()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment