public function SearchApiAttachmentsUserContentAlterSettings::alterItems in Search API attachments 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 SearchApiAttachmentsAlterSettings::alterItems
File
- contrib/
search_api_attachments_user_content/ includes/ callback_attachments_user_content_settings.inc, line 23 - Search API data alteration callback.
Class
Code
public function alterItems(array &$items) {
// Get the file fields.
$file_fields = $this
->getFileFields();
if (empty($file_fields)) {
return;
}
// Get all nodes authored by the indexed users.
$uids = array();
foreach ($items as $item) {
$uids[] = $item->uid;
}
$sql = 'SELECT nid FROM {node} WHERE uid IN (:uids)';
$nids = db_query($sql, array(
':uids' => $uids,
));
$user_nodes = array();
foreach (node_load_multiple($nids
->fetchCol()) as $node) {
$user_nodes[$node->uid][] = $node;
}
foreach ($items as $item) {
// Index the files content.
if (!empty($user_nodes[$item->uid])) {
foreach ($user_nodes[$item->uid] as $node) {
foreach ($file_fields as $file_field) {
// The node has the file field.
if (!empty($node->{$file_field['field_name']})) {
// Get the files.
$files = field_get_items('node', $node, $file_field['field_name']);
if (!empty($files)) {
// Limit to the max number of value per field.
if (!empty($this->options['number_indexed']) && count($files) > $this->options['number_indexed']) {
$files = array_slice($files, 0, $this->options['number_indexed']);
}
foreach ($files as $file) {
if ($this
->isFileIndexable($file, $item, $file_field['field_name'])) {
$attachments = 'attachments_' . $file_field['field_name'];
if (isset($item->{$attachments})) {
$item->{$attachments} .= ' ' . $this
->getFileContent($file);
}
else {
$item->{$attachments} = $this
->getFileContent($file);
}
}
}
}
}
}
}
}
}
}