1

On a profile listing page, you can query string to pre-select the field. e.g. add &state_province-Primary=NN where NN is the ID in the civicrm_state_province table.

However, on a profile create page, specifically on a contribution page, this does not work.

How can we make this work?

1

Since you have the jQuery tag, I assume you're open to a jQuery solution.

  • This StackOverflow question covers pulling a variable from a URL argument.
  • Then it's just a matter of identifying the CSS identifier of the field you want to fill (right-click, use "Inspect Element") and referencing it. E.g.:
var state = GetURLParameter('state');
$('#state_province_id-2').val(state);

The Select2 documentation suggests this should work on Select2 widgets as well.

1

Alternative answer is a Civi extension that uses the buildForm hook to prepopulate the field. Here's some example code that does something very similar on a custom field:

function trackingcodes_civicrm_buildForm( $formName, &$form ) {
  if ($formName == 'CRM_Contribute_Form_Contribution_Main' ||
      $formName == 'CRM_Contribute_Form_Contribution_Confirm' ||
      $formName == 'CRM_Contribute_Form_Contribution_ThankYou' ) {
    CRM_Core_Resources::singleton()->addStyleFile('trackingcodes', 'trackingcodes.css');
  }
  if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
    if (isset($_GET['track1'])) {
      $defaults['custom_180'] = $_GET['track1'];
    }
    if (isset($_GET['track2'])) {
      $defaults['custom_181'] = $_GET['track2'];
    }
    if (isset($_GET['track3'])) {
      $defaults['custom_182'] = $_GET['track3'];
    }
    $form->setDefaults($defaults);
  }
}

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.