4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Event\Subscriber\Generic
;
15 use Civi\API\Event\PrepareEvent
;
16 use Civi\Api4\Generic\AbstractAction
;
17 use Civi\Api4\Generic\AbstractCreateAction
;
18 use Civi\Api4\Generic\AbstractUpdateAction
;
23 abstract class PreSaveSubscriber
extends AbstractPrepareSubscriber
{
29 public $supportedOperation = 'both';
32 * @param \Civi\API\Event\PrepareEvent $event
34 public function onApiPrepare(PrepareEvent
$event) {
35 $apiRequest = $event->getApiRequest();
37 if ($apiRequest instanceof AbstractAction
&& $this->applies($apiRequest)) {
38 \CRM_Core_Error
::deprecatedWarning("Use of APIv4 'PreSaveSubscriber' is deprecated. '" . get_class($this) . "' should be removed ({$apiRequest->getEntityName()}::{$apiRequest->getActionName()}).");
40 ($apiRequest instanceof AbstractCreateAction
&& $this->supportedOperation
!== 'update') ||
41 ($apiRequest instanceof AbstractUpdateAction
&& $this->supportedOperation
!== 'create')
43 $values = $apiRequest->getValues();
44 $this->modify($values, $apiRequest);
45 $apiRequest->setValues($values);
51 * Modify the item about to be saved
54 * @param \Civi\Api4\Generic\AbstractAction $request
57 abstract protected function modify(&$item, AbstractAction
$request);
60 * Check if this subscriber should be applied to the request
62 * @param \Civi\Api4\Generic\AbstractAction $request
66 abstract protected function applies(AbstractAction
$request);