Skip to content

Commit

Permalink
CustomField - Remove pointless caching and move form variable to form…
Browse files Browse the repository at this point in the history
… class
  • Loading branch information
colemanw committed Apr 4, 2020
1 parent 9fcbec0 commit 5d2f1ed
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 74 deletions.
81 changes: 15 additions & 66 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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'),
];
}

/**
Expand All @@ -91,40 +74,6 @@ public static function dataToType() {
];
}

/**
* 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.
*
Expand Down
34 changes: 28 additions & 6 deletions CRM/Custom/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -77,10 +103,6 @@ public function preProcess() {
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);

Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testCustomFieldCreateAllAvailableFormInputs() {
$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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/CustomValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function testCreateCustomValue() {
$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'];

Expand Down

0 comments on commit 5d2f1ed

Please sign in to comment.