Skip to content

Commit

Permalink
shouldBeSearchableEvent for studioespresso#205
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens authored and davist11 committed Oct 1, 2021
1 parent dc9054b commit c0e9594
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/behaviors/SearchableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use rias\scout\engines\Engine;
use rias\scout\events\ShouldBeSearchableEvent;
use rias\scout\jobs\MakeSearchable;
use rias\scout\Scout;
use rias\scout\ScoutIndex;
use rias\scout\serializer\AlgoliaSerializer;
use Tightenco\Collect\Support\Arr;
use Tightenco\Collect\Support\Collection;
use yii\base\Behavior;
use yii\base\Event;

/**
* @mixin Element
*
* @property Element $owner
* @property int $id
* @property int $id
*/
class SearchableBehavior extends Behavior
{
const EVENT_SHOULD_BE_SEARCHABLE = "shouldBeSearchableEvent";

public function validatesCriteria(ScoutIndex $scoutIndex): bool
{
$criteria = clone $scoutIndex->criteria;
Expand All @@ -49,12 +53,12 @@ public function getIndices(): Collection
->getIndices()
->filter(function (ScoutIndex $scoutIndex) {
$siteIds = array_map(function ($siteId) {
return (int) $siteId;
return (int)$siteId;
}, Arr::wrap($scoutIndex->criteria->siteId));

return $scoutIndex->elementType === get_class($this->owner)
&& ($scoutIndex->criteria->siteId === '*'
|| in_array((int) $this->owner->siteId, $siteIds));
|| in_array((int)$this->owner->siteId, $siteIds));
});
}

Expand All @@ -79,8 +83,8 @@ public function searchable(bool $propagate = true)
if (Scout::$plugin->getSettings()->queue) {
return Craft::$app->getQueue()->push(
new MakeSearchable([
'id' => $this->owner->id,
'siteId' => $this->owner->siteId,
'id' => $this->owner->id,
'siteId' => $this->owner->siteId,
'indexName' => $engine->scoutIndex->indexName,
'propagate' => $propagate,
])
Expand Down Expand Up @@ -168,6 +172,15 @@ public function shouldBeSearchable(): bool
return false;
}

if (Event::hasHandlers(SearchableBehavior::class, self::EVENT_SHOULD_BE_SEARCHABLE)) {
$event = new ShouldBeSearchableEvent([
'element' => $this->owner,
'shouldBeSearchable' => true
]);
Event::trigger(SearchableBehavior::class, self::EVENT_SHOULD_BE_SEARCHABLE, $event);
return $event->shouldBeSearchable;
}

return true;
}
}
25 changes: 25 additions & 0 deletions src/events/ShouldBeSearchableEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace rias\scout\events;

use craft\base\Element;
use yii\base\Event;

/**
* This event fires as apart of the chain that checks wether or not an element should be marked as searchable.
* By settings `shouldBeSearchable` to false, you can change default behavior for certain elements or element types
*/
class ShouldBeSearchableEvent extends Event
{
/**
* Element that is being saved
* @var Element
*/
public $element;

/**
* Wether or not the element should be marked as searchable
* @var bool
*/
public $shouldBeSearchable = true;
}

0 comments on commit c0e9594

Please sign in to comment.