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) »
  • CiviCRM Profile Fields Amendments
Pages: [1]

Author Topic: CiviCRM Profile Fields Amendments  (Read 596 times)

deej99

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Joomla
  • MySQL version: 5.5.9
  • PHP version: 5.3.6
CiviCRM Profile Fields Amendments
August 09, 2011, 03:26:01 am
Hello. I need some help and advice as I am struggling with this code. I made a Custom profile form and now would like to use coding to make the option available when user select something.

For e.g.: There are 2 fields.

Have you got a topic to discuss - Yes/No (Drop Down)

*If user select No, nothing happens. If Yes Another field come which states Please Specify - Text Box.

Both these fields are in the profile form lets name it Registration. Now I have done this coding but looks like I am doing something wrong.

I worked on two files: select.php & Block.tpl

In Select.php I added this code:

$myName = $this->getName();
                $this->setName($myName . '[]');
                $attrString = $this->_getAttrString($this->_attributes);
                $this->setName($myName);
            }
      
******NEW CODE STARTS******   
         if($this->_attributes[name]=="custom_14"){
         $strHtml .= $tabs . '<select onchange=topic(this.value)' . $attrString . ">\n";
         }
         else
         {
            $strHtml .= $tabs . '<select' . $attrString . ">\n";
         }
***NEW CODE ENDS******

            $strValues = is_array($this->_values)? array_map('strval', $this->_values): array();
            foreach ($this->_options as $option) {
                if (!empty($strValues) && in_array($option['attr']['value'], $strValues, true)) {
                    $option['attr']['selected'] = 'selected';
                }

IN THE ABOVE CODE - Custom_14 is text field id which I picked up from HTML Snippet in the registration form code. I am using topic as value.

This is what I have added to select.php

Now in block.tpl

{* Show explanatory text for field if not in 'view' or 'preview' modes *}
            {if $field.help_post && $action neq 4 && $action neq 1028}
                <span class="description">{$field.help_post}</span>
            {/if}
            </td>
        </tr>
    {else}
**NEW CODE STARTS HERE**
        <tr>
        {if $form.$n.label == '<label for="custom_15">Please specify?</label>'}
            <div class="label" id="semina1" style="display:none;">{$form.$n.label}</div>
             
              {else}
            <div class="label">{$form.$n.label}</div>
            {/if}
           
            {if $form.$n.label == '<label for="custom_15">Please specify?</label>' }
            <div class="edit-value content" id="semina2" style="display:none;">
           
            {else}
             <div class="edit-value content">
            {/if}
            **CODE ENDS HERE**
         
    {else}
        <div class="crm-section {$n}-section">
           {* Show explanatory text for field if not in 'view' or 'preview' modes *}
             {if $field.help_pre && $action neq 4 && $action neq 1028}
                &nbsp;&nbsp;<span class="description">{$field.help_pre}</span>
             {/if}

I am using content id- semina1 & semina2 and Also Custom 14 is drop down menu & custom 15 is text field stating Please specify? It will appear when Option Yes is selected.

IN THE SAME BLOCK.TPL FILE I ADDED THE BELOW CODE AS WELL TO DEFINE CONTENT ID.

{if $help_post && $action neq 4}<br /><div class="messages help">{$help_post}</div>{/if}
    {/strip}
 
{/if} {* fields array is not empty *}

**NEW CODE START**

{if $field.groupTitle == "Registration"}
        {literal}
        <script type="text/javascript">
         function topic(element){
         if(element == "Yes"){
         document.getElementById("semina1").style.display = "";
         document.getElementById("semina2").style.display = "";
         }
         else{
            document.getElementById("semina1").style.display = "none";
            document.getElementById("semina2").style.display = "none";
         }
       }
      
      </script>
      {/literal}
        {/if}
**NEW CODE ENDS**
{literal}
  <script type="text/javascript">
   
IN THIS CODE - Registration is name of the profile form, Topic is select value mentioned in Select.php, Content Id semina1 & 2 as mentioned above.

I am not sure what I am doing wrong. Looks like I am missing something in block.tpl.

Thank you for your help in advance.

It will be a good idea if this option can be added to the new update as it will make it more userfriendly and make the civicrm more powerful tool than before.

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: CiviCRM Profile Fields Amendments
August 10, 2011, 01:58:15 am
I don't think your approach correct. In general you should avoid modifying core files etc. You can achieve this functionality using simple js / jquery.
You can use below code as a reference and can use it using custom template or buildForm hook.

Quote
function showTextBox( selectedValue ) {
   console.log( selectedValue);
   if ( selectedValue == '1' ) {
       cj('.custom_8-section').show();
   } else {
       cj('.custom_8-section').hide();
   }
}
cj(function(){
   showTextBox( cj('#custom_7').val() );
   cj('#custom_7').change( function() {
      showTextBox( cj(this).val() );
   });
});

HTh
Kurund
Found this reply helpful? Support CiviCRM

deej99

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Joomla
  • MySQL version: 5.5.9
  • PHP version: 5.3.6
Re: CiviCRM Profile Fields Amendments
August 10, 2011, 02:54:55 am
Thank you for your help & support. Really appreciated. I will try this method as well. I made the code and it works now but I am looking for the best approach and see which is feasible. I try this method and if require any further information, I will post here.

BIG THANK YOU

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: CiviCRM Profile Fields Amendments
August 10, 2011, 03:13:29 am
Hi,

That's even easier to add the bit of jquery code you want now:

http://civicrm.org/blogs/dave-greenberg/now-its-easier-add-custom-behaviors-templates

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

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

This forum was archived on 2017-11-26.