FacetApiAutocompleteController.php in Select 2 8
File
modules/select2_facets/src/Controller/FacetApiAutocompleteController.php
View source
<?php
namespace Drupal\select2_facets\Controller;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\Routing\AccessAwareRouterInterface;
use Drupal\Core\Site\Settings;
use Drupal\facets\FacetManager\DefaultFacetManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class FacetApiAutocompleteController extends ControllerBase {
protected $facetManager;
protected $requestStack;
protected $currentPathStack;
protected $router;
protected $pathProcessor;
protected $storedRequests = [];
public static function create(ContainerInterface $container) {
$controller = parent::create($container);
$controller
->setFacetManager($container
->get('facets.manager'));
$controller
->setRequestStack($container
->get('request_stack'));
$controller
->setCurrentPathStack($container
->get('path.current'));
$controller
->setRouter($container
->get('router'));
$controller
->setPathProcessor($container
->get('path_processor_manager'));
return $controller;
}
protected function setFacetManager(DefaultFacetManager $facetManager) {
$this->facetManager = $facetManager;
}
protected function setRequestStack(RequestStack $requestStack) {
$this->requestStack = $requestStack;
}
protected function setCurrentPathStack(CurrentPathStack $currentPathStack) {
$this->currentPathStack = $currentPathStack;
}
protected function setRouter(AccessAwareRouterInterface $router) {
$this->router = $router;
}
protected function setPathProcessor(InboundPathProcessorInterface $pathProcessor) {
$this->pathProcessor = $pathProcessor;
}
public function handleAutocomplete(Request $request, $facetsource_id, $facet_id, $selection_settings_key) {
$matches['results'] = [];
if ($input = $request->query
->get('q')) {
$typed_string = mb_strtolower($input);
$selection_settings = $this
->keyValue('entity_autocomplete')
->get($selection_settings_key, FALSE);
if ($selection_settings !== FALSE) {
$selection_settings_hash = Crypt::hmacBase64(serialize($selection_settings) . $facetsource_id . $facet_id, Settings::getHashSalt());
if (!hash_equals($selection_settings_hash, $selection_settings_key)) {
throw new AccessDeniedHttpException('Invalid selection settings key.');
}
}
else {
throw new AccessDeniedHttpException();
}
$new_request = $this
->createRequestFromPath($selection_settings['path']);
$request->attributes
->add($this->router
->matchRequest($new_request));
$this
->overwriteRequestStack($new_request);
$facets = $this->facetManager
->getFacetsByFacetSourceId($facetsource_id);
foreach ($facets as $facet) {
if ($facet
->id() != $facet_id) {
continue;
}
$this->facetManager
->build($facet);
foreach ($facet
->getResults() as $result) {
$display_value = mb_strtolower($result
->getDisplayValue());
if ($selection_settings['match_operator'] == 'CONTAINS' && strpos($display_value, $typed_string) === FALSE || $selection_settings['match_operator'] == 'STARTS_WITH' && strpos($display_value, $typed_string) !== 0) {
continue;
}
$matches['results'][] = [
'id' => $result
->getUrl()
->toString(),
'text' => $selection_settings['show_numbers'] ? sprintf('%s (%d)', $result
->getDisplayValue(), $result
->getCount()) : $result
->getDisplayValue(),
];
}
}
$this
->restoreRequestStack();
}
return new JsonResponse($matches);
}
protected function createRequestFromPath($path) {
$new_request = Request::create($path);
$processed = $this->pathProcessor
->processInbound($path, $new_request);
$this->currentPathStack
->setPath($processed);
return $new_request;
}
protected function overwriteRequestStack(Request $request) {
while ($this->requestStack
->getCurrentRequest()) {
$this->storedRequests[] = $this->requestStack
->pop();
}
$this->requestStack
->push($request);
}
protected function restoreRequestStack() {
$this->requestStack
->pop();
foreach ($this->storedRequests as $request) {
$this->requestStack
->push($request);
}
}
}