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) »
  • problem with filling in default value
Pages: [1]

Author Topic: problem with filling in default value  (Read 497 times)

alainb

  • I’m new here
  • *
  • Posts: 24
  • Karma: 0
  • CiviCRM version: 4.4.x
  • CMS version: Drupal
  • MySQL version: 5.x
  • PHP version: 5.x
problem with filling in default value
November 26, 2013, 06:59:00 am
When I register an event participant, I'd like to set the default value of a custom field to a value from a custom field of the event.

Here's the code I wrote:

Code: [Select]
function mydrupalmodule_civicrm_buildForm($formName, &$form)
{
    if ($formName === 'CRM_Event_Form_Participant')
    {
        // get the id of the event
        $eventID = $form->getVar('_eventId');
        if ($eventID)
        {
            // get the event
            $params = array(
                'version' => 3,
                'sequential' => 1,
                'id' => $eventID,
            );           
            $results = civicrm_api('Event', 'getsingle', $params);           
            if (isset($results['id']) && $results['custom_16'])
            {
                // set the field from the event as default for the registration
                $defaults = array(
                    'custom_20_-1' => $results['custom_16'],                   
                );
                $form->setDefaults($defaults);       
            }
        }
    }
}

Although the setDefaults method gets executed, the field is blank on the screen.

What am I doing wrong?

Thanks in advance for your help.


« Last Edit: November 27, 2013, 12:54:53 am by alainb »

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: problem with filling in default value
November 28, 2013, 12:36:18 am
Do you know if the var on the template is also called 'custom_20_1'? It might well be called custom_20?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: problem with filling in default value
November 28, 2013, 12:32:28 pm
Also note that contact reference custom fields need the id tpl field & the name tpl field filled in
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • problem with filling in default value

This forum was archived on 2017-11-26.