You are here

public function ContributeAutocompleteController::autocomplete in Contribute 8

Returns account type autocomplete matches.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

string $account_type: The account type to autocomplete.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

1 string reference to 'ContributeAutocompleteController::autocomplete'
contribute.routing.yml in ./contribute.routing.yml
contribute.routing.yml

File

src/Controller/ContributeAutocompleteController.php, line 55

Class

ContributeAutocompleteController
Class ContributeAutocompleteController.

Namespace

Drupal\contribute\Controller

Code

public function autocomplete(Request $request, $account_type = 'user') {
  $q = $request->query
    ->get('q');
  switch ($account_type) {
    case 'user':
      $response = $this->httpClient
        ->get('https://www.drupal.org/index.php?q=admin/views/ajax/autocomplete/user/' . urlencode($q));
      $data = Json::decode($response
        ->getBody());
      $matches = [];
      foreach ($data as $value) {
        $matches[] = [
          'value' => $value,
          'label' => $value,
        ];
      }
      return new JsonResponse($matches);
    case 'organization':
      $response = $this->httpClient
        ->get('https://www.drupal.org/index.php?q=entityreference/autocomplete/tags/field_for_customer/comment/comment_node_project_issue/NULL/' . urlencode($q));
      $data = Json::decode($response
        ->getBody());
      $matches = [];
      foreach ($data as $value) {
        $value = strip_tags($value);
        $matches[] = [
          'value' => $value,
          'label' => $value,
        ];
      }
      return new JsonResponse($matches);
  }
}