load the data from submission tableif submission id is passed
authorKurund Jalmi <kurundjalmi@thirdsectordesign.org>
Fri, 18 Aug 2023 12:17:02 +0000 (13:17 +0100)
committerKurund Jalmi <kurundjalmi@thirdsectordesign.org>
Wed, 6 Dec 2023 00:23:33 +0000 (00:23 +0000)
ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php

index cc96c5851d5f0501818830bb7f70eabd9a7daf96..bbd741f7e78c5c89e59c34950d7b6615a04f36a3 100644 (file)
@@ -86,6 +86,13 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction {
     $sorter = new AfformEntitySortEvent($this->_afform, $this->_formDataModel, $this);
     \Civi::dispatcher()->dispatch('civi.afform.sort.prefill', $sorter);
     $sortedEntities = $sorter->getSortedEnties();
+
+    // if submission id is passed then we should display the submission data
+    if (!empty($this->args['sid'])) {
+      $this->prePopulateSubmissionData($sortedEntities);
+      return;
+    }
+
     foreach ($sortedEntities as $entityName) {
       $entity = $this->_formDataModel->getEntity($entityName);
       $this->_entityIds[$entityName] = [];
@@ -105,6 +112,25 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction {
     }
   }
 
+  /**
+   * Load the data from submission table
+   */
+  protected function prePopulateSubmissionData($sortedEntities) {
+    // if submission id is passed then get the data from submission
+    $afformSubmissionData = \Civi\Api4\AfformSubmission::get(TRUE)
+      ->addSelect('data')
+      ->addWhere('id', '=', $this->args['sid'])
+      ->execute()->first();
+
+    foreach ($sortedEntities as $entityName) {
+      foreach ($afformSubmissionData['data'] as $entity => $data) {
+        if ($entity == $entityName) {
+          $this->_entityValues[$entityName] = $data;
+        }
+      }
+    }
+  }
+
   /**
    * Fetch all data needed to display a given entity on this form
    *