Fix for CRM-15729
[civicrm-core.git] / CRM / Contact / Form / Edit / TagsAndGroups.php
index 80d7eb2b825e9196e0f1cab76931176eb1be48dc..3e87594de9f4b0a64382087813e870cc3c68c47b 100644 (file)
 class CRM_Contact_Form_Edit_TagsAndGroups {
 
   /**
-   * Constant to determine which forms we are generating
+   * Constant to determine which forms we are generating.
    *
    * Used by both profile and edit contact
    */
   const GROUP = 1, TAG = 2, ALL = 3;
 
   /**
-   * build form elements
+   * build form elements.
    * params object $form object of the form
    *
    * @param CRM_Core_Form $form
@@ -148,11 +148,6 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
     }
 
     if ($type & self::TAG) {
-      // CODE FROM CRM/Tag/Form/Tag.php //
-      CRM_Core_Resources::singleton()
-        ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
-        ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
-
       $fName = 'tag';
       if ($fieldName) {
         $fName = $fieldName;
@@ -162,6 +157,16 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
       // get the list of all the categories
       $tags = new CRM_Core_BAO_Tag();
       $tree = $tags->getTree('civicrm_contact', TRUE);
+      // let's not load jstree if there are not children. This also fixes blank
+      // display at the beginning of checkboxes
+      $form->assign('loadjsTree', FALSE);
+      if (!empty(CRM_Utils_Array::retrieveValueRecursive($tree, 'children'))) {
+        // CODE FROM CRM/Tag/Form/Tag.php //
+        CRM_Core_Resources::singleton()
+          ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
+          ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
+        $form->assign('loadjsTree', TRUE);
+      }
 
       $elements = array();
       self::climbtree($form, $tree, $elements);
@@ -210,7 +215,7 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
   }
 
   /**
-   * Set defaults for relevant form elements
+   * Set defaults for relevant form elements.
    *
    * @param int $id
    *   The contact id.