Created
May 3, 2024 09:57
-
-
Save johnny-aroza/ac5101596a4aa1753eca71fa203e39ff to your computer and use it in GitHub Desktop.
Search API Event Suscriber to update custom value
This file contains hidden or 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 | |
| declare(strict_types=1); | |
| namespace Drupal\ecm_solr\EventSubscriber; | |
| use Drupal\search_api\Event\SearchApiEvents; | |
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
| use Drupal\search_api\Event\IndexingItemsEvent; | |
| /** | |
| * @todo Add description for this subscriber. | |
| */ | |
| final class EcmSolrSubscriber implements EventSubscriberInterface { | |
| public function indexingItems(IndexingItemsEvent $event): void { | |
| //this can be used to change the stage as well | |
| $index = $event->getIndex(); | |
| $items = $event->getItems(); | |
| foreach ($items as $item) { | |
| $fields = $item->getFields(); | |
| foreach ($fields as $field) { | |
| //Get field name | |
| //Check here if ts & ss can be appended | |
| $fieldName = $field->getFieldIdentifier(); | |
| //value got updated here | |
| if ($fieldName == 'jackson_string'){ | |
| $field->setValues(['New John Value']); | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function getSubscribedEvents(): array { | |
| $events[SearchApiEvents::INDEXING_ITEMS][] = ['indexingItems']; | |
| return $events; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment