Merge pull request #17358 from seamuslee001/d8_prevnext_cache_test_fix
[civicrm-core.git] / CRM / Core / BAO / UFGroup.php
index ca4d9f9502b87b4117b62df3522b854591efcf71..5841ab0d72451a4c9b3585fb0e582a30fc7ce960 100644 (file)
@@ -516,6 +516,7 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup {
       // if field is not present in customFields, that means the user
       // DOES NOT HAVE permission to access that field
       if (array_key_exists($field->field_name, $customFields)) {
+        $formattedField['serialize'] = !empty($customFields[$field->field_name]['serialize']);
         $formattedField['is_search_range'] = $customFields[$field->field_name]['is_search_range'];
         // fix for CRM-1994
         $formattedField['options_per_line'] = $customFields[$field->field_name]['options_per_line'];
@@ -2337,8 +2338,8 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
           elseif ($name == 'world_region') {
             $defaults[$fldName] = $details['worldregion_id'];
           }
-          elseif ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($name)) {
-            $defaults[$fldName] = self::reformatProfileDefaults($field, $details[$name]);
+          elseif (CRM_Core_BAO_CustomField::getKeyID($name)) {
+            $defaults[$fldName] = self::formatCustomValue($field, $details[$name]);
           }
           else {
             $defaults[$fldName] = $details[$name];
@@ -2417,19 +2418,13 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
                         $defaults[$fldName] = $value[$fieldName];
                       }
                     }
-                    elseif (substr($fieldName, 0, 14) === 'address_custom' &&
-                      CRM_Utils_Array::value(substr($fieldName, 8), $value)
-                    ) {
-                      $defaults[$fldName] = self::reformatProfileDefaults($field, $value[substr($fieldName, 8)]);
+                    elseif (strpos($fieldName, 'address_custom') === 0 && !empty($value[substr($fieldName, 8)])) {
+                      $defaults[$fldName] = self::formatCustomValue($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)]);
-                  }
+                elseif (strpos($fieldName, 'address_custom') === 0 && !empty($value[substr($fieldName, 8)])) {
+                  $defaults[$fldName] = self::formatCustomValue($field, $value[substr($fieldName, 8)]);
                 }
               }
             }
@@ -2705,7 +2700,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
 
     if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
       $fixUrl = CRM_Utils_System::url('civicrm/admin/domain', 'action=update&reset=1');
-      CRM_Core_Error::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM &raquo; Communications &raquo; FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', [1 => $fixUrl]));
+      CRM_Core_Error::statusBounce(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM &raquo; Communications &raquo; FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', [1 => $fixUrl]));
     }
 
     foreach ($emailList as $emailTo) {
@@ -3590,48 +3585,31 @@ SELECT  group_id
   }
 
   /**
-   * This function is used to format the profile default values.
+   * Format custom field value for use in prepopulating a quickform profile field.
    *
    * @param array $field
-   *   Associated array of profile fields to render.
+   *   Field metadata.
    * @param string $value
-   *   Value to render
+   *   Raw value
    *
-   * @return $defaults
+   * @return mixed
    *   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;
+  private static function formatCustomValue($field, $value) {
+    if (CRM_Core_BAO_CustomField::isSerialized($field)) {
+      $value = CRM_Utils_Array::explodePadded($value);
 
-      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;
-          }
+      if ($field['html_type'] === 'CheckBox') {
+        $checkboxes = [];
+        foreach (array_filter($value) as $item) {
+          $checkboxes[$item] = 1;
+          // CRM-2969 seems like we need this for QF style checkboxes in profile where its multiindexed
+          $checkboxes["[{$item}]"] = 1;
         }
-        break;
-
-      default:
-        $defaults = $value;
-        break;
+        return $checkboxes;
+      }
     }
-    return $defaults;
+    return $value;
   }
 
 }