You are here

public function RoleAccess::addFieldValues in Search API 8

Adds the values of properties defined by this processor to the item.

Parameters

\Drupal\search_api\Item\ItemInterface $item: The item whose field values should be added.

Overrides ProcessorPluginBase::addFieldValues

File

src/Plugin/search_api/processor/RoleAccess.php, line 121

Class

RoleAccess
Adds access checks based on user roles.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function addFieldValues(ItemInterface $item) {
  $role_has_access = function (RoleInterface $role) use ($item) {
    $transient_account = $this
      ->createTransientAccountWithRole($role);
    return $item
      ->getDatasource()
      ->getItemAccessResult($item
      ->getOriginalObject(), $transient_account)
      ->isAllowed();
  };
  $allowed_roles = array_filter(user_roles(), $role_has_access);
  $allowed_roles = array_map(function (RoleInterface $role) {
    return $role
      ->id();
  }, $allowed_roles);
  $fields = $item
    ->getFields();
  $fields = $this
    ->getFieldsHelper()
    ->filterForPropertyPath($fields, NULL, static::ROLE_ACCESS_FIELD);
  foreach ($fields as $field) {
    $field
      ->setValues($allowed_roles);
  }
}