public function ContactAutocompleteController::autocomplete in RedHen CRM 8
Handler for autocomplete request.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request.
Return value
\Symfony\Component\HttpFoundation\JsonResponse The response.
1 string reference to 'ContactAutocompleteController::autocomplete'
- redhen_contact.routing.yml in modules/
redhen_contact/ redhen_contact.routing.yml - modules/redhen_contact/redhen_contact.routing.yml
File
- modules/
redhen_contact/ src/ Controller/ ContactAutocompleteController.php, line 42
Class
- ContactAutocompleteController
- Class ContactAutocompleteController.
Namespace
Drupal\redhen_contact\ControllerCode
public function autocomplete(Request $request) {
$results = [];
// Sanitize input parameters.
$q = trim(str_replace('%', '', Xss::filter($request->query
->get('q'))));
$match_operator = Xss::filter($request->query
->get('match_operator'));
$match_limit = intval($request->query
->get('match_limit'));
if ($match_limit <= 0) {
$match_limit = 10;
}
elseif ($match_limit > 100) {
$match_limit = 100;
}
if ($q) {
if ($match_operator == 'STARTS_WITH') {
$full_name_like = preg_replace('/\\s+/', '% ', $q) . '%';
$email_like = $q . '%';
}
else {
$full_name_like = '%' . preg_replace('/\\s+/', '% %', $q) . '%';
$email_like = '%' . $q . '%';
}
$query = $this->database
->query('
SELECT id
FROM redhen_contact
WHERE CONCAT(first_name, \' \', last_name) LIKE :full_name_like
OR email LIKE :email_like
ORDER BY first_name ASC, last_name ASC
LIMIT ' . $match_limit . '
', [
':full_name_like' => $full_name_like,
':email_like' => $email_like,
]);
$contact_ids = $query
->fetchCol();
$contacts = $this
->entityTypeManager()
->getStorage('redhen_contact')
->loadMultiple($contact_ids);
/** @var \Drupal\redhen_contact\Entity\Contact $contact */
foreach ($contacts as $contact) {
$results[] = [
'value' => EntityAutocomplete::getEntityLabels([
$contact,
]),
'label' => $contact
->label() . ' (' . $contact
->getEmail() . ')',
];
}
}
return new JsonResponse($results);
}