You are here

public function AutocompleteHelper::access in Search API Autocomplete 8

Checks access to the autocompletion route.

Parameters

\Drupal\search_api_autocomplete\SearchInterface $search_api_autocomplete_search: The configured autocompletion search.

\Drupal\Core\Session\AccountInterface $account: The account.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

Overrides AutocompleteHelperInterface::access

File

src/Utility/AutocompleteHelper.php, line 70

Class

AutocompleteHelper
Provides helper methods for creating autocomplete suggestions.

Namespace

Drupal\search_api_autocomplete\Utility

Code

public function access(SearchInterface $search_api_autocomplete_search, AccountInterface $account) {
  $search = $search_api_autocomplete_search;
  $permission = 'use search_api_autocomplete for ' . $search
    ->id();
  $access = AccessResult::allowedIf($search
    ->status())
    ->andIf(AccessResult::allowedIf($search
    ->hasValidIndex() && $search
    ->getIndex()
    ->status()))
    ->andIf(AccessResult::allowedIfHasPermissions($account, [
    $permission,
    'administer search_api_autocomplete',
  ], 'OR'))
    ->addCacheableDependency($search);
  if ($access instanceof AccessResultReasonInterface) {
    $access
      ->setReason("The \"{$permission}\" permission is required and autocomplete for this search must be enabled.");
  }
  return $access;
}