public function TokenAutocompleteController::autocomplete in Token 8
Retrieves suggestions for block category autocompletion.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The current request.
string $token_type: The token type.
string $filter: The autocomplete filter.
Return value
\Symfony\Component\HttpFoundation\JsonResponse A JSON response containing autocomplete suggestions.
1 string reference to 'TokenAutocompleteController::autocomplete'
File
- src/
Controller/ TokenAutocompleteController.php, line 47
Class
- TokenAutocompleteController
- Returns autocomplete responses for tokens.
Namespace
Drupal\token\ControllerCode
public function autocomplete($token_type, $filter, Request $request) {
$filter = substr($filter, strrpos($filter, '['));
$matches = [];
if (!mb_strlen($filter)) {
$matches["[{$token_type}:"] = 0;
}
else {
$depth = max(1, substr_count($filter, ':'));
$tree = $this->treeBuilder
->buildTree($token_type, [
'flat' => TRUE,
'depth' => $depth,
]);
foreach (array_keys($tree) as $token) {
if (strpos($token, $filter) === 0) {
$matches[$token] = levenshtein($token, $filter);
if (isset($tree[$token]['children'])) {
$token = rtrim($token, ':]') . ':';
$matches[$token] = levenshtein($token, $filter);
}
}
}
}
asort($matches);
$keys = array_keys($matches);
$matches = array_combine($keys, $keys);
return new JsonResponse($matches);
}