Skip to content

Commit

Permalink
Add before query event
Browse files Browse the repository at this point in the history
  • Loading branch information
lenvanessen committed Sep 23, 2022
1 parent 89c3543 commit 3dd1f74
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Elasticsearch Plugin

## Unreleased

### Added
- Event to manipulate the search parameters before querying Elasticsearch.

## 1.2.0 - 2022-06-15

### Added
Expand Down
13 changes: 13 additions & 0 deletions src/events/BeforeQueryEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace codemonauts\elastic\events;

use yii\base\Event;

class BeforeQueryEvent extends Event
{
/**
* @var array The params that will be sent to Elasticsearch.
*/
public array $params = [];
}
14 changes: 14 additions & 0 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace codemonauts\elastic\services;

use codemonauts\elastic\Elastic;
use codemonauts\elastic\events\BeforeQueryEvent;
use craft\base\Component;
use craft\base\ElementInterface;
use craft\models\Site;
Expand All @@ -17,6 +18,11 @@
*/
class Elements extends Component
{
/**
* @event BeforeQueryEvent The event that is triggered before the query is sent to ELasticsearch.
*/
public const EVENT_BEFORE_QUERY = 'beforeQuery';

/**
* Adds the keywords of an element to the Elasticsearch index of a site.
*
Expand Down Expand Up @@ -158,6 +164,14 @@ public function search(SearchQuery $searchQuery, array $scope, Site $site)
];
}

// Allow plugins to modify the query parameters
$event = new BeforeQueryEvent([
'params' => $params,
]);
$this->trigger(self::EVENT_BEFORE_QUERY, $event);
$params = $event->params;


return Elastic::$plugin->getElasticsearch()->getClient()->search($params);
}

Expand Down

0 comments on commit 3dd1f74

Please sign in to comment.