write back changes in Drupal pre hooks to CiviCRM before commit/post
Problem
CiviCRM Entity forwards hook_civicrm_pre and hook_civicrm_post to Drupal's entity hooks. When CiviCRM makes a write
itself (API3, API4, CiviCRM's own forms), whatever a hook_entity_presave implementation changes is lost:
- Create:
onPrebuilds an entity from the params and runs presave on it. That entity is then thrown away, andonPostloads a fresh copy. Drupal field values set in presave are never saved, and changes to CiviCRM fields never reach CiviCRM. - Update:
onPreloads the stored entity without the new params, and its original holds the same data, so presave sees no change.onPostsets the original after the write, so update hooks also see original == entity.
For example, AI Automators fills fields in presave. On an API4 activity create it calls the AI provider and the results are lost; on an API4 update it doesn't run at all.
Solution
CiviCRM passes its params to the pre hook by reference, reads them again after the hook (copyValues(),
writeRecord(), which fills defaults after the hook "to accept any hook modifications"), and passes the same params to
the post hook. This MR relies on that:
- Updates see the values being written. The update's params are applied to the stored entity before presave.
They're converted the way a create's params already are (through
initFieldValues()), and only the fields the params name change. Custom field values come from$params['custom'](API3/API4) orcustom_N(forms). The original stays the stored entity. - Presave changes are written back.
civicrmApiNormalize()runs before and after presave, and changed values go into the params, so CiviCRM saves them in the same write:- core fields as
$params[$name]; - single-record custom fields (except File) in both
$params['custom'](viaCRM_Core_BAO_CustomField::formatCustomField()) andcustom_N. API3 and API4 buildcustombefore the pre hook, while the contact form builds it fromcustom_Nafter.
- core fields as
- Pre and post are paired by a token, not by record fields. The pre hook adds
civicrm_entity_write_tokento the params and keeps the presaved entity under it (static, per entity type). Updates are also indexed by ID, for post hooks that come without params. A write that fails after its pre hook leaves an entry no post hook claims; at most 100 are kept per type, oldest dropped first. - The post hook finishes the save. New
CiviEntityStorage::claimCiviWrite()takes the kept entity, sets the ID on create, copies back what CiviCRM stored (refreshFromCivicrm()), saves the Drupal fields (doSaveFieldItems()), and passes the entity todoPostSave()with the pre-write original. If nothing was kept, the current behavior (a fresh load) applies. The held-create path (an entity CiviCRM creates inside Drupal's create) uses the kept entity too. - CiviCRM's
YmdHisdates (14 digits, as API3 and thecustomparam give them) are read as dates ininitFieldValues(); they used to be taken as a Unix timestamp.
Unchanged: Drupal's own writes, deletes, restores, EntityTag and GroupContact (their params are lists), and any params with integer keys.
API changes
CiviEntityStorage::civiPreSave()takes an optional?array &$params. Callers that don't pass it (CustomRecordHookSubscriber) behave as before.CiviEntityStorage::holdCreatedId()takes optional$params.- New public
CiviEntityStorage::claimCiviWrite()and constantCiviEntityStorage::WRITE_TOKEN. refreshFromCivicrm()(private) now returns whether the record was found.- Other
hook_civicrm_pre/postlisteners see the token in the params. It doesn't appear in API3 or API4 results: API4 hands each record to the BAO by value, and API3 builds its result from the DAO.
Known limitations
- CiviCRM's contact edit form fires its post hook without params (
CRM/Contact/Form/Contact.php,postProcess()). Updates pair by ID instead. For contacts created through that form, CiviCRM fields set in presave are saved, but Updates pair by ID instead. For contacts created through that form, CiviCRM fields set in presave are saved, but Drupal-only fields are not (same as before). Passing$paramsto that post hook in CiviCRM core would close the gap. - Drupal fields are written in the post hook. If an outer CiviCRM transaction later rolls back, their rows remain for a record that no longer exists.
Testing
- New kernel test
CiviCrmWriteBackTest(real CiviCRM) with test modulecivicrm_entity_write_back_test. Its presave hook sets a Drupal field, a CiviCRM base field and a custom field from the title. Covers:- API4 create and update in
api4_nativeandapi3modes: values saved, update hook sees the old title as original, presave sees the new start date as a load reads it, no token in the API result; - API3 create and update, whose dates come as
YmdHis; - an event CiviCRM creates inside Drupal's create (held path);
- a nested create CiviCRM rolls back: no insert hook and no Drupal field rows.
- API4 create and update in
DrupalWriteHooksTest: RESULT PENDING (11/11 before this change).- AI Automators on CiviCRM Meeting activities (four Decision automators): with every AI call blocked and a marker written instead, an API4 create now saves all four values (before: none), and an API4 update runs the three automators whose source changed (before: none). A live run is still to do.