public function SearchApiAlterNodeAccess::alterItems in Search API 7
Alter items before indexing.
Items which are removed from the array won't be indexed, but will be marked as clean for future indexing. This could for instance be used to implement some sort of access filter for security purposes (e.g., don't index unpublished nodes or comments).
Parameters
array $items: An array of items to be altered, keyed by item IDs.
Overrides SearchApiAlterCallbackInterface::alterItems
File
- includes/
callback_node_access.inc, line 40 - Contains the SearchApiAlterNodeAccess class.
Class
- SearchApiAlterNodeAccess
- Adds node access information to node indexes.
Code
public function alterItems(array &$items) {
static $account;
if (!isset($account)) {
// Load the anonymous user.
$account = drupal_anonymous_user();
}
foreach ($items as $id => $item) {
$node = $this
->getNode($item);
// Check whether all users have access to the node.
if (!node_access('view', $node, $account)) {
// Get node access grants.
$result = db_query('SELECT * FROM {node_access} WHERE (nid = 0 OR nid = :nid) AND grant_view = 1', array(
':nid' => $node->nid,
));
// Store all grants together with their realms in the item.
foreach ($result as $grant) {
$items[$id]->search_api_access_node[] = "node_access_{$grant->realm}:{$grant->gid}";
}
}
else {
// Add the generic view grant if we are not using node access or the
// node is viewable by anonymous users.
$items[$id]->search_api_access_node = array(
'node_access__all',
);
}
}
}