Lack of hooks to detect when an Attachment is deleted
Overview
At present, when an Attachment is deleted via the CiviCRM UI (e.g. on "Edit Activity" dialogs/screens) only two CiviCRM hooks fire: the civi.dao.preDelete
and civi.dao.postDelete
Symfony events triggered by CRM_Core_BAO_EntityTag::del()
. All other deletions are done via direct SQL queries.
It would be great to:
- Have
hook_civicrm_pre
andhook_civicrm_post
fire - Have some way of retrieving the Attachment data prior to deletion
Reproduction steps
- Set up CiviCRM to log all hooks
- Click the "bin/trash" icon next to an Attachment (as per the screenshot below)
- Inspect logged callbacks
Current behaviour
The following callbacks are the only ones that will be seen (data trimmed for concision)
civi.dao.preDelete
[event] => Civi\Core\DAO\Event\PreDelete Object
(
[object] => CRM_Core_BAO_EntityTag Object
(
[id] =>
[entity_table] => civicrm_file
[entity_id] => 6
[tag_id] =>
)
)
civi.dao.postDelete
[event] => Civi\Core\DAO\Event\PostDelete Object
(
[object] => CRM_Core_BAO_EntityTag Object
(
[id] =>
[entity_table] => civicrm_file
[entity_id] => 6
[tag_id] =>
)
[result] => 0
)
It is possible to use the entity_id
to retrieve the "File" data via the CiviCRM API. However, because of the order of deletions in the deleteEntityFile()
method, it is not possible to retrieve the compound "Attachment" data via the API (because the "Entity File" data has already been deleted) and it is therefore not possible to find out which Entity/Entities the File was attached to.
It should also be noted that when the "Delete All Attachment(s)" checkbox is used, the civi.dao.preDelete
and civi.dao.postDelete
Symfony events fire before hook_civicrm_pre
is fired for the Activity. To me, it would make more sense for hook_civicrm_pre
to fire before the File deletion process is initiated so that Activity.pre
and Activity.post
"wrap" the entire process.
Expected behaviour
I would expect to be able to receive callbacks from hook_civicrm_pre
and hook_civicrm_post
, where $op = "delete"
and $objectName = "File"
along with some more contextual data about the "File" and the "Attachment" being deleted.
Comments
I'm going to open a PR that addresses the issue of retrieving the "Attachment" data via the API when using the Symfony hook, since that's pretty straightforward.
I'd appreciate any guidance from those more familiar with this class on the most sensible place(s) to add hooks during the deletion process - or whether deletion should more properly be done via BAO
objects instead of direct queries.