Simple function extraction on editing Address (within main contact edit
authoreileen <emcnaughton@wikimedia.org>
Thu, 29 Mar 2018 11:17:21 +0000 (00:17 +1300)
committereileen <emcnaughton@wikimedia.org>
Thu, 29 Mar 2018 11:17:21 +0000 (00:17 +1300)
CRM/Contact/Form/Edit/Address.php

index 366d02ba44ac3b88eee12a8d0541ee51b6a3e2c8..a2dceba22aa5520ea080f91d594641f5891462f7 100644 (file)
@@ -158,56 +158,7 @@ class CRM_Contact_Form_Edit_Address {
     }
     $form->assign('geoCode', $geoCode);
 
-    // Process any address custom data -
-    $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address', NULL, $entityId);
-
-    if (isset($groupTree) && is_array($groupTree)) {
-      // use simplified formatted groupTree
-      $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
-
-      // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]'
-      foreach ($groupTree as $id => $group) {
-        foreach ($group['fields'] as $fldId => $field) {
-          $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
-          $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]";
-        }
-      }
-
-      $defaults = array();
-      CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
-
-      // since we change element name for address custom data, we need to format the setdefault values
-      $addressDefaults = array();
-      foreach ($defaults as $key => $val) {
-        if (empty($val)) {
-          continue;
-        }
-
-        // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
-        // this works for all types custom data
-        $keyValues = explode('[', str_replace(']', '', $key));
-        $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
-      }
-
-      $form->setDefaults($addressDefaults);
-
-      // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
-      // And we can't set it to 'address_' because we want to set it in a slightly different format.
-      CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
-
-      // during contact editing : if no address is filled
-      // required custom data must not produce 'required' form rule error
-      // more handling done in formRule func
-      CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree);
-
-      $tplGroupTree = CRM_Core_Smarty::singleton()->get_template_vars('address_groupTree');
-      $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree;
-
-      $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree));
-      // unset the temp smarty var that got created
-      $form->assign('dnc_groupTree', NULL);
-    }
-    // address custom data processing ends ..
+    self::addCustomDataToForm($form, $entityId, $blockId);
 
     if ($sharing) {
       // shared address
@@ -434,4 +385,64 @@ class CRM_Contact_Form_Edit_Address {
     }
   }
 
+  /**
+   * Add custom data to the form.
+   *
+   * @param CRM_Core_Form $form
+   * @param int $entityId
+   * @param int $blockId
+   */
+  protected static function addCustomDataToForm(&$form, $entityId, $blockId) {
+    $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address', NULL, $entityId);
+
+    if (isset($groupTree) && is_array($groupTree)) {
+      // use simplified formatted groupTree
+      $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
+
+      // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]'
+      foreach ($groupTree as $id => $group) {
+        foreach ($group['fields'] as $fldId => $field) {
+          $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
+          $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]";
+        }
+      }
+
+      $defaults = array();
+      CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
+
+      // since we change element name for address custom data, we need to format the setdefault values
+      $addressDefaults = array();
+      foreach ($defaults as $key => $val) {
+        if (empty($val)) {
+          continue;
+        }
+
+        // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
+        // this works for all types custom data
+        $keyValues = explode('[', str_replace(']', '', $key));
+        $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
+      }
+
+      $form->setDefaults($addressDefaults);
+
+      // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
+      // And we can't set it to 'address_' because we want to set it in a slightly different format.
+      CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
+
+      // during contact editing : if no address is filled
+      // required custom data must not produce 'required' form rule error
+      // more handling done in formRule func
+      CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree);
+
+      $tplGroupTree = CRM_Core_Smarty::singleton()
+        ->get_template_vars('address_groupTree');
+      $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree;
+
+      $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree));
+      // unset the temp smarty var that got created
+      $form->assign('dnc_groupTree', NULL);
+    }
+    // address custom data processing ends ..
+  }
+
 }