Repository navigation
Conversation
|
🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷 Introduction for new contributors...
Quick links for reviewers...
|
b99ee8b to
36a0eac
Compare
|
@MegaphoneJon You might be interested in this (it would fix https://lab.civicrm.org/extensions/civirules/-/issues/244) |
|
This seems related to https://lab.civicrm.org/dev/core/-/work_items/5413 which has been sponsored by WMF so I'll be working on it shortly. Will circle back to see how this one relates. |
| // In order to return (and eventually save) using API4 style customFields | ||
| // We need to copy them to the DAO object. | ||
| // @fixme: Need to load spec and parse through foreach above | ||
| $api4CustomFields = array_filter($params, function($key) { | ||
| return str_contains($key, '.'); | ||
| }, ARRAY_FILTER_USE_KEY); | ||
| foreach ($api4CustomFields as $fieldName => $fieldValue) { | ||
| $this->$fieldName = $fieldValue; | ||
| } |
There was a problem hiding this comment.
@mattwire what does this code do? I'm guessing that for Api4 calls it copies the custom fields to the object, but for non-api4 calls it does nothing?
There was a problem hiding this comment.
Yes exactly. It's identifying API4 style custom fields by the "." ie. "CustomGroup.CustomFieldname". and copying them onto the DAO object so they are passed through to the post hook and returned to the user. Because otherwise they get filtered out even if you pass them in.
There was a problem hiding this comment.
Yea ok, but maybe we don't need this if we add getParam/setParam helpers, since this only works for Api4 but the helpers will work regardless of how the hook is called.
There was a problem hiding this comment.
Ok, I'm experimenting by creating a copy of getValues() here and using that. I don't think we actually need getValue()/setValue() for this PR?
|
I would suggest adding methods similar to these to the PreUpdate and PostUpdate objects for making the |
7fe47b7 to
2cae74c
Compare
2cae74c to
3855ed4
Compare
|
@colemanw I've reworked this. What do you think of it now? |
|
|
||
| if (!empty($record['custom']) && is_array($record['custom'])) { | ||
| CRM_Core_BAO_CustomValueTable::store($record['custom'], static::getTableName(), $instance->$idField, $op); | ||
| $instance->_customParams = $record['custom']; |
There was a problem hiding this comment.
This is a very fine nuance of PHP. You're accessing a private instance variable from a static method. Normally that's not allowed, but PHP allows it here because the method is the same class as the instance. So in PHP, private seems to mean "scoped to only this class" not "only this instance".
The PHP docs are not super clear on this point, but it works and tests pass :)
There was a problem hiding this comment.
Yeah.. I'm not totally liking the private class variable but it does seem the simplest way of doing what we need.
|
Minor: tests now avoid hard-coded location type ids (seeded ids are offset in tests). Please use |
3855ed4 to
095b196
Compare
Thanks @aydun - switched to 'location_type_id:name' => 'Home'. |
|
/rebase |
writeRecord() stored custom data after save(), so listeners on civi.dao.postInsert, civi.dao.postUpdate and hook_civicrm_postSave_<table> read missing or stale custom values. save() now stores the custom data immediately after the insert/update, before those events are dispatched.
095b196 to
50d4d91
Compare
|
Closing in favour of #37943 |
|
@mattwire I think you pushed to this branch and it reopened the PR. Closing again. |
|
I might have just forgotten to actually click close! |
Overview
civi.dao.postInsert/civi.dao.postUpdate(andhook_civicrm_postSave_<table>) fire beforewriteRecord()saves custom data, so listeners see missing or stale custom values. See https://lab.civicrm.org/extensions/civirules/-/issues/244.Before
On create, a listener reading the custom table finds nothing; on update it finds the previous value.
After
Custom data is stored immediately after the insert/update, before those events are dispatched.
Technical Details
writeRecord()hands$record['custom']to the instance;save()stores it afterinsert()/update()and clears it.hook_civicrm_customnow fires before the DAO post events rather than after.writeRecord(); BAOs with their owncreate()(e.g. Contact, Contribution) are unchanged.CRM_Core_DAOTest::testWriteRecordSavesCustomDataBeforePostEvents.Comments
Rewritten from the earlier proof of concept: the
copyValues()change is dropped. Exposing custom values in API4 form on the DAO events (viaEntityParamsTrait, per #35492) can follow separately - related https://lab.civicrm.org/dev/core/-/work_items/5413