1

Currently I am working on a civicrm-wordpress plugin. I have two custom fields displayed for a contact: - Date of consent - Age at recruit

When the user enters the date of consent I would like to calculate the Age at recruit (using the contact's date of birth and the date of consent field). How can I target a custom field and update it with a value when the user clicks on Save?

What I have done so far: - Created and enabled a plugin with the following content:

add_action('civicrm_preProcess', 'set_ageatrecruitment', 92,2);
function set_ageatrecruitment($formName, &$form){

        global $recruitmentDate;
        global $contactData;
        global $ageAtRecruit;
        $contact_id=$_GET['cid'];
        $contactDOB = $contactData[values][$contact_id][birth_date];

            if($recruitmentDate !='' && $contactDOB !='' ) {
                $ageAtRecruitment = getAgeAtRecruitment($contactDOB,$recruitmentDate);

            if ($ageAtRecruit != $ageAtRecruitment){
                updateCustomField($contact_id, "OC-Integrator", "Age at Recruitment", $ageAtRecruitment);}
            }

}

//updates the field, returns number of fields updated
function updateCustomField($uid,$group_name,$field_name,$value, $overwriteblank=FALSE){

    //Don't overwrite existing data if our data value is empty/blank
    //We consider anything that is just whitespace to be "blank"
    if ( ( !isset($value) || strlen(trim($value))==0 ) && !$overwriteblank) return 0;

    $results=civicrm_api("CustomValue","create", array (version => '3','sequential' =>'1', 'entity_id' =>$uid, "custom_" .$group_name . ":" . $field_name => $value));

    return 1;

}

There is another hook called before this which will give value to the global variables.

1

Here's how I would go about this:

  1. Configure a field on the contact. Select "set via PHP" or whatever the option's exact name is.
  2. Generate an extension using Civix. Remember portability - your extension can generate the field in (1), meaning that if you decided to share the extension or use it on multiple projects that.
  3. Use the hook as Ramesh said to fill the values in the field.

It sounds like a neat extension. If you want to commission CiviFirst for this I'd be more than happy to help!

JohnFF

  • 1
    How is this CiviFirst commission works exactly? :) – Csaba Aug 21 '15 at 13:49
  • Dear Csaba, please email me at john@civifirst.com for details! – JohnFF Aug 22 '15 at 20:38
0

you need to create an Extension and use hook_civicrm_custom http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_custom

Please let me know if you need more help -

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.