CRM-15476 - Add fall-back for ChainSelect when control field not present
authorColeman Watts <coleman@civicrm.org>
Wed, 22 Oct 2014 14:09:57 +0000 (10:09 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 22 Oct 2014 14:09:57 +0000 (10:09 -0400)
CRM/Core/BAO/UFGroup.php
CRM/Core/Form.php

index b8076b880bb1ba9ce98e627644c087c7873c20a8..184f520dd7baf893096732038ee5ba56e3103171 100644 (file)
@@ -1790,11 +1790,6 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
 
     $selectAttributes = array('class' => 'crm-select2', 'placeholder' => TRUE);
 
-    // CRM-15172 - keep track of all the fields we've processed
-    // This is hackish but we can't always rely on $form->_fields depending on how this is called
-    static $fieldsProcessed = array();
-    $fieldsProcessed[] = $name;
-
     if ($fieldName == 'image_URL' && $mode == CRM_Profile_Form::MODE_EDIT) {
       $deleteExtra = ts('Are you sure you want to delete contact image.');
       $deleteURL = array(
@@ -1825,14 +1820,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
     );
 
     if (substr($fieldName, 0, 14) === 'state_province') {
-      $controlField = str_replace('state_province', 'country', $name);
-      if (isset($form->_fields[$controlField]) || in_array($controlField, $fieldsProcessed)) {
-          $form->addChainSelect($name, array('label' => $title, 'required' => $required));
-      }
-      else {
-        $options = CRM_Core_PseudoConstant::stateProvince();
-        $form->add('select', $name, $title, $options, $required && $options, $selectAttributes);
-      }
+      $form->addChainSelect($name, array('label' => $title, 'required' => $required));
       $config = CRM_Core_Config::singleton();
       if (!in_array($mode, array(
         CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH)) &&
@@ -1855,14 +1843,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
     }
     elseif (substr($fieldName, 0, 6) === 'county') {
       if ($addressOptions['county']) {
-        $controlField = str_replace('county', 'state_province', $name);
-        if (isset($form->_fields[$controlField]) || in_array($controlField, $fieldsProcessed)) {
-          $form->addChainSelect($name, array('label' => $title, 'required' => $required));
-        }
-        else {
-          $options = CRM_Core_PseudoConstant::county();
-          $form->add('select', $name, $title, $options, $required && $options, $selectAttributes);
-        }
+        $form->addChainSelect($name, array('label' => $title, 'required' => $required));
       }
     }
     elseif (substr($fieldName, 0, 9) === 'image_URL') {
index 0f13418efbc87837618aa3155ee1687fa3c81cbc..d805d59f8c1253f78efd93122c4c0d97c3f79578 100644 (file)
@@ -1744,7 +1744,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
       'placeholder' => empty($settings['required']) ? ts('- none -') : ts('- select -'),
     );
     CRM_Utils_Array::remove($props, 'label', 'required', 'control_field');
-    $props['class'] = (empty($props['class']) ? '' : "{$props['class']} ") . 'crm-chain-select-target crm-select2';
+    $props['class'] = (empty($props['class']) ? '' : "{$props['class']} ") . 'crm-select2';
     $props['data-select-prompt'] = $props['placeholder'];
     $props['data-name'] = $elementName;
 
@@ -1762,26 +1762,35 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    */
   private function preProcessChainSelectFields() {
     foreach ($this->_chainSelectFields as $control => $target) {
-      $controlField = $this->getElement($control);
       $targetField = $this->getElement($target);
-      $controlType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ? 'stateProvince' : 'country';
-
-      $css = (string) $controlField->getAttribute('class');
-      $controlField->updateAttributes(array(
-        'class' => ($css ? "$css " : 'crm-select2 ') . 'crm-chain-select-control',
-        'data-target' => $target,
-      ));
-      $controlValue = $controlField->getValue();
+      $targetType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ? 'county' : 'stateProvince';
       $options = array();
-      if ($controlValue) {
-        $options = CRM_Core_BAO_Location::getChainSelectValues($controlValue, $controlType, TRUE);
-        if (!$options) {
-          $targetField->setAttribute('placeholder', $targetField->getAttribute('data-none-prompt'));
+      // If the control field is on the form, setup chain-select and dynamically populate options
+      if ($this->elementExists($control)) {
+        $controlField = $this->getElement($control);
+        $controlType = $targetType == 'county' ? 'stateProvince' : 'country';
+
+        $targetField->setAttribute('class', $targetField->getAttribute('class') . ' crm-chain-select-target');
+
+        $css = (string) $controlField->getAttribute('class');
+        $controlField->updateAttributes(array(
+          'class' => ($css ? "$css " : 'crm-select2 ') . 'crm-chain-select-control',
+          'data-target' => $target,
+        ));
+        $controlValue = $controlField->getValue();
+        if ($controlValue) {
+          $options = CRM_Core_BAO_Location::getChainSelectValues($controlValue, $controlType, TRUE);
+          if (!$options) {
+            $targetField->setAttribute('placeholder', $targetField->getAttribute('data-none-prompt'));
+          }
+        } else {
+          $targetField->setAttribute('placeholder', $targetField->getAttribute('data-empty-prompt'));
+          $targetField->setAttribute('disabled', 'disabled');
         }
       }
+      // Control field not present - fall back to loading default options
       else {
-        $targetField->setAttribute('placeholder', $targetField->getAttribute('data-empty-prompt'));
-        $targetField->setAttribute('disabled', 'disabled');
+        $options = CRM_Core_PseudoConstant::$targetType();
       }
       if (!$targetField->getAttribute('multiple')) {
         $options = array('' => $targetField->getAttribute('placeholder')) + $options;