Skip to content

Commit

Permalink
Delete site page fulltext
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsafley committed Dec 9, 2020
1 parent 38de23b commit 9f8c338
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion application/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public function attachListeners(SharedEventManagerInterface $sharedEventManager)
$sharedEventManager->attach(
'*',
'api.delete.pre',
[$this, 'deleteFulltextPre']
);

$sharedEventManager->attach(
'*',
'api.delete.post',
[$this, 'deleteFulltext']
);

Expand Down Expand Up @@ -518,17 +524,42 @@ public function saveFulltext(ZendEvent $event)
);
}

/**
* Prepare to delete the fulltext of an API resource.
*
* @param ZendEvent $event
*/
public function deleteFulltextPre(ZendEvent $event)
{
$request = $event->getParam('request');
if ('site_pages' === $request->getResource()) {
// The site_pages resource uses a compound ID that cannot be read
// from the database. Here we set the actual entity ID to a request
// option so self::deleteFulltext() can handle it correctly.
$em = $this->getServiceLocator()->get('Omeka\EntityManager');
$sitePage = $em->getRepository('Omeka\Entity\SitePage')->findOneBy($request->getId());
$request->setOption('deleted_entity_id', $sitePage->getId());
}
}

/**
* Delete the fulltext of an API resource.
*
* Typically this will delete on the resource ID that's set on the request
* object. Resources that do not have conventional IDs should set the actual
* ID to the "deleted_entity_id" request option prior to the api.delete.post
* event. If the option exists, this function will use it to delete the
* fulltext.
*
* @param ZendEvent $event
*/
public function deleteFulltext(ZendEvent $event)
{
$fulltext = $this->getServiceLocator()->get('Omeka\FulltextSearch');
$request = $event->getParam('request');
$fulltext->delete(
// Note that the resource may not have an ID after being deleted.
$event->getParam('request')->getId(),
$request->getOption('deleted_entity_id') ?? $request->getId(),
$event->getTarget()
);
}
Expand Down

0 comments on commit 9f8c338

Please sign in to comment.