Skip to content

Commit

Permalink
Use ShowInSearch rather than getField()
Browse files Browse the repository at this point in the history
getField(), unlike hasField(), does not check for the corresponding
getter method. This is because getField is itself meant to be called
from getter methods. In other words, hasField and getField are not
exactly parallel, by design.

This causes any DataObject whose ShowInSearch exists only as a getter to
be unconditionally deleted from the index whenever it is saved.

This fix works by replacing the getField call with a reference to
ShowInSearch, which will automatically call getShowInSearch.
  • Loading branch information
Fred Condo committed Mar 1, 2023
1 parent 5a8403b commit 0df5e70
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public function reIndex($stage = Versioned::LIVE)
return;
}

if (!$versionToIndex->hasField('ShowInSearch') || $versionToIndex->getField('ShowInSearch')) {
if (!$versionToIndex->hasField('ShowInSearch') || $versionToIndex->ShowInSearch) {
$this->service->index($versionToIndex);
} else {
$this->service->remove($versionToIndex);
Expand Down Expand Up @@ -505,7 +505,7 @@ protected function updateDependentClasses()

foreach ($list as $object) {
if ($object instanceof DataObject && $object->hasExtension(Searchable::class)) {
if (!$object->hasField('ShowInSearch') || $object->getField('ShowInSearch')) {
if (!$object->hasField('ShowInSearch') || $object->ShowInSearch) {
$this->service->index($object);
} else {
$this->service->remove($object);
Expand Down

0 comments on commit 0df5e70

Please sign in to comment.