Afform - Fix compatability of existing contact widget and 'current user' default
authorColeman Watts <coleman@civicrm.org>
Sun, 7 Aug 2022 01:32:14 +0000 (21:32 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 10 Aug 2022 02:28:44 +0000 (22:28 -0400)
ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php
ext/afform/core/ang/af/afField.component.js
ext/afform/core/ang/af/afForm.component.js

index 17c25b78f3542c119c782f1871bf7ca951dbccc5..fb58a574da75a67e082c04f8bc9dfc7221fb6738 100644 (file)
@@ -9,6 +9,9 @@ use Civi\Api4\Utils\CoreUtil;
 /**
  * Shared functionality for form submission pre & post processing.
  * @package Civi\Api4\Action\Afform
+ *
+ * @method $this setFillMode(string $fillMode) Set entity/form fill mode.
+ * @method string getFillMode()
  */
 abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction {
 
@@ -25,6 +28,13 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction {
    */
   protected $args = [];
 
+  /**
+   * Used by prefill action to indicate if the entire form or just one entity is being filled.
+   * @var string
+   * @options form,entity
+   */
+  protected $fillMode = 'form';
+
   /**
    * @var array
    */
@@ -79,7 +89,7 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction {
           $ids = array_slice($ids, 0, !empty($entity['af-repeat']) ? $entity['max'] ?? NULL : 1);
           $this->loadEntity($entity, $ids);
         }
-        elseif (!empty($entity['autofill'])) {
+        elseif (!empty($entity['autofill']) && $this->fillMode !== 'entity') {
           $this->autofillEntity($entity, $entity['autofill']);
         }
       }
index 1e0cda0dbc9f842246b0bad852804cb959ea23c8..63d0021f24788c7ca3d7ad12cfe6d1e8768eafc6 100644 (file)
         }
       };
 
+      // ngChange callback from Existing entity field
       ctrl.onSelectExisting = function() {
         var val = $scope.getSetSelect();
         var entity = ctrl.afFieldset.modelName;
index 314d710cc515f95b5114c969a53a64544c99b122..dbaa9c910490f4bc89f970ddaa47d6bd71e0777e 100644 (file)
         // Load single entity
         if (selectedEntity) {
           toLoad = selectedId;
+          params.fillMode = 'entity';
           params.args[selectedEntity] = {};
           params.args[selectedEntity][selectedIndex] = selectedId;
-        } else {
+        }
+        // Prefill entire form
+        else {
           args = _.assign({}, $scope.$parent.routeParams || {}, $scope.$parent.options || {});
           _.each(schema, function (entity, entityName) {
             if (args[entityName] || entity.autofill) {
               });
             });
         }
+        // Clear existing contact selection
+        else if (selectedEntity) {
+          data[selectedEntity][selectedIndex].fields = {};
+          if (data[selectedEntity][selectedIndex].joins) {
+            data[selectedEntity][selectedIndex].joins = {};
+          }
+        }
       };
 
       // Used when submitting file fields