Created
February 20, 2024 12:24
-
-
Save sorenmalling/15f2e4ba7f9c9bff19592da3f060443c to your computer and use it in GitHub Desktop.
Setting document sitehash based on the requested site
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 | |
namespace Vendor\Solr\EventListener; | |
use ApacheSolrForTypo3\Solr\Event\Indexing\BeforeDocumentsAreIndexedEvent; | |
final class ModifyDocumentSiteHash | |
{ | |
public function __invoke(BeforeDocumentsAreIndexedEvent $event): void | |
{ | |
$documents = []; | |
foreach ($event->getDocuments() as $document) { | |
$originalSiteHash = $document->siteHash; | |
$originalId = $document->id; | |
$siteIdentifier = $event->getSite()->getIdentifier(); | |
$newSiteHash = sha1($siteIdentifier . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . 'tx_solr'); | |
$document->setField('id', str_replace($originalSiteHash, $newSiteHash, $originalId)); | |
$document->setField('site', $siteIdentifier); | |
$document->setField('siteHash', $newSiteHash); | |
$documents[] = $document; | |
} | |
$event->setDocuments($documents); | |
} | |
} |
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 | |
namespace Vendor\Solr\EventListener; | |
use ApacheSolrForTypo3\Solr\Event\Search\AfterSearchQueryHasBeenPreparedEvent; | |
use TYPO3\CMS\Core\Http\ServerRequest; | |
final class ModifySearchQuerySiteHash | |
{ | |
public function __invoke(AfterSearchQueryHasBeenPreparedEvent $event): void | |
{ | |
/** @var ServerRequest $request */ | |
$request = $GLOBALS['TYPO3_REQUEST']; | |
$site = $request->getAttribute('site'); | |
$siteIdentifier = $site->getIdentifier(); | |
$newSiteHash = sha1($siteIdentifier . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . 'tx_solr'); | |
$query = $event->getQuery(); | |
$siteHashFilterQuery = $query->getFilterQuery('siteHash'); | |
$siteHashFilterQuery->setOptions([ | |
'query' => 'siteHash:"' . $newSiteHash . '"' | |
]); | |
} | |
} |
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
services: | |
_defaults: | |
autowire: true | |
autoconfigure: true | |
public: false | |
Vendor\Solr\: | |
resource: '../Classes/*' | |
Vendor\Solr\EventListener\ModifyDocumentSiteHash: | |
tags: | |
- name: event.listener | |
identifier: 'Vendor/solr/modify-document-site-hash' | |
Vendor\Solr\EventListener\ModifySearchQuerySiteHash: | |
tags: | |
- name: event.listener | |
identifier: 'Vendor/solr/modify-search-query-site-hash' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment