EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
late save/remove hooks
Yuriif6a6aefd5e46
diff --git a/application/Espo/Core/Hook/GeneralInvoker.php b/application/Espo/Core/Hook/GeneralInvoker.phpindex 74daf42421..6f49e2c6a8 100644--- a/application/Espo/Core/Hook/GeneralInvoker.php+++ b/application/Espo/Core/Hook/GeneralInvoker.php@@ -36,6 +36,8 @@ use Espo\Core\Hook\Hook\AfterSave;use Espo\Core\Hook\Hook\AfterUnrelate;use Espo\Core\Hook\Hook\BeforeRemove;use Espo\Core\Hook\Hook\BeforeSave;+use Espo\Core\Hook\Hook\LateAfterRemove;+use Espo\Core\Hook\Hook\LateAfterSave;use Espo\ORM\Entity;use Espo\ORM\Query\Select;use Espo\ORM\Repository\Option\MassRelateOptions;@@ -52,8 +54,10 @@ class GeneralInvoker{private const HOOK_BEFORE_SAVE = 'beforeSave';private const HOOK_AFTER_SAVE = 'afterSave';+ private const HOOK_LATE_AFTER_SAVE = 'lateAfterSave';private const HOOK_BEFORE_REMOVE = 'beforeRemove';private const HOOK_AFTER_REMOVE = 'afterRemove';+ private const HOOK_LATE_AFTER_REMOVE = 'lateAfterRemove';private const HOOK_AFTER_RELATE = 'afterRelate';private const HOOK_AFTER_UNRELATE = 'afterUnrelate';private const HOOK_AFTER_MASS_RELATE = 'afterMassRelate';@@ -93,6 +97,16 @@ class GeneralInvokerreturn;}+ if ($name === self::HOOK_LATE_AFTER_SAVE && $hook instanceof LateAfterSave) {+ if (!$subject instanceof Entity) {+ throw new LogicException();+ }++ $hook->lateAfterSave($subject, SaveOptions::fromAssoc($options));++ return;+ }+if ($name === self::HOOK_BEFORE_REMOVE && $hook instanceof BeforeRemove) {if (!$subject instanceof Entity) {throw new LogicException();@@ -113,6 +127,16 @@ class GeneralInvokerreturn;}+ if ($name === self::HOOK_LATE_AFTER_REMOVE && $hook instanceof LateAfterRemove) {+ if (!$subject instanceof Entity) {+ throw new LogicException();+ }++ $hook->lateAfterRemove($subject, RemoveOptions::fromAssoc($options));++ return;+ }+if ($name === self::HOOK_AFTER_RELATE && $hook instanceof AfterRelate) {$relationName = $hookData['relationName'] ?? null;$relatedEntity = $hookData['foreignEntity'] ?? null;diff --git a/application/Espo/Core/Hook/Hook/LateAfterRemove.php b/application/Espo/Core/Hook/Hook/LateAfterRemove.phpnew file mode 100644index 0000000000..c0636b8298--- /dev/null+++ b/application/Espo/Core/Hook/Hook/LateAfterRemove.php@@ -0,0 +1,51 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Hook\Hook;++use Espo\ORM\Entity;+use Espo\ORM\Repository\Option\RemoveOptions;++/**+ * A lateAfterRemove hook.+ *+ * @template TEntity of Entity = Entity+ *+ * @since 9.4.0+ */+interface LateAfterRemove+{+ /**+ * Processed after an entity is removed, after the transaction (if one is used).+ *+ * @param TEntity $entity An entity.+ * @param RemoveOptions $options Options.+ */+ public function lateAfterRemove(Entity $entity, RemoveOptions $options): void;+}diff --git a/application/Espo/Core/Hook/Hook/LateAfterSave.php b/application/Espo/Core/Hook/Hook/LateAfterSave.phpnew file mode 100644index 0000000000..1cefc987d2--- /dev/null+++ b/application/Espo/Core/Hook/Hook/LateAfterSave.php@@ -0,0 +1,51 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Hook\Hook;++use Espo\ORM\Entity;+use Espo\ORM\Repository\Option\SaveOptions;++/**+ * A lateAfterSave hook.+ *+ * @template TEntity of Entity = Entity+ *+ * @since 9.4.0+ */+interface LateAfterSave+{+ /**+ * Processed after an entity is saved, after the transaction (if one is used).+ *+ * @param TEntity $entity An entity.+ * @param SaveOptions $options Options.+ */+ public function lateAfterSave(Entity $entity, SaveOptions $options): void;+}diff --git a/application/Espo/Core/Repositories/Database.php b/application/Espo/Core/Repositories/Database.phpindex db260236d6..c94f947eb4 100644--- a/application/Espo/Core/Repositories/Database.php+++ b/application/Espo/Core/Repositories/Database.php@@ -140,11 +140,11 @@ class Database extends RDBRepository$this->entityManager->getTransactionManager()->run(function () use ($entity, $options) {$this->saveInternal($entity, $options);});-- return;+ } else {+ $this->saveInternal($entity, $options);}- $this->saveInternal($entity, $options);+ $this->lateAfterSave($entity, $options);}/**@@ -170,6 +170,17 @@ class Database extends RDBRepositoryparent::save($entity, $options);}+ /**+ * @param TEntity $entity+ * @param array<string, mixed> $options+ */+ private function lateAfterSave(Entity $entity, array $options): void+ {+ if (!$this->hooksDisabled && empty($options[SaveOption::SKIP_HOOKS])) {+ $this->hookManager->process($this->entityType, 'lateAfterSave', $entity, $options);+ }+ }+/*** Remove a record (mark as deleted).*/@@ -179,11 +190,22 @@ class Database extends RDBRepository$this->entityManager->getTransactionManager()->run(function () use ($entity, $options) {$this->removeInternal($entity, $options);});-- return;+ } else {+ $this->removeInternal($entity, $options);}- $this->removeInternal($entity, $options);+ $this->lateAfterRemove($entity, $options);+ }++ /**+ * @param TEntity $entity+ * @param array<string, mixed> $options+ */+ private function lateAfterRemove(Entity $entity, array $options): void+ {+ if (!$this->hooksDisabled && empty($options[SaveOption::SKIP_HOOKS])) {+ $this->hookManager->process($this->entityType, 'lateAfterRemove', $entity, $options);+ }}/**