Use shared parent formRule in custom import api
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 26 Aug 2022 04:35:28 +0000 (16:35 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 26 Aug 2022 04:35:28 +0000 (16:35 +1200)
CRM/Custom/Import/Form/MapField.php

index aa1391cb03e4228ff1852f2d519e3d3a952473ac..7a7b8726c62ab42a710aa0ff46b769ab49d9c8a3 100644 (file)
@@ -8,11 +8,10 @@ class CRM_Custom_Import_Form_MapField extends CRM_Import_Form_MapField {
   /**
    * Build the form object.
    *
-   * @return void
+   * @throws \CRM_Core_Exception
    */
-  public function buildQuickForm() {
-    $savedMappingID = (int) $this->getSubmittedValue('savedMapping');
-    $this->buildSavedMappingFields($savedMappingID);
+  public function buildQuickForm(): void {
+    $this->addSavedMappingFields();
     $this->addFormRule(['CRM_Custom_Import_Form_MapField', 'formRule']);
     $this->addMapper();
     $this->addFormButtons();
@@ -27,7 +26,7 @@ class CRM_Custom_Import_Form_MapField extends CRM_Import_Form_MapField {
    * @return array|bool
    *   list of errors to be posted back to the form
    */
-  public static function formRule($fields) {
+  public static function formRule(array $fields) {
     // todo - this could be shared with other mapFields forms.
     $errors = [];
     if (!array_key_exists('savedMapping', $fields)) {
@@ -44,37 +43,7 @@ class CRM_Custom_Import_Form_MapField extends CRM_Import_Form_MapField {
         $errors['_qf_default'] .= ts('Missing required field: %1', [1 => ts('Contact ID or External Identifier')]);
       }
     }
-
-    if (!empty($fields['saveMapping'])) {
-      $nameField = $fields['saveMappingName'] ?? NULL;
-      if (empty($nameField)) {
-        $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
-      }
-      else {
-        if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Multi value custom data'))) {
-          $errors['saveMappingName'] = ts('Duplicate Mapping Name');
-        }
-      }
-    }
-
-    //display Error if loaded mapping is not selected
-    if (array_key_exists('loadMapping', $fields)) {
-      $getMapName = $fields['savedMapping'] ?? NULL;
-      if (empty($getMapName)) {
-        $errors['savedMapping'] = ts('Select saved mapping');
-      }
-    }
-
-    if (!empty($errors)) {
-      if (!empty($errors['saveMappingName'])) {
-        $_flag = 1;
-        $assignError = new CRM_Core_Page();
-        $assignError->assign('mappingDetailsError', $_flag);
-      }
-      return $errors;
-    }
-
-    return TRUE;
+    return empty($errors) ? TRUE : $errors;
   }
 
   /**