Commit 799ea27e authored by legolasbo's avatar legolasbo Committed by borisson_

Issue #3013991 by mglaman, chr.fritsch, michaellenahan, legolasbo, borisson_:...

Issue #3013991 by mglaman, chr.fritsch, michaellenahan, legolasbo, borisson_: QueryString::buildUrls should not rebuild requests for the same facet source path
parent 4b9e7051
......@@ -57,40 +57,10 @@ class QueryString extends UrlProcessorPluginBase {
// Set the url alias from the facet object.
$this->urlAlias = $facet->getUrlAlias();
$request = $this->request;
$facet_path = $facet->getFacetSource()->getPath();
if ($facet_path) {
$request = Request::create($facet_path);
$request->attributes->set('_format', $this->request->get('_format'));
}
// 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'),
],
]);
}
$requestUrl->setOption('attributes', ['rel' => 'nofollow']);
$facet_source_path = $facet->getFacetSource()->getPath();
$request = $this->getRequestByFacetSourcePath($facet_source_path);
$requestUrl = $this->getUrlForRequest($facet_source_path, $request);
$routeParameters = $this->getUrlRouteParameters();
/** @var \Drupal\facets\Result\ResultInterface[] $results */
foreach ($results as &$result) {
......@@ -207,6 +177,110 @@ class QueryString extends UrlProcessorPluginBase {
return $results;
}
/**
* Gets a request object based on the facet source path.
*
* If the facet's source has a path, we construct a request object based on
* that path, as it may be different than the current request's. This method
* statically caches the request object based on the facet source path so that
* subsequent calls to this processer do not recreate the same request object.
*
* @param string $facet_source_path
* The facet source path.
*
* @return \Symfony\Component\HttpFoundation\Request
* The request.
*/
protected function getRequestByFacetSourcePath($facet_source_path) {
$requestsByPath = &drupal_static(__CLASS__ . __FUNCTION__, []);
if (!$facet_source_path) {
return $this->request;
}
if (array_key_exists($facet_source_path, $requestsByPath)) {
return $requestsByPath[$facet_source_path];
}
$request = Request::create($facet_source_path);
$request->attributes->set('_format', $this->request->get('_format'));
$requestsByPath[$facet_source_path] = $request;
return $request;
}
/**
* Gets the route parameters from the original request.
*
* This method statically caches the route parameters for the request, so that
* subsequent calls to this processor do not re-run Url::createFromRequest for
* the same request.
*
* @return array
* The route parameters.
*/
protected function getUrlRouteParameters() {
$routeParameters = &drupal_static(__CLASS__ . __FUNCTION__, []);
if ($routeParameters) {
return $routeParameters;
}
// Grab any route params from the original request.
try {
$routeParameters = Url::createFromRequest($this->request)->getRouteParameters();
} catch (ResourceNotFoundException $e) {
$routeParameters = [];
}
return $routeParameters;
}
/**
* Gets the URL object for a request.
*
* This method statically caches the URL object for a request based on the
* facet source path. This reduces subsequent calls to the processor from
* having to regenerate the URL object.
*
* @param string $facet_source_path
* The facet source path.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return \Drupal\Core\Url
* The URL.
*/
protected function getUrlForRequest($facet_source_path, Request $request) {
/** @var \Drupal\Core\Url[] $requestUrlsByPath */
$requestUrlsByPath = &drupal_static(__CLASS__ . __FUNCTION__, []);
if (array_key_exists($facet_source_path, $requestUrlsByPath)) {
return $requestUrlsByPath[$facet_source_path];
}
// 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 {
$requestUrl = Url::createFromRequest($request);
}
catch (ResourceNotFoundException $e) {
// Bypass exception if no path available.
// Should be unreachable in default FacetSource implementations,
// but you never know.
if (!$facet_source_path) {
throw $e;
}
$requestUrl = Url::fromUserInput($facet_source_path, [
'query' => [
'_format' => $this->request->get('_format'),
],
]);
}
$requestUrl->setOption('attributes', ['rel' => 'nofollow']);
$requestUrlsByPath[$facet_source_path] = $requestUrl;
return $requestUrl;
}
/**
* Initializes the active filters from the request query.
*
......
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