Hi,
I had added Taxonomy term parents for a field, please see the screenshot. I am using this module along with Search api solr module. On Tracking I found that the following line on line no 775 in search_api_solr/src/plugin/search_api/backend/SearchApiSolrBackend.php is not giving values for parent.
$values = $field->getValues();
So the value of field is coming blank and is not being sent to the index.
As an interim solution i am using the following code to send it to the index:
/*
* Implements hook_search_api_solr_documents_alter().
*/
function hook_search_api_solr_documents_alter(array $documents, \Drupal\search_api\IndexInterface $index, array $items) {
// Adds a "foo" field with value "bar" to all documents.
if ($index->id() == 'general') {
$storage = \Drupal::service('entity_type.manager')
->getStorage('taxonomy_term');
foreach ($documents as $document) {
if (isset($document->its_field_organization) && !empty($document->its_field_organization)) {
$parents = $storage->loadAllParents($document->its_field_organization);
if (!empty($parents)) {
foreach ($parents as $parent) {
$label = $parent->label();
$text_value = new \Drupal\search_api\Plugin\search_api\data_type\value\TextValue($label);
if (strlen($text_value->getText()) > 32766) {
$label = new TextValue(Unicode::truncateBytes($text_value)->getText(), 32766);
}
$parent_name[] = strtolower($label);
}
}
$document->setField('itm_field_organization_parents_tid', array_keys($parents));
if (!empty($parent_name)) {
$document->setField('tm_field_organization_parents_name', $parent_name);
}
}
}
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| Screen Shot 2017-04-26 at 1.45.47 AM.png | 90.32 KB | sagar ramgade |
Comments
Comment #2
sagar ramgade commentedComment #3
drunken monkeyIndexing taxonomy term parents is currently not supported due to a technical restriction in Drupal Core – see #2822708: Parent term is not available in adding fields for index.