You are here

public function ZoneSelectionForm::autocompleteZone in CloudFlare 8

Retrieves suggestions for zone autocompletion.

Parameters

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

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response containing autocomplete suggestions.

1 string reference to 'ZoneSelectionForm::autocompleteZone'
cloudflare.routing.yml in ./cloudflare.routing.yml
cloudflare.routing.yml

File

src/Form/ZoneSelectionForm.php, line 236

Class

ZoneSelectionForm
Class ZoneSelectionForm.

Namespace

Drupal\cloudflare\Form

Code

public function autocompleteZone(Request $request) {
  $zone_autocomplete_text = $request->query
    ->get('q');
  $matches = [];

  // Tracks if the current CloudFlare account has multiple zones.

  /** @var \CloudFlarePhpSdk\ApiTypes\Zone\Zone $zone */
  foreach ($this->zoneApi
    ->listZones() as $zone) {
    if (stripos($zone
      ->getName(), $zone_autocomplete_text) === 0) {
      $matches[] = [
        'value' => $zone
          ->getZoneId(),
        'label' => $zone
          ->getName(),
      ];
    }
  }
  return new JsonResponse($matches);
}