You are here

public function CategoryAutocompleteController::autocomplete in Monitoring 8

Retrieves suggestions for sensor category auto completion.

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 'CategoryAutocompleteController::autocomplete'
monitoring.routing.yml in ./monitoring.routing.yml
monitoring.routing.yml

File

src/Controller/CategoryAutocompleteController.php, line 28
Contains \Drupal\monitoring\Controller\CategoryAutocompleteController.

Class

CategoryAutocompleteController
Returns auto complete responses for sensor categories.

Namespace

Drupal\monitoring\Controller

Code

public function autocomplete(Request $request) {

  // Stores the sensor categories.
  $configs = monitoring_sensor_manager()
    ->getAllSensorConfig();
  $categories = array();
  foreach ($configs as $conf) {
    $category = $conf->category;
    if (!in_array($category, $categories)) {
      $categories[] = $category;
    }
  }
  $matches = array();

  // Create the pattern to search in categories.
  $pattern = '/^' . $request->query
    ->get('q') . '/i';
  $prefixMatches = preg_grep($pattern, $categories);
  foreach ($prefixMatches as $config) {
    $matches[] = array(
      'value' => $config,
      'label' => new HtmlEscapedText($config),
    );
  }
  return new JsonResponse($matches);
}