4

I'm using buildQuickForm in an extension to allow editing of some custom settings. It's working well, but when I created the custom settings there was a field called, "help_text". I can't seem to figure out how to display the "help_text" in the form elements.

I create the settings in a page called givingstatements.setting.php and this is the code from that page

/* * https://github.com/artfulrobot/uk.artfulrobot.civicrm.autogroup/blob/master/settings/autogroup.setting.php */ return array(

/*
 * When to show legal statement on giving statements
 */
'givingstatements_show_legal_statement' => array(
    'title' => 'Always show legal statement on giving statements',
    'group_name' => 'givingstatements_show_legal_statement',
    'group' => 'CiviCRM Preferences',
    'name' => 'givingstatements_show_legal_statement',
    'type' => 'String',
    'default' => '0',
    'add' => '4.7',
    'is_domain' => 1,
    'is_contact' => 0,
    'description' => 'Always show legal statement on giving statements.',
    'help_text' => 'If no, legal statement will only appear on statements that are 1 full year',
    'quick_form_type' => 'YesNo', // Without this it will not appear on the settings form.
    //'quick_form_type' => 'Element', // Without this it will not appear on the settings form.
    'html_type' => 'radio',
),

/*
 * Default option for combining giving statements for spouses
 */
'givingstatements_combine_spouse_default' => array(
    'title' => 'Combine giving statements for spouses by default',
    'group_name' => 'givingstatements_combine_spouse_default',
    'group' => 'CiviCRM Preferences',
    'name' => 'givingstatements_combine_spouse_default',
    'type' => 'String',
    'default' => '0',
    'add' => '4.7',
    'is_domain' => 1,
    'is_contact' => 0,
    'description' => 'Combine giving statements for spouses by default.',
    'help_text' => 'User preferences will override this option.',
    'quick_form_type' => 'YesNo', // Without this it will not appear on the settings form.
    //'quick_form_type' => 'Element', // Without this it will not appear on the settings form.
    'html_type' => 'radio',
),

/*
 * Allow individuals without civicontribute access to enter custom date ranges
 */
'givingstatements_custom_date_range_allowed' => array(
    'title' => 'Allow individuals without civicontribute access to enter custom date ranges',
    'group_name' => 'givingstatements_custom_date_range_allowed',
    'group' => 'CiviCRM Preferences',
    'name' => 'givingstatements_custom_date_range_allowed',
    'type' => 'String',
    'default' => '0',
    'add' => '4.7',
    'is_domain' => 1,
    'is_contact' => 0,
    'description' => 'Allow individuals without civicontribute access to enter custom date ranges.',
    'help_text' => 'If set to no, individuals will only be able to select full years.',
    'quick_form_type' => 'YesNo', // Without this it will not appear on the settings form.
    //'quick_form_type' => 'Element', // Without this it will not appear on the settings form.
    'html_type' => 'radio',
),
);

My page that creates the form is called Settings.php and the code on that page is this enter image description here

My tpl page has this code

  {foreach from=$elementNames item=elementName}
<div class="crm-section">
  <div class="label">{$form.$elementName.label}</div>
  <div class="content">{$form.$elementName.html}</div>
  <div class="help">({$form.$elementName.help_text})</div>
  <div class="clear"></div>
</div>
{/foreach}

But $form.$elementName.help_text is empty If I do {$form.$elementName|@debug_print_var} I get the array and it doesn't contain help_text, or the contents of help_text under a different key. It outputs

(Array (11) name => "givingstatements_show_legal_statement" value => "1" type => "group" frozen => false required => true error => null label => " Always show legal statement ..." separator => null html => " Array (9) name => "givingstatements_show_legal_statement" value => "1" type => "radio" frozen => false required => false error => null id => "CIVICRM_QFID_1_givingstatements_show_..." label => " " Array (9) name => "givingstatements_show_legal_statement" value => "0" type => "radio" frozen => false required => false error => null id => "CIVICRM_QFID_0_givingstatements_show_..." label => " "

If I go to the CiviCRM api page I can return the help_text there so I know it's storing it. enter image description here

2

I don't know if this is the best answer but many of the core settings tpl's have the help text directly in the tpl, e.g. <span class="description">{ts}Here is the help text{/ts}</span>. Unless you want your users to be able to edit the help text, that seems like a good enough solution here(?), since your tpl is your own custom file to begin with.

EDIT: An alternative: (Haven't tested this, just as a sketch of proof of concept.)

In buildQuickForm in your loop add a line like this:

$help_texts[$key] = $result['values'][$key]['help_text'];

Then after the loop add a line:

$this->assign('help_texts', $help_texts);

Then in the tpl inside the loop:

<div class="help">({$help_texts.$elementName})</div>

  • Notice that I’m calling the code in the tpl file in a foreach loop. I can’t statically set text in that loop. Obviously I could do if/else statements to check if the element name is each one and then set the help text, but that’s not a great way of doing it. – Jason Brown May 20 at 19:46
  • Yeah, the core ones that do this don't use a loop. I guess that is an option if you're not planning to change your settings list too often. Can I also ask what is "data-something" in your buildQuickForm - if you view the source of the rendered page is there an attribute on the input element called data-something that has your help text? – Demerit May 20 at 20:15
  • Yeah, I forgot that was in there. I had been trying to test some things in the buildQuickForm code and just used that as a temporary var to see if I could get it to populate with the help_text. – Jason Brown May 20 at 20:19
  • 1
    That could be an alternate option too then. Set a different array with your help text in buildQuickForm, keyed on the same $key, and use $this->assign to assign it to the template, and then access it in your smarty loop. – Demerit May 20 at 20:22
  • I think you're on to something there! I just need to figure out how to concatenate the variable name from the $key and the string in smarty. So, I'm doing this in buildQuickForm now foreach($this->_settings AS $key => $value){ $result = civicrm_api3('Setting', 'getfields', [ 'name' => $key, ]); $this->assign($key.'-help_text', $result['values'][$key]['help_text']); //print_r($result['values'][$key]['help_text']); } I've verified that the print_r is the value I want. My only question is, how to concatenate the variable string in smarty for the tpl file? – Jason Brown May 20 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.