Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom field type Hidden #27235

Merged
merged 1 commit into from Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion CRM/Core/BAO/CustomField.php
Expand Up @@ -1132,6 +1132,10 @@ public static function addQuickFormElement(

$qf->assign('customUrls', $customUrls);
break;

case 'Hidden':
$element = $qf->add('hidden', $elementName);
break;
}

switch ($field->data_type) {
Expand Down Expand Up @@ -2221,7 +2225,7 @@ protected static function prepareCreate($params) {

// create any option group & values if required
$allowedOptionTypes = ['String', 'Int', 'Float', 'Money'];
if ($htmlType !== 'Text' && in_array($dataType, $allowedOptionTypes, TRUE)) {
if (!in_array($htmlType, ['Text', 'Hidden'], TRUE) && in_array($dataType, $allowedOptionTypes, TRUE)) {
//CRM-16659: if option_value then create an option group for this custom field.
// An option_type of 2 would be a 'message' from the form layer not to handle
// the option_values key. If not set then it is not ignored.
Expand Down
5 changes: 5 additions & 0 deletions CRM/Core/SelectValues.php
Expand Up @@ -219,6 +219,11 @@ public static function customHtmlType() {
'name' => 'Link',
'label' => ts('Link'),
],
[
'id' => 'Hidden',
'name' => 'Hidden',
'label' => ts('Hidden'),
],
];
}

Expand Down
8 changes: 4 additions & 4 deletions CRM/Custom/Form/Field.php
Expand Up @@ -58,10 +58,10 @@ class CRM_Custom_Form_Field extends CRM_Core_Form {
* @var array[]
*/
public static $_dataToHTML = [
'String' => ['Text', 'Select', 'Radio', 'CheckBox', 'Autocomplete-Select'],
'Int' => ['Text', 'Select', 'Radio'],
'Float' => ['Text', 'Select', 'Radio'],
'Money' => ['Text', 'Select', 'Radio'],
'String' => ['Text', 'Select', 'Radio', 'CheckBox', 'Autocomplete-Select', 'Hidden'],
'Int' => ['Text', 'Select', 'Radio', 'Hidden'],
'Float' => ['Text', 'Select', 'Radio', 'Hidden'],
'Money' => ['Text', 'Select', 'Radio', 'Hidden'],
'Memo' => ['TextArea', 'RichTextEditor'],
'Date' => ['Select Date'],
'Boolean' => ['Radio'],
Expand Down
24 changes: 3 additions & 21 deletions CRM/Custom/Page/Field.php
Expand Up @@ -133,27 +133,9 @@ public function browse() {
$action -= CRM_Core_Action::DISABLE;
}

switch ($customFieldBAO->data_type) {
case "String":
case "Int":
case "Float":
case "Money":
// if Multi Select field is selected in custom field
if ($customFieldBAO->html_type == 'Text') {
$action -= CRM_Core_Action::BROWSE;
}
break;

case "ContactReference":
case "Memo":
case "Date":
case "Boolean":
case "StateProvince":
case "Country":
case "File":
case "Link":
$action -= CRM_Core_Action::BROWSE;
break;
// Remove link to edit option group if there isn't one
if (!$customFieldBAO->option_group_id) {
$action -= CRM_Core_Action::BROWSE;
Comment on lines -136 to +138
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This absolutely takes the prize for the most bizarre waste of 20 lines I've ever seen.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 🎉 Why I'm awarding this one top prize 🏆 🥇 🏆

  1. Switch statement: why even... dear god why?
  2. The code comment made absolutely no sense.
  3. The rest of the switch statement: LOL
  4. None of it had anything to do with the actual criteria that needed to be checked

}

$customFieldDataType = array_column(CRM_Core_BAO_CustomField::dataType(), 'label', 'id');
Expand Down
1 change: 1 addition & 0 deletions Civi/Api4/Generic/BasicGetFieldsAction.php
Expand Up @@ -325,6 +325,7 @@ public function fields() {
'Email' => ts('Email'),
'EntityRef' => ts('Autocomplete Entity'),
'File' => ts('File'),
'Hidden' => ts('Hidden'),
'Location' => ts('Address Location'),
'Number' => ts('Number'),
'Radio' => ts('Radio Buttons'),
Expand Down
Expand Up @@ -181,6 +181,7 @@
return !(defn.options || defn.data_type === 'Boolean');

case 'DisplayOnly':
case 'Hidden':
return true;

default:
Expand Down
1 change: 1 addition & 0 deletions ext/afform/admin/ang/afGuiEditor/inputType/Hidden.html
@@ -0,0 +1 @@
<div class="form-inline"></div>
1 change: 1 addition & 0 deletions ext/afform/core/ang/af/fields/Hidden.html
@@ -0,0 +1 @@
<input type="hidden" id="{{:: fieldId }}" ng-model="getSetValue" ng-model-options="{getterSetter: true}" >
7 changes: 6 additions & 1 deletion templates/CRM/Custom/Form/Edit/CustomField.tpl
Expand Up @@ -14,7 +14,12 @@
<td class="html-adjust description">{$element.help_pre}</td>
</tr>
{/if}
{if $element.options_per_line}
{if $element.html_type === 'Hidden'}
{* Hidden field - render in hidden row *}
<tr class="custom_field-row {$element.element_name}-row hiddenElement">
<td>{$formElement.html}</td>
</tr>
{elseif $element.options_per_line}
<tr class="custom_field-row {$element.element_name}-row">
<td class="label">{$formElement.label}{if $element.help_post}{help id=$element.id file="CRM/Custom/Form/CustomField.hlp" title=$element.label}{/if}</td>
<td class="html-adjust">
Expand Down
4 changes: 2 additions & 2 deletions templates/CRM/Custom/Form/Field.tpl
Expand Up @@ -270,7 +270,7 @@
}

if (_.includes(['String', 'Int', 'Float', 'Money'], dataType)) {
if (htmlType !== "Text") {
if (!['Text', 'Hidden'].includes(htmlType)) {
$("#showoption, #searchable", $form).show();
$("#hideDefault, #hideDesc, #searchByRange", $form).hide();
} else {
Expand All @@ -289,7 +289,7 @@
$("#showoption").hide();
}

if (_.includes(['String', 'Int', 'Float', 'Money'], dataType) && htmlType !== 'Text') {
if (_.includes(['String', 'Int', 'Float', 'Money'], dataType) && !['Text', 'Hidden'].includes(htmlType)) {
if (serialize) {
$('div[id^=checkbox]', '#optionField').show();
$('div[id^=radio]', '#optionField').hide();
Expand Down
16 changes: 11 additions & 5 deletions tests/phpunit/api/v4/Custom/CustomFieldGetFieldsTest.php
Expand Up @@ -52,7 +52,7 @@ public function tearDown(): void {
->execute();
}

public function testDisabledFields(): void {
public function testDisabledAndHiddenFields(): void {
// Create a custom group with one enabled and one disabled field
CustomGroup::create(FALSE)
->addValue('extends', 'Activity')
Expand All @@ -62,14 +62,20 @@ public function testDisabledFields(): void {
'records' => [
['label' => 'enabled_field'],
['label' => 'disabled_field', 'is_active' => FALSE],
['label' => 'hidden_field', 'html_type' => 'Hidden'],
],
'defaults' => ['custom_group_id.name' => 'act_test_grp'],
]);

// Only the enabled field shows up
$getFields = Activity::getFields(FALSE)->execute()->column('name');
$this->assertContains('act_test_grp.enabled_field', $getFields);
$this->assertNotContains('act_test_grp.disabled_field', $getFields);
// Only the enabled fields show up
$getFields = Activity::getFields(FALSE)->execute()->indexBy('name');
$this->assertArrayHasKey('act_test_grp.enabled_field', $getFields);
$this->assertArrayHasKey('act_test_grp.hidden_field', $getFields);
$this->assertArrayNotHasKey('act_test_grp.disabled_field', $getFields);

// Hidden field does not have option lists
$this->assertFalse($getFields['act_test_grp.hidden_field']['options']);
$this->assertNull($getFields['act_test_grp.hidden_field']['suffixes']);

// Disable the entire custom group
CustomGroup::update(FALSE)
Expand Down