Skip to content

Commit

Permalink
Additional logic to allow for un-versioned records
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-nz committed Mar 25, 2024
1 parent 6dc75f0 commit 79b5e78
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/DataObject/DataObjectDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,22 @@ public function shouldIndex(): bool

// If an anonymous user can't view it
$isPublic = Member::actAs(null, static function () use ($dataObject) {
// Need to make sure that the version of the DataObject that we access is always the LIVE version
return Versioned::withVersionedMode(static function () use ($dataObject): bool {
Versioned::set_stage(Versioned::LIVE);
if ($dataObject->hasExtension(Versioned::class)) {
// Need to make sure that the version of the DataObject that we access is always the LIVE version
return Versioned::withVersionedMode(static function () use ($dataObject): bool {
Versioned::set_stage(Versioned::LIVE);

$liveDataObject = DataObject::get($dataObject->ClassName)->byID($dataObject->ID);
$liveDataObject = DataObject::get($dataObject->ClassName)->byID($dataObject->ID);

if (!$liveDataObject || !$liveDataObject->exists()) {
return false;
}
if (!$liveDataObject || !$liveDataObject->exists()) {
return false;
}

return $liveDataObject->canView();
});
return $liveDataObject->canView();
});
}

return $dataObject->canView();
});

if (!$isPublic) {
Expand Down
15 changes: 15 additions & 0 deletions src/Extensions/SearchServiceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ public function onAfterPublish(): void
$this->owner->addToIndexes();
}

/**
* When saving an un-versioned DataObject, push this data to Indexer.
* The data which is sent to search is the rendered template from the front end
*
* @throws Exception
*/
public function onAfterWrite(): void
{
if ($this->owner->hasExtension(Versioned::class)) {
return;
}

$this->owner->addToIndexes();
}

/**
* When unpublishing this item, remove from search
*/
Expand Down

0 comments on commit 79b5e78

Please sign in to comment.