From 5d2f1ed691c6fbfec149c8382cfc8e116c5f2297 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 3 Apr 2020 10:44:39 -0400 Subject: [PATCH] CustomField - Remove pointless caching and move form variable to form class --- CRM/Core/BAO/CustomField.php | 81 +++++------------------- CRM/Custom/Form/Field.php | 34 ++++++++-- tests/phpunit/api/v3/CustomFieldTest.php | 2 +- tests/phpunit/api/v3/CustomValueTest.php | 2 +- 4 files changed, 45 insertions(+), 74 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 05298e5e79..ecb7f2b828 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -20,20 +20,6 @@ */ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { - /** - * Array for valid combinations of data_type & descriptions. - * - * @var array - */ - public static $_dataType = NULL; - - /** - * Array for valid combinations of data_type & html_type. - * - * @var array - */ - public static $_dataToHtml = NULL; - /** * Array to hold (formatted) fields for import * @@ -47,24 +33,21 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { * @return array * Data type => Description */ - public static function &dataType() { - if (!(self::$_dataType)) { - self::$_dataType = [ - 'String' => ts('Alphanumeric'), - 'Int' => ts('Integer'), - 'Float' => ts('Number'), - 'Money' => ts('Money'), - 'Memo' => ts('Note'), - 'Date' => ts('Date'), - 'Boolean' => ts('Yes or No'), - 'StateProvince' => ts('State/Province'), - 'Country' => ts('Country'), - 'File' => ts('File'), - 'Link' => ts('Link'), - 'ContactReference' => ts('Contact Reference'), - ]; - } - return self::$_dataType; + public static function dataType() { + return [ + 'String' => ts('Alphanumeric'), + 'Int' => ts('Integer'), + 'Float' => ts('Number'), + 'Money' => ts('Money'), + 'Memo' => ts('Note'), + 'Date' => ts('Date'), + 'Boolean' => ts('Yes or No'), + 'StateProvince' => ts('State/Province'), + 'Country' => ts('Country'), + 'File' => ts('File'), + 'Link' => ts('Link'), + 'ContactReference' => ts('Contact Reference'), + ]; } /** @@ -91,40 +74,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { ]; } - /** - * Get data to html array. - * - * (Does this caching achieve anything?) - * - * @return array - */ - public static function dataToHtml() { - if (!self::$_dataToHtml) { - self::$_dataToHtml = [ - [ - 'Text' => 'Text', - 'Select' => 'Select', - 'Radio' => 'Radio', - 'CheckBox' => 'CheckBox', - 'Multi-Select' => 'Multi-Select', - 'Autocomplete-Select' => 'Autocomplete-Select', - ], - ['Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'], - ['Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'], - ['Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'], - ['TextArea' => 'TextArea', 'RichTextEditor' => 'RichTextEditor'], - ['Date' => 'Select Date'], - ['Radio' => 'Radio'], - ['StateProvince' => 'Select State/Province', 'Multi-Select' => 'Multi-Select State/Province'], - ['Country' => 'Select Country', 'Multi-Select' => 'Multi-Select Country'], - ['File' => 'File'], - ['Link' => 'Link'], - ['ContactReference' => 'Autocomplete-Select'], - ]; - } - return self::$_dataToHtml; - } - /** * Takes an associative array and creates a custom field object. * diff --git a/CRM/Custom/Form/Field.php b/CRM/Custom/Form/Field.php index 3881c1d980..4f5625d81a 100644 --- a/CRM/Custom/Form/Field.php +++ b/CRM/Custom/Form/Field.php @@ -62,10 +62,36 @@ class CRM_Custom_Form_Field extends CRM_Core_Form { private static $_dataTypeValues = NULL; private static $_dataTypeKeys = NULL; - private static $_dataToHTML = NULL; - private static $_dataToLabels = NULL; + /** + * Used for mapping data types to html type options. + * + * Each item in this array corresponds to the same index in the dataType array + * @var array + */ + public static $_dataToHTML = [ + [ + 'Text' => 'Text', + 'Select' => 'Select', + 'Radio' => 'Radio', + 'CheckBox' => 'CheckBox', + 'Multi-Select' => 'Multi-Select', + 'Autocomplete-Select' => 'Autocomplete-Select', + ], + ['Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'], + ['Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'], + ['Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'], + ['TextArea' => 'TextArea', 'RichTextEditor' => 'RichTextEditor'], + ['Date' => 'Select Date'], + ['Radio' => 'Radio'], + ['StateProvince' => 'Select State/Province', 'Multi-Select' => 'Multi-Select State/Province'], + ['Country' => 'Select Country', 'Multi-Select' => 'Multi-Select Country'], + ['File' => 'File'], + ['Link' => 'Link'], + ['ContactReference' => 'Autocomplete-Select'], + ]; + /** * Set variables up before form is built. * @@ -77,10 +103,6 @@ class CRM_Custom_Form_Field extends CRM_Core_Form { self::$_dataTypeValues = array_values(CRM_Core_BAO_CustomField::dataType()); } - if (!self::$_dataToHTML) { - self::$_dataToHTML = CRM_Core_BAO_CustomField::dataToHtml(); - } - //custom field id $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); diff --git a/tests/phpunit/api/v3/CustomFieldTest.php b/tests/phpunit/api/v3/CustomFieldTest.php index ecb40289f2..5d828378d2 100644 --- a/tests/phpunit/api/v3/CustomFieldTest.php +++ b/tests/phpunit/api/v3/CustomFieldTest.php @@ -105,7 +105,7 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase { $gid = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'testAllFormInputs']); $dtype = CRM_Core_BAO_CustomField::dataType(); - $htype = CRM_Core_BAO_CustomField::dataToHtml(); + $htype = CRM_Custom_Form_Field::$_dataToHTML; $n = 0; foreach ($dtype as $dkey => $dvalue) { diff --git a/tests/phpunit/api/v3/CustomValueTest.php b/tests/phpunit/api/v3/CustomValueTest.php index f2f02d4fed..e7bb8d2a0a 100644 --- a/tests/phpunit/api/v3/CustomValueTest.php +++ b/tests/phpunit/api/v3/CustomValueTest.php @@ -94,7 +94,7 @@ class api_v3_CustomValueTest extends CiviUnitTestCase { $this->_customFieldID = $this->_customField['id']; $customFieldDataType = CRM_Core_BAO_CustomField::dataType(); - $dataToHtmlTypes = CRM_Core_BAO_CustomField::dataToHtml(); + $dataToHtmlTypes = CRM_Custom_Form_Field::$_dataToHTML; $count = 0; $optionSupportingHTMLTypes = ['Select', 'Radio', 'CheckBox', 'Autocomplete-Select', 'Multi-Select']; -- 2.25.1