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) »
  • Discussion (deprecated) »
  • Alpha and Beta Release Testing »
  • 2.1 Release Testing »
  • Can't add new form elements to CiviCRM forms.
Pages: [1]

Author Topic: Can't add new form elements to CiviCRM forms.  (Read 4451 times)

dalin

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 8
  • CiviCRM version: many
  • CMS version: Drupal 6
  • MySQL version: 5.0
  • PHP version: 5.2
Can't add new form elements to CiviCRM forms.
August 19, 2008, 03:02:53 am
I'm not sure if this will be true of all forms, or just CRM_Contact_Form_CustomData.  But take this example:

/**
 * Implementation of hook_civicrm_buildForm
 *
 * @param string $form_id
 * @param object $form
 */
function oa_custom_civicrm_buildForm($form_id, &$form) {
  drupal_set_message('$form_id: <pre>'. var_export($form_id, true) .'</pre>');
  switch ($form_id) {
    case 'CRM_Contact_Form_CustomData':
     
      // This form shows up on multiple pages.
      if ($_REQUEST['groupId'] == 3) {

        // Add the core field "Source" to this form.
        require_once('packages/HTML/QuickForm/text.php');
        $field = new HTML_QuickForm_text('source', 'Source');
        $form->_elements[] = $field;

        // So far so good.  But now we need to also add an element to CRM_Contact_Form_CustomData->_groupTree
        // so that the element gets printed to the page. 
        // Ack, but we can't since _groupTree is protected!
 
        drupal_set_message(CRM_Core_Error::debug('hook_civicrm_buildForm', $form, false));
      }
       
      break;
  }
}

Or am I going about this all wrong?
--
Dave Hansen-Lange
Web Developer
Advomatic LLC
http://advomatic.com
Hong Kong office

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Can't add new form elements to CiviCRM forms.
August 19, 2008, 11:40:20 am
I would do:

Code: [Select]

function moduleName_civicrm_buildForm( $formName, &$form  ) {
    if ( $formName == 'CRM_Contact_Form_CustomData' ) {
$form->addElement('text', 'source', ts('Source'));
    }
}


Then use custom templates feature of CiviCRM and add below code in my custom template:

Code: [Select]
{$form.source.label}  {$form.source.html}

HTH

Kurund

Found this reply helpful? Support CiviCRM

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Can't add new form elements to CiviCRM forms.
August 19, 2008, 11:45:32 am
I dont think u need to add the below form element to $form->_groupTree.

In addition to the below, you will also need to customize the template and add the 'source' field to the layout where appropriate. You probably want to make this conditional on a specific form to which u are adding (unless u want it added to all CustomData)

note that we reuse form code/templates quite a bit, so some forms like the below will appear in multiple places

You can also write the below code as

Code: [Select]
$form->addElement( 'text', 'source', ts( 'Source' ) );

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

dalin

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 8
  • CiviCRM version: many
  • CMS version: Drupal 6
  • MySQL version: 5.0
  • PHP version: 5.2
Re: Can't add new form elements to CiviCRM forms.
August 19, 2008, 05:55:05 pm
That's much simpler.  I gave up too soon. 

Much original plan was to move the core "Source" field to one of the custom field tabs.  But after my hair pulling yesterday I just gave up and created a new custom field.
--
Dave Hansen-Lange
Web Developer
Advomatic LLC
http://advomatic.com
Hong Kong office

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Discussion (deprecated) »
  • Alpha and Beta Release Testing »
  • 2.1 Release Testing »
  • Can't add new form elements to CiviCRM forms.

This forum was archived on 2017-11-26.