From c0fce6be80ea0526d5211d337174d8ff12c408cc Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Tue, 29 May 2018 21:57:35 +0100 Subject: [PATCH] Switch to API and get all option groups that are not reserved instead of only ones that are already linked to custom fields --- CRM/Core/BAO/CustomField.php | 2 ++ CRM/Custom/Form/Field.php | 44 +++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 554eb94dc9..282687c183 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -2030,6 +2030,8 @@ AND cf.id = %1"; /** * Get custom option groups. * + * @deprecated Use the API OptionGroup.get + * * @param array $includeFieldIds * Ids of custom fields for which option groups must be included. * diff --git a/CRM/Custom/Form/Field.php b/CRM/Custom/Form/Field.php index 5326a2ff10..014ae6ad6e 100644 --- a/CRM/Custom/Form/Field.php +++ b/CRM/Custom/Form/Field.php @@ -318,21 +318,26 @@ class CRM_Custom_Form_Field extends CRM_Core_Form { if ($this->_action == CRM_Core_Action::UPDATE) { $this->freeze('data_type'); } - $includeFieldIds = NULL; + + $optionGroupParams = [ + 'is_reserved' => 0, + 'is_active' => 1, + 'options' => ['limit' => 0, 'sort' => "title ASC"], + 'return' => ['title'], + ]; if ($this->_action == CRM_Core_Action::UPDATE) { - $includeFieldIds = $this->_values['id']; - } - $optionGroups = CRM_Core_BAO_CustomField::customOptionGroup($includeFieldIds); - $emptyOptGroup = FALSE; - if (empty($optionGroups)) { - $emptyOptGroup = TRUE; - $optionTypes = array('1' => ts('Create a new set of options')); + $optionGroupParams['id'] = $this->_values['id']; } - else { - $optionTypes = array( - '1' => ts('Create a new set of options'), - '2' => ts('Reuse an existing set'), - ); + // Get all custom (is_reserved=0) option groups + $optionGroupMetadata = civicrm_api3('OptionGroup', 'get', $optionGroupParams); + + // OptionGroup selection + $optionTypes = array('1' => ts('Create a new set of options')); + + if (!empty($optionGroupMetadata['values'])) { + $emptyOptGroup = FALSE; + $optionGroups = CRM_Utils_Array::collect('title', $optionGroupMetadata['values']); + $optionTypes['2'] = ts('Reuse an existing set'); $this->add('select', 'option_group_id', @@ -342,6 +347,10 @@ class CRM_Custom_Form_Field extends CRM_Core_Form { ) + $optionGroups ); } + else { + // No custom (non-reserved) option groups + $emptyOptGroup = TRUE; + } $element = &$this->addRadio('option_type', ts('Option Type'), @@ -350,6 +359,10 @@ class CRM_Custom_Form_Field extends CRM_Core_Form { 'onclick' => "showOptionSelect();", ), '
' ); + // if empty option group freeze the option type. + if ($emptyOptGroup) { + $element->freeze(); + } $contactGroups = CRM_Core_PseudoConstant::group(); asort($contactGroups); @@ -370,11 +383,6 @@ class CRM_Custom_Form_Field extends CRM_Core_Form { $this->add('hidden', 'filter_selected', 'Group', array('id' => 'filter_selected')); - //if empty option group freeze the option type. - if ($emptyOptGroup) { - $element->freeze(); - } - // form fields of Custom Option rows $defaultOption = array(); $_showHide = new CRM_Core_ShowHideBlocks('', ''); -- 2.25.1