From 99a34c517e4914ded15a5fc213a57c498d002204 Mon Sep 17 00:00:00 2001 From: Theofilos Intzoglou Date: Tue, 30 Dec 2025 22:43:12 +0200 Subject: [PATCH 1/4] Fix order of arguments for onContentBeforeSave event Fixes #171 --- .../admin/src/Controller/AttachmentController.php | 8 ++++---- .../site/src/Controller/DisplayController.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/attachments_component/admin/src/Controller/AttachmentController.php b/attachments_component/admin/src/Controller/AttachmentController.php index b0076a25..3e7a8250 100644 --- a/attachments_component/admin/src/Controller/AttachmentController.php +++ b/attachments_component/admin/src/Controller/AttachmentController.php @@ -375,8 +375,8 @@ public function saveNew() $app->triggerEvent('onContentBeforeSave', [ 'com_attachments.attachment', $attachment, - null, - true + true, + null ]); // Upload new file/url and create the attachment @@ -783,8 +783,8 @@ public function save($key = null, $urlVar = null) $app->triggerEvent('onContentBeforeSave', [ 'com_attachments.attachment', $attachment, - null, - false + false, + null ]); $apm = AttachmentsPluginManager::getAttachmentsPluginManager(); diff --git a/attachments_component/site/src/Controller/DisplayController.php b/attachments_component/site/src/Controller/DisplayController.php index 55333f0b..f5f9d53c 100644 --- a/attachments_component/site/src/Controller/DisplayController.php +++ b/attachments_component/site/src/Controller/DisplayController.php @@ -377,15 +377,15 @@ public function save() $app->triggerEvent('onContentBeforeSave', [ 'com_attachments.attachment', $attachment, - null, - true + true, + null ]); } else { $app->triggerEvent('onContentBeforeSave', [ 'com_attachments.attachment', $attachment, - null, - false + false, + null ]); } From 8b94145525eb0bf8d9fefb279f59c4a918e51424 Mon Sep 17 00:00:00 2001 From: Theofilos Intzoglou Date: Fri, 2 Jan 2026 14:35:20 +0200 Subject: [PATCH 2/4] More event argument fixes --- .../src/Controller/AttachmentController.php | 12 ++++++------ .../src/Controller/AttachmentsController.php | 4 ++-- .../site/src/Controller/DisplayController.php | 16 +++++++--------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/attachments_component/admin/src/Controller/AttachmentController.php b/attachments_component/admin/src/Controller/AttachmentController.php index 3e7a8250..08b9cc15 100644 --- a/attachments_component/admin/src/Controller/AttachmentController.php +++ b/attachments_component/admin/src/Controller/AttachmentController.php @@ -376,7 +376,7 @@ public function saveNew() 'com_attachments.attachment', $attachment, true, - null + $attachment->getProperties() ]); // Upload new file/url and create the attachment @@ -429,8 +429,8 @@ public function saveNew() $app->triggerEvent('onContentAfterSave', [ 'com_attachments.attachment', $attachment, - null, - true + true, + $attachment->getProperties() ]); // See where to go to next @@ -784,7 +784,7 @@ public function save($key = null, $urlVar = null) 'com_attachments.attachment', $attachment, false, - null + $attachment->getProperties() ]); $apm = AttachmentsPluginManager::getAttachmentsPluginManager(); @@ -1007,8 +1007,8 @@ public function save($key = null, $urlVar = null) $app->triggerEvent('onContentAfterSave', [ 'com_attachments.attachment', $attachment, - null, - false + false, + $attachment->getProperties() ]); switch ($this->getTask()) { diff --git a/attachments_component/admin/src/Controller/AttachmentsController.php b/attachments_component/admin/src/Controller/AttachmentsController.php index 15e67768..99f0fb56 100644 --- a/attachments_component/admin/src/Controller/AttachmentsController.php +++ b/attachments_component/admin/src/Controller/AttachmentsController.php @@ -202,8 +202,8 @@ public function delete() Factory::getApplication()->triggerEvent('onContentAfterDelete', [ 'com_attachments.attachment', $attachment, - null, - false + false, + $attachment->getProperties() ]); } else { $parent_entity = $parent->getCanonicalEntityId($parent_entity); diff --git a/attachments_component/site/src/Controller/DisplayController.php b/attachments_component/site/src/Controller/DisplayController.php index f5f9d53c..c7b6bca8 100644 --- a/attachments_component/site/src/Controller/DisplayController.php +++ b/attachments_component/site/src/Controller/DisplayController.php @@ -378,14 +378,14 @@ public function save() 'com_attachments.attachment', $attachment, true, - null + $attachment->getProperties() ]); } else { $app->triggerEvent('onContentBeforeSave', [ 'com_attachments.attachment', $attachment, false, - null + $attachment->getProperties() ]); } @@ -440,15 +440,15 @@ public function save() $app->triggerEvent('onContentAfterSave', [ 'com_attachments.attachment', $attachment, - null, - true + true, + $attachment->getProperties() ]); } else { $app->triggerEvent('onContentAfterSave', [ 'com_attachments.attachment', $attachment, - null, - false + false, + $attachment->getProperties() ]); } @@ -581,9 +581,7 @@ public function delete() PluginHelper::importPlugin('content'); Factory::getApplication()->triggerEvent('onContentAfterDelete', [ 'com_attachments.attachment', - $attachment, - null, - false + $attachment ]); // Clean up after ourselves From d9a28de87a261f3d15c72ff0e8b8e5c2b36397b4 Mon Sep 17 00:00:00 2001 From: Theofilos Intzoglou Date: Fri, 2 Jan 2026 17:18:23 +0200 Subject: [PATCH 3/4] onContentBeforeSave should be triggered after updating the model values and just before saving the values to the database --- .../src/Controller/AttachmentController.php | 26 ++++++++++--------- .../site/src/Controller/DisplayController.php | 21 +++++---------- .../site/src/Helper/AttachmentsHelper.php | 14 ++++++++++ 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/attachments_component/admin/src/Controller/AttachmentController.php b/attachments_component/admin/src/Controller/AttachmentController.php index 08b9cc15..94b8ec3d 100644 --- a/attachments_component/admin/src/Controller/AttachmentController.php +++ b/attachments_component/admin/src/Controller/AttachmentController.php @@ -372,12 +372,6 @@ public function saveNew() $attachment->modified_by = $user->get('id'); PluginHelper::importPlugin('content'); - $app->triggerEvent('onContentBeforeSave', [ - 'com_attachments.attachment', - $attachment, - true, - $attachment->getProperties() - ]); // Upload new file/url and create the attachment $msg = ''; @@ -418,6 +412,13 @@ public function saveNew() // Set up the parent entity to save $attachment->parent_entity = $parent_entity; + $app->triggerEvent('onContentBeforeSave', [ + 'com_attachments.attachment', + $attachment, + true, + $attachment->getProperties() + ]); + // Save the updated attachment info if (!$attachment->store()) { $errmsg = $attachment->getError() . ' (ERR 131)'; @@ -780,12 +781,6 @@ public function save($key = null, $urlVar = null) // Get the parent handler for this attachment PluginHelper::importPlugin('attachments'); PluginHelper::importPlugin('content'); - $app->triggerEvent('onContentBeforeSave', [ - 'com_attachments.attachment', - $attachment, - false, - $attachment->getProperties() - ]); $apm = AttachmentsPluginManager::getAttachmentsPluginManager(); if (!$apm->attachmentsPluginInstalled($attachment->parent_type)) { @@ -997,6 +992,13 @@ public function save($key = null, $urlVar = null) unset($attachment->parent_entity_name); } + $app->triggerEvent('onContentBeforeSave', [ + 'com_attachments.attachment', + $attachment, + false, + $attachment->getProperties() + ]); + // Save the updated attachment info if (!$attachment->store()) { $errmsg = $attachment->getError() . ' (ERR 142)'; diff --git a/attachments_component/site/src/Controller/DisplayController.php b/attachments_component/site/src/Controller/DisplayController.php index c7b6bca8..42b6ac4a 100644 --- a/attachments_component/site/src/Controller/DisplayController.php +++ b/attachments_component/site/src/Controller/DisplayController.php @@ -373,20 +373,6 @@ public function save() if ($save_type == 'upload') { $attachment->created_by = $user->get('id'); $attachment->parent_id = $parent_id; - - $app->triggerEvent('onContentBeforeSave', [ - 'com_attachments.attachment', - $attachment, - true, - $attachment->getProperties() - ]); - } else { - $app->triggerEvent('onContentBeforeSave', [ - 'com_attachments.attachment', - $attachment, - false, - $attachment->getProperties() - ]); } // Update the modified info @@ -424,6 +410,13 @@ public function save() ); // NOTE: store() is not needed if addUrl() is called since it does it } else { + $app->triggerEvent('onContentBeforeSave', [ + 'com_attachments.attachment', + $attachment, + $save_type == 'upload' ? true : false, + $attachment->getProperties() + ]); + // Save the updated attachment info if (!$attachment->store()) { $errmsg = $attachment->getError() . ' (ERR 11)'; diff --git a/attachments_component/site/src/Helper/AttachmentsHelper.php b/attachments_component/site/src/Helper/AttachmentsHelper.php index 1ebcd482..af29e2c5 100644 --- a/attachments_component/site/src/Helper/AttachmentsHelper.php +++ b/attachments_component/site/src/Helper/AttachmentsHelper.php @@ -928,6 +928,13 @@ public static function uploadFile(&$attachment, &$parent, $attachment_id = false // Add the icon file type $attachment->icon_filename = AttachmentsFileTypes::iconFilename($filename, $ftype); + $app->triggerEvent('onContentBeforeSave', [ + 'com_attachments.attachment', + $attachment, + $attachment_id === false ? true : false, + $attachment->getProperties() + ]); + // Save the updated attachment if (!$attachment->store()) { $errmsg = Text::_('ATTACH_ERROR_SAVING_FILE_ATTACHMENT_RECORD') . $attachment->getError() . ' (ERR 37)'; @@ -1528,6 +1535,13 @@ public static function addUrl( throw new \Exception($errmsg, 500); } + $app->triggerEvent('onContentBeforeSave', [ + 'com_attachments.attachment', + $attachment, + $attachment_id === false ? true : false, + $attachment->getProperties() + ]); + // Save the updated attachment if (!$attachment->store()) { $errmsg = Text::_('ATTACH_ERROR_SAVING_URL_ATTACHMENT_RECORD') . $attachment->getError() . ' (ERR 40)'; From d4a35112b1c4541179e3611fa5132cf47daeedfa Mon Sep 17 00:00:00 2001 From: Theofilos Intzoglou Date: Sat, 3 Jan 2026 13:59:32 +0200 Subject: [PATCH 4/4] Ignore component's own events --- attachments_plugin/src/Extension/Attachments.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/attachments_plugin/src/Extension/Attachments.php b/attachments_plugin/src/Extension/Attachments.php index 46f4c97e..664c1d6d 100644 --- a/attachments_plugin/src/Extension/Attachments.php +++ b/attachments_plugin/src/Extension/Attachments.php @@ -351,6 +351,11 @@ public function onContentAfterSave(Event $event) $isNew = $event->getArgument('isNew'); } + if ($context == 'com_attachments.attachment') { + // Do not do anything for this component's own items + return false; + } + if (!$isNew) { // If the item is not new, this step is not needed return true;