Skip to content

Commit

Permalink
[5.1] Delete schemaorg item after deleting an item (#43839)
Browse files Browse the repository at this point in the history
  • Loading branch information
chmst authored Jul 25, 2024
1 parent dabf3fe commit 6d7d276
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions plugins/system/schemaorg/src/Extension/Schemaorg.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static function getSubscribedEvents(): array
'onContentPrepareData' => 'onContentPrepareData',
'onContentPrepareForm' => 'onContentPrepareForm',
'onContentAfterSave' => 'onContentAfterSave',
'onContentAfterDelete' => 'onContentAfterDelete',
];
}

Expand Down Expand Up @@ -204,16 +205,7 @@ public function onContentAfterSave(Model\AfterSaveEvent $event)
$itemId = (int) $table->id;

if (empty($data['schema']) || empty($data['schema']['schemaType']) || $data['schema']['schemaType'] === 'None') {
$query = $db->getQuery(true);

$query->delete($db->quoteName('#__schemaorg'))
->where($db->quoteName('itemId') . '= :itemId')
->bind(':itemId', $itemId, ParameterType::INTEGER)
->where($db->quoteName('context') . '= :context')
->bind(':context', $context, ParameterType::STRING);

$db->setQuery($query)->execute();

$this->deleteSchemaOrg($itemId, $context);
return;
}

Expand Down Expand Up @@ -537,4 +529,45 @@ protected function isSupported($context)

return false;
}

/**
* The delete event.
*
* @param Object $event The event
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onContentAfterDelete(Model\AfterDeleteEvent $event)
{
$context = $event->getContext();
$itemId = $event->getItem()->id;

$this->deleteSchemaOrg($itemId, $context);
}

/**
* Delete SchemaOrg record from Database.
*
* @param Integer $itemId
* @param String $context
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function deleteSchemaOrg($itemId, $context)
{
$db = $this->getDatabase();
$query = $db->getQuery(true);

$query->delete($db->quoteName('#__schemaorg'))
->where($db->quoteName('itemId') . '= :itemId')
->where($db->quoteName('context') . '= :context')
->bind(':itemId', $itemId, ParameterType::INTEGER)
->bind(':context', $context, ParameterType::STRING);

$db->setQuery($query)->execute();
}
}

0 comments on commit 6d7d276

Please sign in to comment.