Skip to content

Instantly share code, notes, and snippets.

@johnny-aroza
Created May 3, 2024 09:57
Show Gist options
  • Select an option

  • Save johnny-aroza/ac5101596a4aa1753eca71fa203e39ff to your computer and use it in GitHub Desktop.

Select an option

Save johnny-aroza/ac5101596a4aa1753eca71fa203e39ff to your computer and use it in GitHub Desktop.
Search API Event Suscriber to update custom value
<?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