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) »
  • Is this a bug in the way civicrm_buildForm Hook is called?
Pages: [1]

Author Topic: Is this a bug in the way civicrm_buildForm Hook is called?  (Read 656 times)

lolas

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 9
    • Freeform Solutions
  • CiviCRM version: Several
  • CMS version: Drupal
  • MySQL version: 5.1+
  • PHP version: Several
Is this a bug in the way civicrm_buildForm Hook is called?
March 04, 2013, 09:57:54 am
I've got a multivalued custom group called Contract added as a tab on the contacts page.
I am trying to default one of the values only when a new record is added and I am following the example from wiki below:

Code: [Select]
function example_civicrm_buildForm($formName, &$form) {
  if ($formName == 'CRM_Event_Form_Registration_Register') {
    if ($form->getAction() == CRM_Core_Action::ADD) {
      $defaults['price_3'] = '710';
      $form->setDefaults($defaults);
    }
  }
}

When I go to the tab for this custom data group there is one button "Edit contract" which implies that there is already a contract for this contact even if there is not one - double checked with database.
My code is below. If I wrap it with
Code: [Select]
if ($form->getAction() == CRM_Core_Action::ADD) { it will never get called. It seems that CRM_Core_Action::UPDATE is always called even when creating a new contract data record.

Code: [Select]
function mymod_contracts_civicrm_buildForm($formName, &$form) {
  if ($formName == 'CRM_Contact_Form_CustomData' && $form->_groupID == '2') {

    $gotNumElement = $form->elementExists('custom_11_3');
    $gotYrElement = $form->elementExists('custom_101_3');
    $action = $form->getAction();
    dpm($action); // Gives 2
    dpm(CRM_Core_Action::ADD); // Gives 1
    if ($form->getAction() == CRM_Core_Action::ADD) {
      if($gotNumElement) {
        $max_contact_num = 0;
        $maxsql = "SELECT MAX(custom_11) as max_contract_num FROM civicrm_value_contract_2";
        $dao = CRM_Core_DAO::executeQuery( $maxsql );
        while ( $dao->fetch( ) ) {
            $max_contact_num = intval($dao->max_contract_num);
        }
        $defaults = array();
        $defaults['custom_11_3'] = $max_contact_num + 1;
        $form->setDefaults($defaults);
      }
    }
    if($gotYrElement) {
      $form->addRule('custom_101_3', 'Contract Year is required', 'required');
      $form->addRule('custom_101_3', 'Contract Year must be a valid year, 1900 or greater', 'regex' , '/^(?:19)|(?:20)\d\d$/');
    }   
  }
}
Freeform Solutions provides technology and management consulting, website and database development, and managed internet hosting solutions for not-for-profit organizations (NFPs).

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: Is this a bug in the way civicrm_buildForm Hook is called?
March 04, 2013, 04:49:01 pm

In this case since the contact is already created, we treat the create / edit of any data attached to the contact as a generic update.

I think it made the code a bit cleaner and easier to write

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

lolas

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 9
    • Freeform Solutions
  • CiviCRM version: Several
  • CMS version: Drupal
  • MySQL version: 5.1+
  • PHP version: Several
Re: Is this a bug in the way civicrm_buildForm Hook is called?
March 05, 2013, 06:40:30 am
Ok, thanks. However, I think that for a multi-valued custom group of fields where more than one of them can be added to the contact that this can be a drawback.

I shall have to try to get the existing value of the field and if it is empty I will default it. This will have the effect that it will default even when the value has somehow been left blank on the first save or deleted - since it is not required.

Most of the time this is not a problem except that it is less clear to the user that this is a new generated value. When they are editing an existing record they may think that the value is already saved. I think perhaps I should do this with Ajax and Javascript so that I can also format the field and point out that it is defaulting.
Freeform Solutions provides technology and management consulting, website and database development, and managed internet hosting solutions for not-for-profit organizations (NFPs).

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Is this a bug in the way civicrm_buildForm Hook is called?

This forum was archived on 2017-11-26.