Created
February 26, 2024 16:53
-
-
Save sam2de/7fe0809c878cf907630d8c36ab84c1af to your computer and use it in GitHub Desktop.
add entity type to solr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
sdgs_helper.twig.goals_count_extension: | |
class: Drupal\sdgs_helper\Twig\GoalsCountExtension | |
arguments: ['@config.factory'] | |
tags: | |
- { name: twig.extension } | |
sdgs_helper.solr_search_alter_documents: | |
class: Drupal\sdgs_helper\EventSubscriber\SearchAlterDocuments | |
tags: | |
- { name: 'event_subscriber' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#web/modules/custom/sdgs_helper/src/EventSubscriber/SearchAlterDocuments.php | |
namespace Drupal\sdgs_helper\EventSubscriber; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Drupal\search_api_solr\Event\SearchApiSolrEvents; | |
use Drupal\search_api_solr\Event\PostCreateIndexDocumentEvent; | |
class SearchAlterDocuments implements EventSubscriberInterface { | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getSubscribedEvents(): array { | |
return [ | |
SearchApiSolrEvents::POST_CREATE_INDEX_DOCUMENT => 'postCreateIndexDocuments', | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function postCreateIndexDocuments(PostCreateIndexDocumentEvent $event): void { | |
// Add a "foo" field with value "bar" to the document. | |
$document = $event->getSolariumDocument(); | |
$document_fields = $document->getFields(); | |
if ($document['ss_type'] == 'partnerships' && !empty($document['fts_field_entity_types'])) { | |
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'partnerships'); | |
if (array_key_exists('field_entity_types', $fields)) { | |
/** @var \Drupal\field\Entity\FieldConfig $fieldConfig */ | |
$fieldConfig = $fields['field_entity_types']; | |
$allowedValues = $fieldConfig->getSetting('allowed_values'); | |
} | |
if (!empty($allowedValues[$document['fts_field_entity_types']])) { | |
$document->setField('sm_field_entity_types_name', $allowedValues[$document['fts_field_entity_types']]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment