Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Afform] Do not replace multi record custom data on afform submit #31477

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions ext/afform/core/Civi/Api4/Action/Afform/Submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,23 @@ protected static function saveJoins(AfformSubmitEvent $event, $index, $entityId,
// the contact was being auto-updated via a dedupe rule; in that case we would not want to
// delete any existing records.
elseif ($values) {
$result = civicrm_api4($joinEntityName, 'replace', [
// Disable permission checks because the main entity has already been vetted
'checkPermissions' => FALSE,
'where' => $whereClause,
'records' => $values,
]);
// Based on afform Submit::getCustomGroupBlocks(), we can use this check for "is_multiple"
if (str_contains($joinEntityName, "Custom_")) {
$values[0]["entity_id"] = $entityId;
$result = civicrm_api4($joinEntityName, 'save', [
Comment on lines +488 to +489
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to propagate the $entityId into all records, not just the first one. APIv4 has a shorthand way of doing that:

Suggested change
$values[0]["entity_id"] = $entityId;
$result = civicrm_api4($joinEntityName, 'save', [
$result = civicrm_api4($joinEntityName, 'save', [
'defaults' = ['entity_id' => $entityId],

// Disable permission checks because the main entity has already been vetted
'checkPermissions' => FALSE,
'records' => $values,
]);
}
else {
$result = civicrm_api4($joinEntityName, 'replace', [
// Disable permission checks because the main entity has already been vetted
'checkPermissions' => FALSE,
'where' => $whereClause,
'records' => $values,
]);
}
$indexedResult = array_combine(array_keys($values), (array) $result);
$event->setJoinIds($index, $joinEntityName, $indexedResult);
}
Expand Down