Skip to content

Commit

Permalink
fix: page indexing after delete hook
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Nov 16, 2023
1 parent 320cef3 commit 9f1395c
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions extensions/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,22 @@
$index = $algolia->getAlgoliaIndex();
$index->deleteObject($page->uri($languageCode));
},
'page.*:after' => function (\Kirby\Cms\Event $event, \Kirby\Cms\Page $page) {
'page.delete:after' => function (bool $status, \Kirby\Cms\Page $page) {
/** @var \Kirby\Cms\App $kirby */
$kirby = $this;

if (
$kirby->option('johannschopplich.algolia-docsearch.hooks', false) !== true ||
$kirby->option('debug')
) {
return;
}

$algolia = DocSearch::instance();
$index = $algolia->getAlgoliaIndex();
$index->deleteObject($page->uri($kirby->languageCode()));
},
'page.*:after' => function (\Kirby\Cms\Event $event, \Kirby\Cms\Page $newPage) {
/** @var \Kirby\Cms\App $kirby */
$kirby = $this;

Expand All @@ -30,24 +45,24 @@
}

// Check if we want to handle the action
if (!in_array($event->action(), ['changeSlug', 'changeStatus', 'changeTitle', 'delete', 'update'], true)) {
if (!in_array($event->action(), ['changeSlug', 'changeStatus', 'changeTitle', 'update'], true)) {
return;
}

$languageCode = $page->kirby()->languageCode();
$languageCode = $kirby->languageCode();
$algolia = DocSearch::instance();
$index = $algolia->getAlgoliaIndex();

if (
$event->action() === 'delete' ||
($event->action() === 'changeStatus' && !$page->isListed())
($event->action() === 'changeStatus' && !$newPage->isListed())
) {
$index->deleteObject($page->uri($languageCode));
$index->deleteObject($newPage->uri($languageCode));
return;
}

try {
$algolia->indexPage($page, $languageCode);
$algolia->indexPage($newPage, $languageCode);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
Expand Down

0 comments on commit 9f1395c

Please sign in to comment.