Details
-
Type: Bug
-
Status: Open
-
Priority: Minor
-
Resolution: Unresolved
-
Affects Version/s: 4.7.20
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Versioning Impact:Patch (backwards-compatible bug fixes)
-
Documentation Required?:None
-
Funding Source:Contributed Code
-
Verified?:No
Description
The _post hook is designed to be used to pick up on database writes, but as we know it isn't triggered in all circumstances. This leads to inconsistent behavior.
For example, if setting 'is_opt_out' via the API or front-end forms, hook_post is triggered.
However, if a user unsubscribes from a mailing, then the code:
$contact = new CRM_Contact_BAO_Contact();
$contact->id = $q->contact_id;
$contact->is_opt_out = TRUE;
$contact->save();
This does not trigger hook_post; but does trigger hook_postSave, which is called from Core_DAO.
So let's update this code to use the API?
civicrm_api3('contact', 'create', array( 'id' => $q->contact_id, 'is_opt_out' => 1 ));
As a follow up, the same pattern of using CRM_Contact_BAO_Contact() to quickly 'save' an update is infrequently used elsewhere:
\CRM_Contact_BAO_Individual::format
\CRM_Contact_Form_Task_AlterPreferences::postProcess