Switch to API and get all option groups that are not reserved instead of only ones...
authorMatthew Wire <devel@mrwire.co.uk>
Tue, 29 May 2018 20:57:35 +0000 (21:57 +0100)
committerMatthew Wire <devel@mrwire.co.uk>
Wed, 11 Jul 2018 12:01:41 +0000 (13:01 +0100)
CRM/Core/BAO/CustomField.php
CRM/Custom/Form/Field.php

index 554eb94dc9ea141e0178b67a996f8da24a37452a..282687c1830c8b688374e1e9a06a77a87b32aaae 100644 (file)
@@ -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.
    *
index 5326a2ff108e7a8a207d8a47f4a220652d00e7ae..014ae6ad6e0e1a8e709a729efcc278498c45df70 100644 (file)
@@ -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();",
       ), '<br/>'
     );
+    // 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('', '');