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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Adding a field on a Contribution page with buildForm hook
Pages: [1]

Author Topic: Adding a field on a Contribution page with buildForm hook  (Read 1265 times)

nicolas

  • I post occasionally
  • **
  • Posts: 92
  • Karma: 6
    • cividesk
  • CiviCRM version: 4.4 LTS
  • CMS version: Standalone (yep)
  • MySQL version: 5.1
  • PHP version: 5.3
Adding a field on a Contribution page with buildForm hook
February 28, 2013, 07:21:35 pm
I am trying to add a field on a Contribution page, and then catch the value of this field when submitted to create stuff unrelated to the contribution itself.
I thought using the buildForm hook was the solution, but the field I am creating in the form is not displayed on screen.

function mymodule_civicrm_buildForm($formName, &$form) {
  if (($formName == 'CRM_Contribute_Form_Contribution_Main') && ($form->getVar('_id') == 2)) {
    $form->add('text', 'testfield', ts('Test field'));
  }
}


 Am I missing something or taking the wrong route (ie. vs alterContent hook)?
cividesk -- CiviCRM delivered ... your way!

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Adding a field on a Contribution page with buildForm hook
February 28, 2013, 07:31:13 pm
When you're using the QuickForms system that CiviCRM uses, you need to do two things to make fields appear:

1. You need to create the item in the form, which is what you're doing in hook_civicrm_buildForm().
2. Usually, you need to modify the Smarty template used to display the form, and make sure that the form item is rendered in the template.

It's been a while since I've written this sort of code, but I don't see anything obviously wrong in your buildForm implementation.  So I'd suggest making sure that your 'testfield' actually get rendered in the template.  You may just need to add it, with a bit of HTML to wrap around it.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Adding a field on a Contribution page with buildForm hook
February 28, 2013, 09:07:06 pm
For #2, there are a few techniques for modifying the Smarty template.

One technique is to locate the core .tpl file and edit it (e.g. open "templates/CRM/Contribute/Form/Contribution/Main.tpl" add "{$form.testField.html}" somewhere). Other techniques involve creating a directory of customized Smarty templates or "extra" Smarty templates. These tend to make the most sense if you're writing a patch for the software or if you're a designer trying to rework the layout.

For module development, it may make more sense to use the "Region" API to output code. For example:

Code: [Select]
/// FILE: mymodule/mymodule.module
function mymodule_civicrm_buildForm($formName, &$form) {
  if (($formName == 'CRM_Contribute_Form_Contribution_Main') && ($form->getVar('_id') == 2)) {
    $form->add('text', 'testfield', ts('Test field'));
    CRM_Core_Region::instance('page-body')->add(array(
      'template' => 'testfield.tpl'
     ));
  }
}

/// FILE: mymodule/templates/testfield.tpl
<tr id="testfield-tr">
  <td>Test field:</td>
  <td>{$form.testField.html}</td>
</tr>

This outputs the field somewhere in the body, but it's not too precise. To get fine-grained, you can add some jQuery:

Code: [Select]
/// FILE: mymodule/templates/testfield.tpl
<tr id="testfield-tr">
  <td>Test field:</td>
  <td>{$form.testField.html}</td>
</tr>
<script type="text/javascript">
cj('#testfield-tr').appendTo('#someOtherElement')
</script>

For more details on using regions and on moving elements with jQuery, see:
 * http://wiki.civicrm.org/confluence/display/CRMDOC42/Region+Reference
 * http://stackoverflow.com/questions/4428312/jquery-move-an-element

Note: If you're writing a Drupal module or Joomla plugin, then the module needs some extra boilerplate. If this is a native CiviCRM module (generated by civix), then you don't have to deal with it. The boilerplate is:

Code: [Select]
/// FILE: mymodule/mymodule.module
function mymodule_civicrm_config() {
  $template =& CRM_Core_Smarty::singleton();

  $extDir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'templates';

  if ( is_array( $template->template_dir ) ) {
      array_unshift( $template->template_dir, $extDir );
  } else {
      $template->template_dir = array( $extDir, $template->template_dir );
  }
}

nicolas

  • I post occasionally
  • **
  • Posts: 92
  • Karma: 6
    • cividesk
  • CiviCRM version: 4.4 LTS
  • CMS version: Standalone (yep)
  • MySQL version: 5.1
  • PHP version: 5.3
Re: Adding a field on a Contribution page with buildForm hook
February 28, 2013, 09:36:32 pm
Thanks much to both of you for your very detailed answers. Karma given.
cividesk -- CiviCRM delivered ... your way!

nicolas

  • I post occasionally
  • **
  • Posts: 92
  • Karma: 6
    • cividesk
  • CiviCRM version: 4.4 LTS
  • CMS version: Standalone (yep)
  • MySQL version: 5.1
  • PHP version: 5.3
Re: Adding a field on a Contribution page with buildForm hook
March 02, 2013, 04:28:12 pm
Wiki updated, page hook_civicrm_buildForm.
cividesk -- CiviCRM delivered ... your way!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Adding a field on a Contribution page with buildForm hook

This forum was archived on 2017-11-26.