From 8f9c3cbea992320f344d576c8a87c5bab318466c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 22 Oct 2014 10:09:57 -0400 Subject: [PATCH] CRM-15476 - Add fall-back for ChainSelect when control field not present --- CRM/Core/BAO/UFGroup.php | 23 ++-------------------- CRM/Core/Form.php | 41 ++++++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index b8076b880b..184f520dd7 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -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') { diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 0f13418efb..d805d59f8c 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -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; -- 2.25.1