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) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • Custom Fields via buildform hook issue.
Pages: [1]

Author Topic: Custom Fields via buildform hook issue.  (Read 2019 times)

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Custom Fields via buildform hook issue.
October 12, 2011, 02:29:37 pm
I am trying to create a module that adds some membership/pricing functionality to the Event Registration screens.

I have managed to get the fields to display, but for some reason, I am getting duplicate sets of fields, one above the form, as was expected, and another at the end of the form.

Here's a screenshot:
http://dl.dropbox.com/u/3290014/duplicates.jpg

And here is the code:
//////////////////////////////////////////////////////////////////////////////////////////////////////
function monkey_civicrm_buildForm($fname, &$form) {

  // Display discount textfield for offline membership/events
  $display_forms = array('CRM_Event_Form_Participant');
 
  if (in_array($fname, $display_forms)) {
      _add_monkey_textfield($form);
      }
}


/**
 * Add the monkey textfield to a form
 */
function _add_monkey_textfield(&$form) {
  $form->addElement('text', 'memberorg', ts('This field should light up with your company name.'));
  $form->addElement('text', 'membertype', ts('This field should light up with your member type.'));
  $form->addElement('text', 'eventid', ts('This field shows the event id.'));
  $form->addElement('text', 'membercount', ts('This field shows how many registrations are used.'));
  $template =& CRM_Core_Smarty::singleton();
  $bhfe = $template->get_template_vars('beginHookFormElements');
  if (!$bhfe) { $bhfe = array(); }
  $bhfe[] = 'memberorg';
  $bhfe[] = 'membertype';
  $bhfe[] = 'eventid';
  $bhfe[] = 'membercount';
  $form->assign('beginHookFormElements', $bhfe);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Any ideas?

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Custom Fields via buildform hook issue.
October 12, 2011, 03:01:55 pm
When I view source I only see one instance of the fields "hard-coded" into the page.

When I use firebug and inspect the lower field set, I can see that they are inside a DIV with class  "crm-participant-form-block-customData"

Is there some kind of jquery code doing something I'm not expecting?

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Custom Fields via buildform hook issue.
October 12, 2011, 03:22:55 pm
Hi DerekL,

Not jQuery per se, but JavaScript, yes.

The issue here is that this particular form, besides displaying itself via the http call that you see, also makes its own ajax call (to itself!) with additional parameters, and then includes that output in the display as well.  So the hook is being called twice, and both times the $fname value is 'CRM_Event_Form_Participant'. 

Fortunately, and perhaps obviously, it's not being called exactly the same way both times, so you should be able to test for the differences and act accordingly. 
  • The URL you see is: http://example.com/civicrm/contact/view/participant?reset=1&action=add&cid=[some-numeric-id]&context=participant
  • The other URL is: http://example.com/civicrm/contact/view/participant?snippet=4&type=Participant&subType=null&subName=null&qfKey=[some-alphanumeric-form-key]&cgcount=1
This means that if nothing else, you can test for the presence of $_GET['qfKey']; the first form call, which is the one most people know about and which you're most likely to deal with, should not have that value set.  So your code might work more as expected with the change NOTED here:

Code: [Select]
//////////////////////////////////////////////////////////////////////////////////////////////////////
function monkey_civicrm_buildForm($fname, &$form) {

  // Display discount textfield for offline membership/events

  $display_forms = array('CRM_Event_Form_Participant');
 
  // NOTE the change in this line. 
  if (in_array($fname, $display_forms) && !$_GET['qfKey']) {
      _add_monkey_textfield($form);
      }

}

// [snip]

There's probably a more elegant way to do this, but the main point is to distinguish between those two calls and only add the fields on the correct one.

- Allen
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Custom Fields via buildform hook issue.
October 12, 2011, 05:35:36 pm
Thank you for your help! This ended up solving the double fields issue.

I've just realized that the fields display in the "search contacts" screen, as well as "new individual", "new household", and "new organization"...

It seems like this isn't really working:

Code: [Select]

$display_forms = array('CRM_Event_Form_Participant');


TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Custom Fields via buildform hook issue.
October 12, 2011, 06:13:16 pm
Hi Derek,

Setting $display_forms to an array() is useful if you're then using in_array($fname, $display_forms) to check the value of $fname; are you still doing that?

FWIW, if I were writing that code, I'd write skip the array definition and the in_array() checking, and just use a straight if() comparison to check the form name. That makes it a little easier to change later if you're using the hook for multiple different forms:
Code: [Select]
if ($fname == 'CRM_Event_Form_Participant') {
  // do stuff here.
} elseif ($fname == 'CRM_Other_Form_Name') {
  // do other stuff here.
}

- Allen
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Custom Fields via buildform hook issue.
October 12, 2011, 06:47:28 pm
Hi Allen,

Yes, sorry, I cut off the snippet for brevity. I meant to communicate that the entire compare form name to array was either not working or was otherwise very indiscriminate in displaying the fields on the intended pages.

I will try your suggestion and see if it behaves any better.

Thanks,
-D

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Custom Fields via buildform hook issue.
October 13, 2011, 05:20:22 pm
Yes, all is well and stable now after the tweaks.

Thanks you for your support, I'll surely be coming back for more!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • Custom Fields via buildform hook issue.

This forum was archived on 2017-11-26.