You are here

public function AutocompleteController::handleAutocomplete in Module Builder 8.3

Handler for autocomplete request for properties with extra options.

Parameters

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

string $property_address: The address of the property this autocomplete request is for, as a string imploded with ':'.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The matching options.

1 string reference to 'AutocompleteController::handleAutocomplete'
module_builder.routing.yml in ./module_builder.routing.yml
module_builder.routing.yml

File

src/Controller/AutocompleteController.php, line 29

Class

AutocompleteController
Defines a route controller for properties with extra options.

Namespace

Drupal\module_builder\Controller

Code

public function handleAutocomplete(Request $request, $property_address) {
  $results = [];
  if ($input = $request->query
    ->get('q')) {
    try {

      // TODO: inject.
      $generate_task = \Drupal::service('module_builder.drupal_code_builder')
        ->getTask('Generate', 'module');
    } catch (\Exception $e) {

      // If we get here we should be ok.
    }

    // Get the definition that the autocomplete is for.
    $component_data = $generate_task
      ->getRootComponentData();
    $root_definition = $component_data
      ->getDefinition();
    $autocomplete_property = $root_definition
      ->getNestedProperty($property_address);
    $options = array_keys($autocomplete_property
      ->getOptions());
    $matched_keys = preg_grep("@{$input}@", $options);
    foreach ($matched_keys as $key) {
      $results[] = [
        'value' => $key,
        'label' => $key,
      ];
    }
  }
  return new JsonResponse($results);
}