commit
fc4d40d39e
3 changed files with 53 additions and 0 deletions
@ -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 |
@ -0,0 +1,5 @@ |
|||||||
|
services: |
||||||
|
roblib_alter_solr.event_subscriber: |
||||||
|
class: Drupal\roblib_alter_solr\EventSubscriber\SolrDocumentAlterSubscriber |
||||||
|
tags: |
||||||
|
- { name: alterSolrDocument } |
@ -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…
Reference in new issue