SearchApiFederatedSolrTerms.php in Search API Federated Solr 7.2
File
src/SearchApiFederatedSolrTerms.php
View source
<?php
class SearchApiFederatedSolrTerms extends SearchApiAbstractAlterCallback {
protected $index;
protected $options;
public function propertyInfo() {
return array(
'federated_terms' => array(
'label' => t('Federated Term'),
'description' => t('By adding this field to your search index configuration, you have enabled the federated terms processor to run when new items are indexed. Next, add a "Federated Terms" field to any taxonomy vocabulary whose terms should be mapped to a "federated" term (this helps map terms across vocabularies and sites to a single "federated" term). Then, edit terms in those vocabularies to add the federated term destination value (i.e. "Conditions>Blood Disorders"). Once that tagged content gets indexed, it will have "federated_terms" populated with any matching federated term destination values.'),
'type' => 'list<string>',
'cardinality' => -1,
),
);
}
public function alterItems(array &$items) {
$entity_type = $this->index
->getEntityType();
$entity_info = entity_get_info($entity_type);
foreach ($items as &$item) {
$id = entity_id($entity_type, $item);
$entity = current(entity_load($entity_type, [
$id,
]));
if (!$entity) {
return;
}
$federated_terms_destination_values = [];
$bundle = $entity->{$entity_info['entity keys']['bundle']};
$bundle_fields = field_info_instances($entity_type, $bundle);
$bundle_taxonomy_fields = [];
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'];
}
}
elseif ($bundle_field_info['type'] === "taxonomy_term_reference") {
$bundle_taxonomy_fields[$bundle_field['field_name']] = $bundle_field['label'];
}
}
foreach ($bundle_taxonomy_fields as $taxonomy_field_id => $taxonomy_field_name) {
$lang = $entity->language;
if (isset($entity->{$taxonomy_field_id}[$lang])) {
foreach ($entity->{$taxonomy_field_id}[$lang] as $term_id) {
if (isset($term_id['tid'])) {
$tid = $term_id['tid'];
}
elseif (isset($term_id['target_id'])) {
$tid = $term_id['target_id'];
}
$entity_term = taxonomy_term_load($tid);
$entity_term_fields = field_info_instances('taxonomy_term', $entity_term->vocabulary_machine_name);
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);
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) {
$federated_terms_destination_values[] = $federated_term['value'];
}
}
}
}
}
}
}
if (!empty($federated_terms_destination_values)) {
$item->federated_terms = $federated_terms_destination_values;
}
}
}
}