Skip to content

Commit

Permalink
Check if criteria is an array #297
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed May 4, 2024
1 parent af5b4df commit 529f701
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/jobs/MakeSearchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace rias\scout\jobs;

use craft\base\Element;
use craft\elements\db\ElementQuery;
use craft\queue\BaseJob;
use rias\scout\Scout;
use rias\scout\ScoutIndex;
Expand Down Expand Up @@ -35,9 +36,17 @@ public function execute($queue): void
} else {
// Element not found, checking if it was disabled and needs to be de-indexed.
$element = $this->getAnyElement();
if($element){
if(!$element->shouldBeSearchable()) {
$element->unsearchable();
if ($element) {
if (is_array($element)) {
collect($element)->each(function ($element) {
if (!$element->shouldBeSearchable()) {
$element->unsearchable();
}
});
} else {
if (!$element->shouldBeSearchable()) {
$element->unsearchable();
}
}
}
}
Expand All @@ -51,6 +60,11 @@ protected function defaultDescription(): string
return '';
}

if(is_array($element)) {
$element = end($element);
}
\Craft::error(json_encode($element, JSON_PRETTY_PRINT));

return sprintf(
'Indexing “%s” in “%s”',
($element->title ?? $element->id),
Expand All @@ -67,6 +81,14 @@ protected function defaultDescription(): string
*/
private function getElement()
{

if(is_array($this->getIndex()->criteria)) {
$element = collect($this->getIndex()->criteria)->first(function (ElementQuery $criteria) {
return $criteria->id($this->id)->siteId($this->siteId)->exists();
});
return $element->one();
}

return $this->getIndex()
->criteria
->id($this->id)
Expand All @@ -76,6 +98,14 @@ private function getElement()

private function getAnyElement()
{
if (is_array($this->getIndex()->criteria)) {
$element = collect($this->getIndex()->criteria)->first(function (ElementQuery $criteria) {
return $criteria->id($this->id)->siteId($this->siteId)->status(null)->exists();
});
return $element->one();

}

return $this->getIndex()
->criteria
->id($this->id)
Expand Down

0 comments on commit 529f701

Please sign in to comment.