You are here

public function AutocompletionConfigurationEditForm::viewAutocomplete in Search Autocomplete 8

Same name and namespace in other branches
  1. 2.x src/Form/AutocompletionConfigurationEditForm.php \Drupal\search_autocomplete\Form\AutocompletionConfigurationEditForm::viewAutocomplete()

Autocomplete the label of a view.

Parameters

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

Return value

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

1 string reference to 'AutocompletionConfigurationEditForm::viewAutocomplete'
search_autocomplete.routing.yml in ./search_autocomplete.routing.yml
search_autocomplete.routing.yml

File

src/Form/AutocompletionConfigurationEditForm.php, line 284

Class

AutocompletionConfigurationEditForm
Class AutocompletionConfigurationEditForm

Namespace

Drupal\search_autocomplete\Form

Code

public function viewAutocomplete(Request $request) {
  $matches = [];

  // Retrieve elligible views.
  $displays = Views::getApplicableViews('autocompletion_callback_display');

  // Add the view as a suggestion if meeting user_input
  $options = [];
  foreach ($displays as $data) {
    list($view_id, $display_id) = $data;
    $view = Views::getView($view_id);
    $display = $view
      ->getDisplay();
    $suggestion_value = $view->storage
      ->get('id') . '::' . $display_id;
    $suggestion_label = $view->storage
      ->get('label') . '::' . $display->display['display_title'];
    if (stristr($suggestion_label, $request->query
      ->get('q')) !== FALSE) {
      $matches[] = [
        'value' => $suggestion_value,
        'label' => $suggestion_label,
      ];
    }
  }
  return new JsonResponse($matches);
}