public function RoleAccess::preprocessSearchQuery in Search API 8
Preprocesses a search query.
Parameters
\Drupal\search_api\Query\QueryInterface $query: The object representing the query to be executed.
Overrides ProcessorPluginBase::preprocessSearchQuery
File
- src/
Plugin/ search_api/ processor/ RoleAccess.php, line 180
Class
- RoleAccess
- Adds access checks based on user roles.
Namespace
Drupal\search_api\Plugin\search_api\processorCode
public function preprocessSearchQuery(QueryInterface $query) {
if ($query
->getOption('search_api_bypass_access')) {
return;
}
$account = $query
->getOption('search_api_access_account', $this
->getCurrentUser());
if (is_numeric($account)) {
$account = User::load($account);
}
$role_field = $this
->findField(NULL, static::ROLE_ACCESS_FIELD, 'string');
if ($role_field) {
$query
->addCondition($role_field
->getFieldIdentifier(), $account
->getRoles(), 'IN');
}
else {
$query
->abort();
$this
->getLogger()
->warning('Role-based access checks could not be added to a search query on index %index since the required field is not available. Please re-save the index.', [
'%index' => $query
->getIndex()
->label(),
]);
}
}