CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Hide fields on contribution page based on business logic
Pages: [1]

Author Topic: Hide fields on contribution page based on business logic  (Read 534 times)

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Hide fields on contribution page based on business logic
October 07, 2011, 08:56:35 am
I'm looking for a way to hide a custom field exposed via a profile on a contribution page in the condition there's already a value for the current user. I'm using the buildForm hook to attempt to do this. Here's the code I've written so far:

Code: [Select]
function testmodule_civicrm_buildForm( $formName, &$form ) {

        global $user;

if ($user->uid) { // Authenticated User
$contact_id = (int)civicrm_api('uf_match','getvalue',array('version' => 3, 'uf_id' => $user->uid, 'return' => 'contact_id'));
} else {
return; // Don't need to do anything for anonymous
}

if ( $formName == 'CRM_Contribute_Form_Contribution_Main' ) {
           
            require_once 'CRM/Core/BAO/CustomField.php';
            $referredByFieldID = CRM_Core_BAO_CustomField::getCustomFieldID('Referred By', 'Marketing');
    $referredByFieldName = 'custom_'.$referredByFieldID;

            if (array_key_exists($referredByFieldID, $results['values'])) {

$referredByValue = $results['values'][$referredByFieldID][0];

if( (isset($referredByValue)) && (!empty($referredByValue)) ) { // if contact has already specified this, don't ask again!

$form->removeElement($referredByFieldName, true);
unset($form->_fields[$referredByFieldName]);

}
   }
      }
}

It does actually remove the field from the contribution page. However, the field's post help still displays, so I'm wondering if there's a way to remove that as well AND/OR if I'm doing this in the best way.

demeritcowboy

  • Ask me questions
  • ****
  • Posts: 570
  • Karma: 42
  • CiviCRM version: Always the latest!
  • CMS version: Drupal 6 mostly, still evaluating 7.
  • MySQL version: Mix of 5.0 / 5.1 / 5.5
  • PHP version: 5.3, usually on Windows
Re: Hide fields on contribution page based on business logic
October 07, 2011, 10:51:00 am
Taking a step back for a moment I see this as a more general question: How do I have different questions for new contributors vs repeat contributors?

There's a separate profile for registration when the visitor is first creating an account, so I would put your "referred by" question on the registration profile, and then for the profile you are using for your contribution page don't include that question.

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Re: Hide fields on contribution page based on business logic
October 07, 2011, 11:00:58 am
That's definitely a valid alternative approach and I do appreciate the suggestion. However, there are two complications:

1) We don't want to force an extra step of creating and validating an account before going to the contribution page to become a member.
2) We don't need to gather a "referred by" value from people who are simply creating an account. We only need to gather it when they become a member.

I could also create a separate contribution page for becoming a member VS. renewing a membership as another valid alternative approach. But ultimately I'm hoping to learn how to effectively show/hide fields from Civi contribution pages/registration pages/profiles through hooks because I can think of other instances for doing so.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Hide fields on contribution page based on business logic

This forum was archived on 2017-11-26.