Browse Source

first commit

alans_branch
Paul Pound 1 month ago
commit
fc4d40d39e
  1. 7
      roblib_alter_solr.info.yml
  2. 5
      services.yml
  3. 41
      src/Event/SolrDocumentAlterSubscriber.php

7
roblib_alter_solr.info.yml

@ -0,0 +1,7 @@
name: Roblib Alter Solr
type: module
description: Handles Solr document events.
core_version_requirement: ^10
dependencies:
- search_api
- search_api_solr

5
services.yml

@ -0,0 +1,5 @@
services:
roblib_alter_solr.event_subscriber:
class: Drupal\roblib_alter_solr\EventSubscriber\SolrDocumentAlterSubscriber
tags:
- { name: alterSolrDocument }

41
src/Event/SolrDocumentAlterSubscriber.php

@ -0,0 +1,41 @@
<?php
namespace Drupal\my_module;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Item\ItemInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\search_api_solr\Event\SolrDocumentAlterEvent;
/**
* Provides a Solr document alter event subscriber.
*/
class SolrDocumentAlterSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[SolrDocumentAlterEvent::class][] = ['alterSolrDocument', 800]; // Priority 800 (adjust as needed)
return $events;
}
/**
* Alters the Solr document before it is sent to Solr.
*
* @param \Drupal\search_api_solr\Event\SolrDocumentAlterEvent $event
* The Solr document alter event.
*/
public function alterSolrDocument(SolrDocumentAlterEvent $event) {
$solrDocument = $event->getSolrDocument();
$item = $event->getItem();
$index = $event->getIndex();
if ($item->hasField('field_geo_area_name')) {
$value = $item->getField('field_geo_area_name')->getValue();
$solrDocument->addField('dc.coverage', $value);
}
}
}
Loading…
Cancel
Save