public function SearchApiFederatedSolrTerms::alterItems in Search API Federated Solr 7
Same name and namespace in other branches
- 7.3 src/SearchApiFederatedSolrTerms.php \SearchApiFederatedSolrTerms::alterItems()
- 7.2 src/SearchApiFederatedSolrTerms.php \SearchApiFederatedSolrTerms::alterItems()
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
- src/
SearchApiFederatedSolrTerms.php, line 32
Class
Code
public function alterItems(array &$items) {
// if (empty($this->options['fields'])) { return; }
$entity_type = $this->index
->getEntityType();
$entity_info = entity_get_info($entity_type);
foreach ($items as &$item) {
$id = entity_id($entity_type, $item);
// Get the entity object for the item being indexed, exit if there's somehow not one.
$entity = current(entity_load($entity_type, [
$id,
]));
if (!$entity) {
return;
}
// Define our array of federated terms destination values.
$federated_terms_destination_values = [];
// Set some helper vars for the entity and bundle type.
$bundle = $entity->{$entity_info['entity keys']['bundle']};
// Get the bundle's fields.
$bundle_fields = field_info_instances($entity_type, $bundle);
// Define array of potential taxonomy fields.
$bundle_taxonomy_fields = [];
// Determine if / which taxonomy fields exist on the entity.
foreach ($bundle_fields as $bundle_field) {
$bundle_field_info = field_info_field($bundle_field['field_name']);
if ($bundle_field_info['type'] === "entityreference") {
if ($bundle_field_info['settings']['target_type'] == 'taxonomy_term') {
$bundle_taxonomy_fields[$bundle_field['field_name']] = $bundle_field['label'];
}
}
}
// For each taxonomy field on the entity, get the terms.
foreach ($bundle_taxonomy_fields as $taxonomy_field_id => $taxonomy_field_name) {
// Iterate through each of the referenced terms.
$lang = $entity->language;
if (isset($entity->{$taxonomy_field_id}[$lang])) {
foreach ($entity->{$taxonomy_field_id}[$lang] as $term_id) {
$entity_term = taxonomy_term_load($term_id['target_id']);
$entity_term_fields = field_info_instances('taxonomy_term', $entity_term->vocabulary_machine_name);
// Iterate through each of the referenced term's fields.
foreach ($entity_term_fields as $entity_term_field) {
$entity_term_field_name = $entity_term_field['field_name'];
$entity_term_field_info = field_info_field($entity_term_field_name);
// Check if the term has a federated_terms field.
if ($entity_term_field_info['type'] === "federated_terms") {
$entity_term_federated_term = $entity_term->{$entity_term_field_name};
if (!empty($entity_term_federated_term)) {
foreach ($entity_term_federated_term['und'] as $federated_term) {
// Add the federated_terms field's value to index.
$federated_terms_destination_values[] = $federated_term['value'];
}
}
}
}
}
}
}
// If there are federated_terms_destination_values save them to the index.
if (!empty($federated_terms_destination_values)) {
$item->federated_terms = $federated_terms_destination_values;
}
}
}