Skip to main content

SugarCRM : Programmatically Hiding and Displaying Panels Based on the Value of a Drop Down

Sugar Version: 5.1

Overview

In many cases it is desirable to hide or show certain fields on a form based on the value of a field.  For example, if the account is a “partner” the form should show partnerish fields.  The easiest way to do this is to create panels with information specific to each record type and then hide or show the panels based on the record type.

Step 1 – Rearrange the Edit and Detail Views

Start with the edit view first.  From Studio, open the account edit view and create 2 panels.
Name the first panel “CUSTOMER INFO” and the second panel “PARTNER INFO”.   The naming of these is very important because they will be used in the JavaScript code to hide these panels.  Drag some fields into both panels (See example below)

Repeat the process for the detail view ensuring the panel names are the same.



Creating a Custom Edit Handler

Create a file called custom/modules/Accounts/views/view.edit.php.  This file is a custom edit view handler for the accounts module.  It will use all of the default behavior, but it will add some JavaScript code to add an event handler for the onchange event of the account type.  The text in red is what you would change for your specific field and panels.
Add the following code:
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/MVC/View/views/view.edit.php');
class AccountsViewEdit extends ViewEdit {
        function AccountsViewEdit(){
                parent::ViewEdit();
        }
        function display() {
               
                                global $app_list_strings;         
                                global $mod_strings;
                               
                                $fieldName = 'account_type'; //change this to the name of your field
                                $dropDownName = 'account_type_dom'; //change this to the name of the dropdown
                               
                                $e=$this->ev->defs['panels'];
                                $panelArray = array();
                                foreach($e as $panel_label=>$panel_data) {
                                                if($panel_label != '' && $panel_label != 'default')
                                                {
                                                                $tempArray = array($panel_label,$mod_strings[strtoupper($panel_label)]);
                                                                array_push($panelArray,$tempArray);
                                                }
                                }

                                $prePop = 'document.getElementById(\''.$fieldName.'\').onchange();';
                                                               
                                print '';                       

$js=<<
        
                               
EOQ;
                parent::display();
                echo $js;
        }
}
?>

Explaining the Code
class AccountsViewEdit extends ViewEdit {
        function AccountsViewEdit(){

These two lines set the class and function that is required in edit views. Change the text in red to the name of the module that you are configuring this for.
$fieldName = 'account_type'; //change this to the name of your field
$dropDownName = 'account_type_dom'; //change this to the name of the dropdown

So that the code can be easily adapted to any module, we assign the field that we are checking and the drop down list to variables that will be used throughout the program. The values in red need to be changed so that they reflect the fields in your module.
1 $e=$this->ev->defs['panels'];
2                              $panelArray = array();
3                              foreach($e as $panel_label=>$panel_data) {
4                                              if($panel_label != '' && $panel_label != 'default')
5                                              {
6                                                              $tempArray = array($panel_label,$mod_strings[strtoupper($panel_label)]);
7                                                              array_push($panelArray,$tempArray);
8                                              }
9                              }
 In 5.1 the div names are set with the system panel label which will be something similar toLBL_PANEL1. There are also corresponding user panel labels which are what is seen in the UI ( CUSTOMER INFO ).
Line 1 assigns the editview ( ev ) panel definitions ( defs[‘panels’]  ) to the variable $e.
The foreach loop iterates through all the system panel labels and checks to make sure that they aren’t empty or named default  ( Line 4 ). Then is pushes the system panel label onto an array with its corresponding user panel label ( Line 6 ). On line 7 we push the $tempArray of corresponding values on to another array called $panelArray. This allows me to iterate through $panelarray and get sets of data instead of one value.
//Code to hide or display the panels when first editing the record
 $prePop = 'document.getElementById(\''.$fieldName.'\').onchange();';

When the form first draws (as either a new record or editing an existing record) the correct panels should be hidden or displayed.  The code above will call the onchange event for the account type so the form is initially rendered properly.
print '';
                                                               
                                                               

$js=<<
        
EOQ;
        parent::display();
        echo $js; 
        }
}
?>


Comments

Popular posts from this blog

STRESS MANAGEMENT

STRESS Stress is an unpleasant fact of life.  We all experience it for various reasons,  and we all try to come up with ways of  coping with it—some with more success  than others. So what exactly is stress  doing to your mind (and body)  when you're staring down a deadline?  And what can you do to power through it? What Stress Actually Does  to You and What You Can  Do About It The real problem with stress is that, for such a well  understood and universally experienced condition,  as a society we deal with it so poorly that it leads  to many of our most lethal illnesses and  long-term health problems. High blood  pressure, heart disease, cancer, stroke,  obesity, and insomnia are all medical  conditions across the spectrum that  can be related to or directly influenced  by high stress as an environmental conditio...

SugarCRM Footer Logo Remove

Hi Googler, Let us discuss about how to remove the SugarCRM Footer logo.. You all are aware of the power of the SugarCRM Tool and also must be very eager to remove the footer in order to make it look more professional. So below mentioned are some tricks for removing the footer from SugarCRM Community edition. 1) O pen-modules/Administration/ updater_utils.php Add-exit() ; in between function check_now() and return . By doing this u can remove 'powered by sugar crm' footer logo. 2) Go to modules/Trackers/Tracker.php, line 128, in the 'logPage' function. Drop the 'echo' statement. 3) Now to root(Sugarfolder)\include\mvc\view\sugarview.php and modify the line array(show header => true, show subpanel => true...........and so on) and make the changes to showfooter=>true to showfooter=>false And then enjoy by making it as your own built tool. As per the requests, make a reply and then I'll post the code and path to let you know how to write a cust...

Jquery Carousels

Jquery Carousels we all love carousels they are a fantastic way to give the effects we want our visitors to have, more than that we have multiple components to show like images, links, text etc. There are lot many ways to achieve it, Jquery is ofcourse the best possible option available outside. We all search for lot of ems it can be Jquery Flexslider  or Jcarousel Lite  whatever you choose, customization is required, in this tutorial I am not going to focus on how to install these libraries rather one step ahead, to let you know how these carousels can be called multiple times in the same page having their controls working respectively for each carousel instance. So, lets get started with the HTML <div id="sideRight"> <div id="first">       <!-- Do not change the class and tag type, as this will remain as it is for all the following divisions-->       <p class="containheader">Plans for you <a ...