You are here

public function FacetApiAutocompleteController::handleAutocomplete in Select 2 8

Autocomplete the label of an entity.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object that contains the typed tags.

string $facetsource_id: The ID of the facet source.

string $facet_id: The ID of the facet.

string $selection_settings_key: The hashed key of the key/value entry that holds the selection handler settings.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The matched entity labels as a JSON response.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Thrown if the selection settings key is not found in the key/value store or if it does not match the stored data.

\Drupal\facets\Exception\InvalidProcessorException

1 string reference to 'FacetApiAutocompleteController::handleAutocomplete'
select2_facets.routing.yml in modules/select2_facets/select2_facets.routing.yml
modules/select2_facets/select2_facets.routing.yml

File

modules/select2_facets/src/Controller/FacetApiAutocompleteController.php, line 151

Class

FacetApiAutocompleteController
Defines a route controller for facets autocomplete form elements.

Namespace

Drupal\select2_facets\Controller

Code

public function handleAutocomplete(Request $request, $facetsource_id, $facet_id, $selection_settings_key) {
  $matches['results'] = [];

  // Get the typed string from the URL, if it exists.
  if ($input = $request->query
    ->get('q')) {
    $typed_string = mb_strtolower($input);

    // Selection settings are passed in as a hashed key of a serialized array
    // stored in the key/value store.
    $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)) {

        // Disallow access when the selection settings hash does not match the
        // passed-in key.
        throw new AccessDeniedHttpException('Invalid selection settings key.');
      }
    }
    else {

      // Disallow access when the selection settings key is not found in the
      // key/value store.
      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);
}