Skip to content

Commit

Permalink
Merge pull request #15375 from ixiam/dev_issue#1282
Browse files Browse the repository at this point in the history
dev/core#1282 Takes care of customfields of type multiselect that were not being rendered
  • Loading branch information
eileenmcnaughton authored Oct 28, 2019
2 parents d2a94fa + 2076c0d commit 6f2246e
Showing 1 changed file with 54 additions and 40 deletions.
94 changes: 54 additions & 40 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2355,45 +2355,7 @@ public static function setProfileDefaults(
$defaults[$fldName] = $details['worldregion_id'];
}
elseif ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($name)) {
// @todo retrieving the custom fields here seems obsolete - $field holds more data for the fields.
$customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $details));

// hack to add custom data for components
$components = ['Contribution', 'Participant', 'Membership', 'Activity'];
foreach ($components as $value) {
$customFields = CRM_Utils_Array::crmArrayMerge($customFields,
CRM_Core_BAO_CustomField::getFieldsForImport($value)
);
}

switch ($customFields[$customFieldId]['html_type']) {
case 'Multi-Select State/Province':
case 'Multi-Select Country':
case 'Multi-Select':
$v = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details[$name]);
foreach ($v as $item) {
if ($item) {
$defaults[$fldName][$item] = $item;
}
}
break;

case 'CheckBox':
$v = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details[$name]);
foreach ($v as $item) {
if ($item) {
$defaults[$fldName][$item] = 1;
// seems like we need this for QF style checkboxes in profile where its multiindexed
// CRM-2969
$defaults["{$fldName}[{$item}]"] = 1;
}
}
break;

default:
$defaults[$fldName] = $details[$name];
break;
}
$defaults[$fldName] = self::reformatProfileDefaults($field, $details[$name]);
}
else {
$defaults[$fldName] = $details[$name];
Expand Down Expand Up @@ -2475,10 +2437,17 @@ public static function setProfileDefaults(
elseif (substr($fieldName, 0, 14) === 'address_custom' &&
CRM_Utils_Array::value(substr($fieldName, 8), $value)
) {
$defaults[$fldName] = $value[substr($fieldName, 8)];
$defaults[$fldName] = self::reformatProfileDefaults($field, $value[substr($fieldName, 8)]);
}
}
}
else {
if (substr($fieldName, 0, 14) === 'address_custom' &&
CRM_Utils_Array::value(substr($fieldName, 8), $value)
) {
$defaults[$fldName] = self::reformatProfileDefaults($field, $value[substr($fieldName, 8)]);
}
}
}
}
}
Expand Down Expand Up @@ -3637,4 +3606,49 @@ public static function getFrontEndTitle(int $profileID) {
return $profile['frontend_title'] ?? $profile['title'];
}

/**
* This function is used to format the profile default values.
*
* @param array $field
* Associated array of profile fields to render.
* @param string $value
* Value to render
*
* @return $defaults
* String or array, depending on the html type
*/
public static function reformatProfileDefaults($field, $value) {
$defaults = [];

switch ($field['html_type']) {
case 'Multi-Select State/Province':
case 'Multi-Select Country':
case 'Multi-Select':
$v = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($value));
foreach ($v as $item) {
if ($item) {
$defaults[$item] = $item;
}
}
break;

case 'CheckBox':
$v = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
foreach ($v as $item) {
if ($item) {
$defaults[$item] = 1;
// seems like we need this for QF style checkboxes in profile where its multiindexed
// CRM-2969
$defaults["[{$item}]"] = 1;
}
}
break;

default:
$defaults = $value;
break;
}
return $defaults;
}

}

0 comments on commit 6f2246e

Please sign in to comment.