WebformOptionsCustomController.php in Webform 6.x
File
modules/webform_options_custom/src/Controller/WebformOptionsCustomController.php
View source
<?php
namespace Drupal\webform_options_custom\Controller;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class WebformOptionsCustomController extends ControllerBase {
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');
$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);
}
}