Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can onContentAfterDelete be used to send notifications after content deletion


Can onContentAfterDelete be used to send notifications after content deletion


Yes, the onContentAfterDelete event in Joomla can be used to delete related data from a custom SQL table when an article is deleted. Here is a summary:

Deleting Related Data from a Custom SQL Table

When an article is deleted in Joomla, you can use the onContentAfterDelete event to automatically delete any related data from a custom SQL table. Here's how:

1. Register the Event in the Plugin XML File: In the plugin XML file, register the onContentAfterDelete event under the events section:

xml
<events>
  <event name="onContentAfterDelete" group="content" />
</events>

2. Implement the Event Handler in the Plugin PHP File: In the plugin PHP file, create a function to handle the onContentAfterDelete event:

php
public function onContentAfterDelete($context, $data)
{
  // Check if the event is for article deletion
  if ($context == 'com_content.article')
  {
    // Get the deleted article ID
    $articleId = $data->id;

    // Connect to the database
    $db = JFactory::getDbo();

    // Prepare the SQL query to delete related data
    $query = $db->getQuery(true)
      ->delete($db->quoteName('#__my_custom_table'))
      ->where($db->quoteName('article_id') . ' = ' . $articleId);

    // Execute the query to delete the related data
    $db->setQuery($query)->execute();
  }

  // Return true to allow deletion to proceed
  return true;
}

This code retrieves the deleted article ID, connects to the database, and executes a SQL query to delete the related data from the custom table `#__my_custom_table` based on the article ID.[1]

Sending Notifications After Content Deletion

While the onContentAfterDelete event can be used to delete related data, it does not provide a built-in mechanism for sending notifications. To send notifications after content deletion, you would need to implement additional code in your plugin to handle that functionality.

One approach would be to add code to your onContentAfterDelete event handler to send an email or trigger a notification to the appropriate users or administrators. This could involve using Joomla's built-in email functionality or integrating with a third-party notification service.

However, it's important to note that the primary purpose of the onContentAfterDelete event is to manage related data, not to handle notification logic. If your plugin's main focus is on sending notifications, you may want to consider creating a separate plugin or event handler specifically for that purpose.

Citations:
[1] https://docs.joomla.org/J3.x:Creating_a_content_plugin/en
[2] https://www.thejoomlaexpert.com/joomla-news-tips/item/239-disabling-joomla-update-notification-emails.html
[3] https://www.joomlatools.com/extensions/logman/developer/plugins/joomla-framework
[4] https://forum.joomla.org/viewtopic.php?t=992773
[5] https://joomla.stackexchange.com/questions/23398/delete-row-from-custom-made-sql-when-deleting-article-in-joomla