[REF] Don't add Contacts to Groups they are already Added to on Edit #26519
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
If you edit a Contact (
contact/add?action=update
), on saving, the form callsCRM_Contact_BAO_Contact::create
withparams['groups']
including all the groups the contact was in before editing and wasn't removed from, which callsCRM_Contact_BAO_GroupContact::addContactsToGroup
for each group, which provides the pre and post hooks and callsCRM_Contact_BAO_GroupContact::bulkAddContactsToGroup
which finally checks the table and says, oh wait, that Contact is already in that Group, there's nothing to do here. Rinse and repeat for each Group.Before
Attempt to add Contact to all Groups submitted in the edit form.
After
Only attempt to add Contact to Groups that it was not already added to. On my local, this saves a few tenths of a second with a handful of groups.
This will also stop erroneous GroupContact hook triggers, but that's a more general problem that also needs to be solved in GroupContact.
Technical Details
We don't technically need to check
$params['group'][$key['group_id']] == 1
here, but it seems safer and more readable to do so.