You are here

public function PermissionsByTermController::autoCompleteMultiple in Permissions by Term 8.2

Same name and namespace in other branches
  1. 8 src/Controller/PermissionsByTermController.php \Drupal\permissions_by_term\Controller\PermissionsByTermController::autoCompleteMultiple()

Returns JSON response for user's autocomplete field in permissions form.

Return value

JsonResponse The response as JSON.

1 string reference to 'PermissionsByTermController::autoCompleteMultiple'
permissions_by_term.routing.yml in ./permissions_by_term.routing.yml
permissions_by_term.routing.yml

File

src/Controller/PermissionsByTermController.php, line 20

Class

PermissionsByTermController
Default controller for the permissions_by_term module.

Namespace

Drupal\permissions_by_term\Controller

Code

public function autoCompleteMultiple() {

  // The user enters a comma-separated list of users.
  // We only autocomplete the last user.
  $array = Tags::explode($_REQUEST['q']);

  // Fetch last user.
  $last_string = trim(array_pop($array));
  $matches = [];
  $aUserIds = \Drupal::entityQuery('user')
    ->condition('name', $last_string, 'CONTAINS')
    ->execute();
  $prefix = count($array) ? implode(', ', $array) . ', ' : '';
  foreach ($aUserIds as $iUserId) {
    $oUser = user_load($iUserId);
    $matches[$prefix . $oUser
      ->getDisplayName()] = $oUser
      ->getDisplayName();
  }
  return new JsonResponse($matches);
}