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) »
  • Help extending CRM_Core_Form
Pages: [1]

Author Topic: Help extending CRM_Core_Form  (Read 874 times)

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Help extending CRM_Core_Form
May 13, 2010, 06:34:57 pm
Hi folks,

I'm adding a form to provide specialized features in CiviCRM.  At this point I'm just shooting for a "Hello World" setup to be sure I understand how it works.

In this setup, I'm using hook_civicrm_xmlMenu() to register a path, pointing to a very simple class that extends CRM_Core_Form.  (The class code and the xml file are included below.)

The path is being registered and works fine, calling the Foo_Form class, which extends CRM_Core_Form.  There's also a Foo.tpl template in the template path that presents the form output.  I add a text input and a submit button using $this->add(), and it displays fine.

But when I submit the form, I'm having real trouble understanding how to process the input.  postProcess() doesn't seem to run at all, and the form is just reloaded.

Surely I'm doing something wrong, but what could it be?

Since the menu xml does get loaded and does create a working path, I'm guessing the problem is in the class.  This is the class itself:


    require_once 'CRM/Core/Form.php';

    class Foo_Form extends CRM_Core_Form {

        function buildQuickForm() {
            drupal_set_message('buildQuickForm is called.');
            $this->add('text', 'message', 'Message:');
            $this->add('submit','submit', 'Save Message');
        }

        function postProcess() {
            drupal_set_message('postProcess is called.');
            // Do other stuff here if you can get this method to be called.
        }
    }



This is foo.xml, just in case it's relevant:

    <?xml version="1.0" encoding="iso-8859-1" ?>

    <menu>
      <item>
         <path>civicrm/foo</path>
         <page_callback>Foo_Form</page_callback>
         <access_arguments>access CiviCRM</access_arguments>
      </item>
    </menu>


I've banged my head against this for several hours now, looking all over for docs or tutorials on extending CRM_Core_Form, so I'm grateful for any tips.

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

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: Help extending CRM_Core_Form
May 14, 2010, 09:43:17 am
Only obvious thing I can see different is the way you are adding the submit button. Here's an example I have from some working code. See if that helps.

Code: [Select]
$this->addElement('submit',
                          $this->getButtonName( 'next', 'doIt' ),
                          ts( 'Next' ),
                          array( 'class' => 'form-submit' )
                          );

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Help extending CRM_Core_Form
May 16, 2010, 03:04:14 pm
For the curious:

Lobo confirmed on IRC that it's best to use the addButtons() method instead of the add() or addElement() methods.  Turns out that QuickForm expects the button name in a very specific pattern for each button, and addButton does that.

He also mentioned it's possible, if more cumbersome, to use addElement(), as long as you define the name of the button with the getButtonName() method, like so:

        $this->addElement('submit',  $this->getButtonName( 'save' ),  ts( 'Button text' ) );

He also pointed out that the button named with getButtonName('cancel') will redirect to the previous page, which it you can manipulate using pushUserContext() method.  I haven't played with that yet, but hopefully it's not too mysterious.

Thanks for the help, fellas.

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Help extending CRM_Core_Form

This forum was archived on 2017-11-26.