public function WebformOptionsCustomController::autocomplete in Webform 6.x
Same name and namespace in other branches
- 8.5 modules/webform_options_custom/src/Controller/WebformOptionsCustomController.php \Drupal\webform_options_custom\Controller\WebformOptionsCustomController::autocomplete()
Returns response for the webform options custom autocompletion.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The current request object containing the search string.
Return value
\Symfony\Component\HttpFoundation\JsonResponse A JSON response containing the autocomplete suggestions.
1 string reference to 'WebformOptionsCustomController::autocomplete'
- webform_options_custom.routing.yml in modules/
webform_options_custom/ webform_options_custom.routing.yml - modules/webform_options_custom/webform_options_custom.routing.yml
File
- modules/
webform_options_custom/ src/ Controller/ WebformOptionsCustomController.php, line 24
Class
- WebformOptionsCustomController
- Provides route responses for webform options custom.
Namespace
Drupal\webform_options_custom\ControllerCode
public function autocomplete(Request $request) {
$q = $request->query
->get('q');
$webform_options_custom_storage = $this
->entityTypeManager()
->getStorage('webform_options_custom');
$query = $webform_options_custom_storage
->getQuery()
->range(0, 10)
->sort('label');
// Query title and id.
$or = $query
->orConditionGroup()
->condition('id', $q, 'CONTAINS')
->condition('label', $q, 'CONTAINS');
$query
->condition($or);
$entity_ids = $query
->execute();
if (empty($entity_ids)) {
return new JsonResponse([]);
}
$webform_options_custom = $webform_options_custom_storage
->loadMultiple($entity_ids);
$matches = [];
foreach ($webform_options_custom as $webform_image) {
$value = new FormattableMarkup('@label (@id)', [
'@label' => $webform_image
->label(),
'@id' => $webform_image
->id(),
]);
$matches[] = [
'value' => $value,
'label' => $value,
];
}
return new JsonResponse($matches);
}