public function ContentAccess::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/ ContentAccess.php, line 150
Class
- ContentAccess
- Adds content access checks for nodes and comments.
Namespace
Drupal\search_api\Plugin\search_api\processorCode
public function addFieldValues(ItemInterface $item) {
static $anonymous_user;
if (!isset($anonymous_user)) {
// Load the anonymous user.
$anonymous_user = new AnonymousUserSession();
}
// Only run for node and comment items.
$entity_type_id = $item
->getDatasource()
->getEntityTypeId();
if (!in_array($entity_type_id, [
'node',
'comment',
])) {
return;
}
// Get the node object.
$node = $this
->getNode($item
->getOriginalObject());
if (!$node) {
// Apparently we were active for a wrong item.
return;
}
$fields = $item
->getFields();
$fields = $this
->getFieldsHelper()
->filterForPropertyPath($fields, NULL, 'search_api_node_grants');
foreach ($fields as $field) {
// Collect grant records for the node. If there are none, use the pseudo
// grant "node_access__all".
$sql = 'SELECT gid, realm FROM {node_access} WHERE (nid = 0 OR nid = :nid) AND grant_view = 1';
$args = [
':nid' => $node
->id(),
];
$grant_records = $this
->getDatabase()
->query($sql, $args)
->fetchAll();
if ($grant_records) {
foreach ($grant_records as $grant) {
$field
->addValue("node_access_{$grant->realm}:{$grant->gid}");
}
}
else {
// Add the generic pseudo view grant if we are not using node access.
$field
->addValue('node_access__all');
}
}
}