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) »
  • How to set Default Value of custom data field to php code output
Pages: [1]

Author Topic: How to set Default Value of custom data field to php code output  (Read 10815 times)

NatHolder

  • Guest
How to set Default Value of custom data field to php code output
March 24, 2009, 01:10:39 pm
I have a Date custom field and want to set it's default value to the current date.  As it is now, date custom fields only support a static date.  I've looked into the CRM/Contact/Form/CustomData.tpl template to see if I can set a default date there, but it looks like it's getting the data using AJAX and I don't know where to hook in to the AJAX call (is there a way?).

Thanks for any help.

~Nat

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: How to set Default Value of custom data field to php code output
March 24, 2009, 02:19:06 pm

The buildForm hook should still work

For our sample generated data, this hook does the needed trick

Code: [Select]
function civicrm_civicrm_buildForm( $formName, &$form ) {
    if ( $formName == 'CRM_Contact_Form_Edit' ) {
        $defaultDate = array( );
        CRM_Utils_Date::getAllDefaultValues( $defaultDate );
        $defaults['custom_3_-1'] = $defaultDate;
        $form->setDefaults( $defaults );
    }
}

note that custom_3 is the 'marriage date' field and we are localizing the change to the edit form only

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

NatHolder

  • Guest
Re: How to set Default Value of custom data field to php code output
March 24, 2009, 06:01:28 pm
Sweet!  That works.  The next problem is that this custom data group allows multiple records.  I figured out that the _1 in the id represents the record number, so I can just create defaults for all of the expected record numbers (in the example below, it allows for 100 new records), ie:

Code: [Select]
for ($i = 1; $i < 100; $i++) {
$defaults['custom_12_-' . $i] = $defaultDate; // NAFY Client Demographics / Date Recorded
}

How do I figure out the record number of the last record so I can create defaults starting from that record number?  In other words, what $i should start with.  This isn't a big deal since presumably it won't hurt to set defaults for existing records.

Thanks!
~Nat

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: How to set Default Value of custom data field to php code output
March 24, 2009, 09:55:31 pm

i suspect you'll have to look at the form elements ($form->_elements) array and extract it from there.

however for elements that already have a DB record, they should have a date value and hence setting a default might not be necessary

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

NatHolder

  • Guest
Re: How to set Default Value of custom data field to php code output
March 24, 2009, 11:44:39 pm
When the custom data is in edit mode the records get populated with AJAX, so they're not in the $form->_elements array.  In view-only mode they are in the $form->_elements array, so I guess I could save them off and then refer to them later.  Or I could retrieve them using AJAX from PHP?

Do you have any other suggestions?

Thanks lobo.

~nat

najoory

  • Guest
Re: How to set Default Value of custom data field to php code output
December 18, 2009, 05:59:14 pm
Quote from: Donald Lobo on March 24, 2009, 02:19:06 pm

The buildForm hook should still work

For our sample generated data, this hook does the needed trick

Code: [Select]
function civicrm_civicrm_buildForm( $formName, &$form ) {
    if ( $formName == 'CRM_Contact_Form_Edit' ) {
        $defaultDate = array( );
        CRM_Utils_Date::getAllDefaultValues( $defaultDate );
        $defaults['custom_3_-1'] = $defaultDate;
        $form->setDefaults( $defaults );
    }
}

note that custom_3 is the 'marriage date' field and we are localizing the change to the edit form only

lobo


In some reason your example not working for me. Field type: Contact reference

Code: [Select]
function mymodule_civicrm_buildForm( $formName, &$form )
{

switch($formName){
case 'CRM_Event_Form_ManageEvent_EventInfo':

if( !empty($_GET['template_id']) && $_GET['template_id']>0){

$defaults['custom_1_-1'] = 'najoory@gmail.com';
$defaults['custom_1_-1_id'] = '1';
$form->setDefaults( $defaults );
}
print_r($defaults);

break;

default:
break;
}
}

Custom Field still empty. Haven't any idea. Is this example the same for custom data field of any another type? I tried with other data types but it fails in any case except CiviCRM built-in default fields.
Both contact's Id and email  presented in DB. Please help!


najoory

  • Guest
Re: How to set Default Value of custom data field to php code output
December 18, 2009, 06:37:01 pm
If it helps. Function which process defaults in QuickForm package "setDefaults" do it twice.

print_r($defaultValues) shows that function was called twice. First time w/o custom fileds and second - with them.  It set some trigger here
Code: [Select]
           $this->_defaultValues = HTML_QuickForm::arrayMerge($this->_defaultValues, $defaultValues);
            print_r($this->_defaultValues);
            foreach (array_keys($this->_elements) as $key) {
                $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
            }

Logically that first time was civicrm call, second - my call.
« Last Edit: December 18, 2009, 06:44:20 pm by najoory »

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: How to set Default Value of custom data field to php code output
December 19, 2009, 06:50:55 am

in most pages custom fields are retrieved via an AJAX call to the same page. hence u see the code being called twice

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

najoory

  • Guest
Re: How to set Default Value of custom data field to php code output
December 21, 2009, 05:40:38 am
Quote from: Donald Lobo on December 19, 2009, 06:50:55 am

in most pages custom fields are retrieved via an AJAX call to the same page. hence u see the code being called twice

lobo


In that case print_r result must be appended to <div id="custom_values"> but I see two arrays in the top of page before html generated by Smarty. So can it be that only first call affects default custom fields? Code in example fails on clear Civi 3.0.2 (3.0.3 same situation) with only one custom field. I think it is some bug.

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: How to set Default Value of custom data field to php code output
December 21, 2009, 05:41:37 pm

i dont understand what u mean exactly. i've been using a fair amount of hooks in 2.2, and will upgrade that module to 3.1 soon

in the meantime, u can help by tracing and figuring out the issue with the code. contact us on IRC if u need help getting started

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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Why is the name of custom fields changing ?
December 23, 2009, 11:41:05 am
Hi,

I have a somehow related problem, the name of the custom fields seem to change between contacts based on ?

it is sometimes,
custom_1_-1
custom_1_3
custom_1_6

Not sure why it changes between contact. What is the reliable way to modify a custom field in a hook ?

X+

-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

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: How to set Default Value of custom data field to php code output
December 23, 2009, 08:32:40 pm

the difference is if it has a value or not (the second integer is the id in the table)

i would use a regex to identify custom fields: custom_(\d+)_-?(\d+) (syntax might not be exact)

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

najoory

  • Guest
Re: How to set Default Value of custom data field to php code output
January 25, 2010, 12:56:39 pm
Quote from: Donald Lobo on December 21, 2009, 05:41:37 pm

i dont understand what u mean exactly. i've been using a fair amount of hooks in 2.2, and will upgrade that module to 3.1 soon

in the meantime, u can help by tracing and figuring out the issue with the code. contact us on IRC if u need help getting started

lobo


Sorry for my bad English :)

Part of issue is lack of attention.

My issue was there:
Quote
if( !empty($_GET['template_id']) && $_GET['template_id']>0)

In Ajax GET not presented template_id parameter (provided event subtype only), so I unable to set custom fields defaults according to event template settings even with hook. Maybe it isn't a bug. But a big mistake I think. Issue may be resolved by hack of "customData.tpl" and using setDefaults in hook_buildForm. E.G.:

customData.tpl
Code: [Select]
{literal}
<script type="text/javascript">

function buildCustomData( type, subType, subName, cgCount, groupID, isMultiple )
{
var dataUrl = {/literal}"{crmURL p=$urlPath h=0 q='snippet=4&type='}"{literal} + type;

if ( subType ) {
dataUrl = dataUrl + '&subType=' + subType;
}

if ( subName ) {
dataUrl = dataUrl + '&subName=' + subName;
cj('#customData' + subName ).show();
} else {
cj('#customData').show();
}

{/literal}
{if $urlPathVar}
dataUrl = dataUrl + '&' + '{$urlPathVar}'
{/if}
{if $groupID}
dataUrl = dataUrl + '&groupID=' + '{$groupID}'
{/if}
{if $qfKey}
dataUrl = dataUrl + '&qfKey=' + '{$qfKey}'
{/if}
{if $entityID}
dataUrl = dataUrl + '&entityID=' + '{$entityID}'
{/if}
{literal}

if ( !cgCount ) {
cgCount = 1;
var prevCount = 1;
} else if ( cgCount >= 1 ) {
var prevCount = cgCount;
cgCount++;
}

dataUrl = dataUrl + '&cgcount=' + cgCount;
        
         /*hack*/
var reg = /&template_id=[\d]+/i; //get template_id
n = reg.exec( document.location );
if(n[0] != ''){
dataUrl = dataUrl + n[0];
}
         /*hack*/

if ( isMultiple ) {
var fname = '#custom_group_' + groupID + '_' + prevCount;
cj("#add-more-link-"+prevCount).hide();
} else {
if ( subName && subName != 'null' ) {
var fname = '#customData' + subName ;
} else {
var fname = '#customData';
}
}

var response = cj.ajax({
url: dataUrl,
async: false
}).responseText;

cj( fname ).html( response );
}

</script>
{/literal}


hook_buildForm
Code: [Select]
function setCustomFieldsDefaultKeys($_fields=array()){

//set all default values like in event template
foreach($_fields as $key => $val){

$nkey = preg_replace('/(custom_[\d]+_)[\d]+(_id)?/i', '\1-1\2', $key);
$_defaults[$nkey] = $val;
}

return $_defaults;
}

///Hooks implementation

function mymodule_civicrm_buildForm( $formName, &$form )
{

civicrm_initialize();
switch($formName){
case 'CRM_Event_Form_ManageEvent_EventInfo':

require_once 'api/v2/Event.php';

//defaults from template
if( !empty($_GET['template_id']) && $_GET['template_id']>0 ){

$params = array( 'id'=> $_GET['template_id'] );
$result = civicrm_event_get( $params );
$defaults = setCustomFieldsDefaultKeys($result);

$form->setDefaults( $defaults );
}

default:
break;
}
}

UPD: In CiviCRM 3.1.2 this issue was fixed. Thanks for your hard work :)
« Last Edit: February 15, 2010, 03:02:26 am by najoory »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to set Default Value of custom data field to php code output

This forum was archived on 2017-11-26.