Move getSubmittedValues to CRM_Core_Form
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 20 Mar 2023 04:17:04 +0000 (17:17 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 20 Mar 2023 04:27:08 +0000 (17:27 +1300)
CRM/Activity/Form/Activity.php
CRM/Core/Form.php
CRM/Import/Forms.php

index c2e24fdcdb5280d799aa737a9862487070aedc33..849264a1772eebd95a387ee2821c0233eb4070c6 100644 (file)
@@ -928,7 +928,10 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
 
     // store the submitted values in an array
     if (!$params) {
-      $params = $this->controller->exportValues($this->_name);
+      $params = $this->getSubmittedValues();
+    }
+    else {
+      CRM_Core_Error::deprecatedWarning('passing params into postProcess is deprecated. Match parent function');
     }
 
     // Set activity type id.
index 032c89b8d901c8522f017ac1a38027a4613b4baa..326fdb45f1f4674076949b3f17c11ce634f393ae 100644 (file)
@@ -205,6 +205,40 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
     return $this->context;
   }
 
+  /**
+   * Get values submitted by the user.
+   *
+   * Compared with $this->controller->exportValues this has a couple of changes
+   * 1) any fields declared in $this->submittableMoneyFields will be de-formatted first.
+   * 2) it is possible to store access fields from related forms if they
+   * are declared in `getSubmittableFields()`. This is notably used in imports
+   * to combine fields from the various screens & save the resulting 'submitted_values'
+   * to the UserJob.
+   *
+   * @return array
+   */
+  public function getSubmittedValues(): array {
+    $values = [];
+    foreach (array_keys($this->getSubmittableFields()) as $key) {
+      $values[$key] = $this->getSubmittedValue($key);
+    }
+    return $values;
+  }
+
+  /**
+   * Get the fields that can be submitted in this form flow.
+   *
+   * To make fields in related forms (ie within the same wizard like
+   * Contribution_Main and Contribution_Confirm) accessible you can override
+   * this function as CRM_Import_Forms does.
+   *
+   * @return string[]
+   */
+  protected function getSubmittableFields(): array {
+    $fieldNames = array_keys($this->controller->exportValues($this->_name));
+    return array_fill_keys($fieldNames, $this->_name);
+  }
+
   /**
    * Set context variable.
    */
index f7aec5cb26f5131dc9498a7f56b8b715721d9b03..10df71e5b74a13eaf4f00ef6d872b79262969fa9 100644 (file)
@@ -155,19 +155,6 @@ class CRM_Import_Forms extends CRM_Core_Form {
 
   }
 
-  /**
-   * Get values submitted on any form in the multi-page import flow.
-   *
-   * @return array
-   */
-  public function getSubmittedValues(): array {
-    $values = [];
-    foreach (array_keys($this->getSubmittableFields()) as $key) {
-      $values[$key] = $this->getSubmittedValue($key);
-    }
-    return $values;
-  }
-
   /**
    * Get the available datasource.
    *
@@ -206,7 +193,7 @@ class CRM_Import_Forms extends CRM_Core_Form {
    * 2) User changes the source to SQL - the ajax updates the html but the
    * form was built with the expectation that the csv-specific fields would be
    * required.
-   * 3) When the user submits Quickform calls preProcess and buildForm and THEN
+   * 3) When the user submits QuickForm calls preProcess and buildForm and THEN
    * retrieves the submitted values based on what has been added in buildForm.
    * Only the submitted values for fields added in buildForm are available - but
    * these have to be added BEFORE the submitted values are determined. Hence
@@ -316,6 +303,8 @@ class CRM_Import_Forms extends CRM_Core_Form {
    * This is called as a snippet in DataSourceConfig and
    * also from DataSource::buildForm to add the fields such
    * that quick form picks them up.
+   *
+   * @throws \CRM_Core_Exception
    */
   protected function getDataSourceFields(): array {
     $className = $this->getDataSourceClassName();