From 2921fb782fa977fd841ce97607ef4335e91f7441 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 14 Dec 2015 14:56:05 -0500 Subject: [PATCH] CRM-17646 - Add getOptions method to custom field objects --- CRM/Core/BAO/CustomField.php | 47 +++++++++++++ CRM/Core/PseudoConstant.php | 30 +------- tests/phpunit/CRM/Core/FieldOptionsTest.php | 77 +++++++++++++++++++++ 3 files changed, 126 insertions(+), 28 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 559b0d2420..e6764f9697 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -362,6 +362,49 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label'); } + /** + * @param string $context + * @return array|bool + */ + public function getOptions($context = 'get') { + CRM_Core_DAO::buildOptionsContext($context); + $options = FALSE; + + if (!$this->id) { + return FALSE; + } + if (!$this->data_type || !$this->custom_group_id) { + $this->find(TRUE); + } + + if (!empty($this->option_group_id)) { + $options = CRM_Core_OptionGroup::valuesByID( + $this->option_group_id, + FALSE, + FALSE, + FALSE, + 'label', + !($context == 'validate' || $context == 'get') + ); + } + else { + if ($this->data_type === 'StateProvince') { + $options = CRM_Core_Pseudoconstant::stateProvince(); + } + elseif ($this->data_type === 'Country') { + $options = $context == 'validate' ? CRM_Core_Pseudoconstant::countryIsoCode() : CRM_Core_Pseudoconstant::country(); + } + elseif ($this->data_type === 'Boolean') { + $options = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean(); + } + } + CRM_Utils_Hook::customFieldOptions($this->id, $options, FALSE); + $entity = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'extends'); + $entity = in_array($entity, array('Individual', 'Household', 'Organization')) ? 'Contact' : $entity; + CRM_Utils_Hook::fieldOptions($entity, "custom_{$this->id}", $options, array('context' => $context)); + return $options; + } + /** * Store and return an array of all active custom fields. * @@ -2234,6 +2277,10 @@ ORDER BY html_type"; return $customData; } + /** + * + */ + /** * Get custom field ID. * diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 99508a78a7..4734ff0b23 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -224,35 +224,9 @@ class CRM_Core_PseudoConstant { // Custom fields are not in the schema if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) { - $customField = new CRM_Core_DAO_CustomField(); + $customField = new CRM_Core_BAO_CustomField(); $customField->id = (int) substr($fieldName, 7); - $customField->find(TRUE); - $options = FALSE; - - if (!empty($customField->option_group_id)) { - $options = CRM_Core_OptionGroup::valuesByID($customField->option_group_id, - FALSE, - $params['grouping'], - $params['localize'], - // Note: for custom fields the 'name' column is NULL - CRM_Utils_Array::value('labelColumn', $params, 'label'), - $params['onlyActive'], - $params['fresh'] - ); - } - else { - if ($customField->data_type === 'StateProvince') { - $options = self::stateProvince(); - } - elseif ($customField->data_type === 'Country') { - $options = $context == 'validate' ? self::countryIsoCode() : self::country(); - } - elseif ($customField->data_type === 'Boolean') { - $options = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean(); - } - } - CRM_Utils_Hook::customFieldOptions($customField->id, $options, FALSE); - CRM_Utils_Hook::fieldOptions($entity, "custom_{$customField->id}", $options, $params); + $options = $customField->getOptions(); if ($options && $flip) { $options = array_flip($options); } diff --git a/tests/phpunit/CRM/Core/FieldOptionsTest.php b/tests/phpunit/CRM/Core/FieldOptionsTest.php index dbafee061d..bb6df7e907 100644 --- a/tests/phpunit/CRM/Core/FieldOptionsTest.php +++ b/tests/phpunit/CRM/Core/FieldOptionsTest.php @@ -49,6 +49,11 @@ class CRM_Core_FieldOptionsTest extends CiviUnitTestCase { $this->hookClass = CRM_Utils_Hook::singleton(); } + public function tearDown() { + parent::tearDown(); + $this->quickCleanup(array('civicrm_custom_field', 'civicrm_custom_group')); + } + /** * Assure CRM_Core_PseudoConstant::get() is working properly for a range of * DAO fields having a tag in the XML schema. @@ -133,6 +138,78 @@ class CRM_Core_FieldOptionsTest extends CiviUnitTestCase { $this->assertEquals($result, $originalGender + $this->appendOptions); } + /** + * Ensure hook_civicrm_fieldOptions works with custom fields + */ + public function testHookFieldOptionsWithCustomFields() { + $this->hookClass->setHook('civicrm_fieldOptions', array($this, 'hook_civicrm_fieldOptions')); + + // Create a custom field group for testing. + $custom_group_name = md5(microtime()); + $api_params = array( + 'title' => $custom_group_name, + 'extends' => 'Individual', + 'is_active' => TRUE, + ); + $customGroup = $this->callAPISuccess('customGroup', 'create', $api_params); + + // Add a custom select field. + $api_params = array( + 'custom_group_id' => $customGroup['id'], + 'label' => $custom_group_name . 1, + 'html_type' => 'Select', + 'data_type' => 'String', + 'option_values' => array( + 'foo' => 'Foo', + 'bar' => 'Bar', + ), + ); + $result = $this->callAPISuccess('custom_field', 'create', $api_params); + $customField1 = $result['id']; + + // Add a custom country field. + $api_params = array( + 'custom_group_id' => $customGroup['id'], + 'label' => $custom_group_name . 2, + 'html_type' => 'Select Country', + 'data_type' => 'Country', + ); + $result = $this->callAPISuccess('custom_field', 'create', $api_params); + $customField2 = $result['id']; + + // Add a custom boolean field. + $api_params = array( + 'custom_group_id' => $customGroup['id'], + 'label' => $custom_group_name . 3, + 'html_type' => 'Radio', + 'data_type' => 'Boolean', + ); + $result = $this->callAPISuccess('custom_field', 'create', $api_params); + $customField3 = $result['id']; + + $this->targetField = 'custom_' . $customField1; + $this->replaceOptions = NULL; + $this->appendOptions = array('baz' => 'Baz'); + $field = new CRM_Core_BAO_CustomField(); + $field->id = $customField1; + $this->assertEquals(array('foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz'), $field->getOptions()); + + $this->targetField = 'custom_' . $customField2; + $this->replaceOptions = array('nowhere' => 'Nowhere'); + $field = new CRM_Core_BAO_CustomField(); + $field->id = $customField2; + $this->assertEquals($this->replaceOptions + $this->appendOptions, $field->getOptions()); + + $this->targetField = 'custom_' . $customField3; + $this->replaceOptions = NULL; + $this->appendOptions = array(2 => 'Maybe'); + $field = new CRM_Core_BAO_CustomField(); + $field->id = $customField3; + $this->assertEquals(array(1 => 'Yes', 0 => 'No', 2 => 'Maybe'), $field->getOptions()); + + $field->free(); + } + /** * Implements hook_civicrm_fieldOptions * -- 2.25.1