From 3d37a2df4a473a63da0d1b630d7cef1b1e378eab Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 18 Sep 2022 15:29:44 -0400 Subject: [PATCH] Afform - Convert Contact Autofill to a Behavior The functionality for autofilling the current user is unchanged, but now encapsulated in a Behavior. --- .../Civi/Afform/Behavior/ContactAutofill.php | 60 +++++++++++++++++++ .../Api4/Action/Afform/AbstractProcessor.php | 21 ------- ext/afform/core/afform.php | 1 + ext/afform/core/ang/af/afEntity.component.js | 3 +- ext/afform/core/info.xml | 2 + .../Civi/Afform/AfformBehaviorTest.php | 24 ++++++++ .../phpunit/api/v4/AfformContactUsageTest.php | 1 + 7 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 ext/afform/core/Civi/Afform/Behavior/ContactAutofill.php create mode 100644 ext/afform/core/tests/phpunit/Civi/Afform/AfformBehaviorTest.php diff --git a/ext/afform/core/Civi/Afform/Behavior/ContactAutofill.php b/ext/afform/core/Civi/Afform/Behavior/ContactAutofill.php new file mode 100644 index 0000000000..31feeef9f2 --- /dev/null +++ b/ext/afform/core/Civi/Afform/Behavior/ContactAutofill.php @@ -0,0 +1,60 @@ + ['onAfformPrefill', 99], + ]; + } + + public static function getEntities():array { + return ['Individual']; + } + + public static function getTitle():string { + return E::ts('Autofill'); + } + + public static function getKey():string { + // Would be contact-autofill but this supports legacy afforms from before this was converted to a behavior + return 'autofill'; + } + + public static function getDescription():string { + return E::ts('Automatically identify this contact'); + } + + public static function getModes(string $entityName):array { + $modes = []; + $modes[] = [ + 'name' => 'user', + 'label' => E::ts('Current User'), + ]; + return $modes; + } + + public static function onAfformPrefill(AfformPrefillEvent $event) { + if ($event->getEntityType() === 'Contact') { + $entity = $event->getEntity(); + $id = $event->getEntityId(); + // Autofill with current user + if (!$id && ($entity['autofill'] ?? NULL) === 'user') { + $id = \CRM_Core_Session::getLoggedInContactID(); + if ($id) { + $event->getApiRequest()->loadEntity($entity, [$id]); + } + } + } + } + +} diff --git a/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php b/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php index 323f3e7323..109e85ba86 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php @@ -90,9 +90,6 @@ 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']) && $this->fillMode !== 'entity') { - $this->autofillEntity($entity, $entity['autofill']); - } } $event = new AfformPrefillEvent($this->_afform, $this->_formDataModel, $this, $entity['type'], $entityName, $this->_entityIds); \Civi::dispatcher()->dispatch('civi.afform.prefill', $event); @@ -140,24 +137,6 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction { } } - /** - * Fetch an entity based on its autofill settings - * - * @param $entity - * @param $mode - */ - private function autoFillEntity($entity, $mode) { - $id = NULL; - if ($entity['type'] == 'Contact') { - if ($mode == 'user') { - $id = \CRM_Core_Session::getLoggedInContactID(); - } - } - if ($id) { - $this->loadEntity($entity, [$id]); - } - } - private function validateBySavedSearch($entity, array $ids) { $idField = CoreUtil::getIdFieldName($entity['type']); $fetched = civicrm_api4($entity['type'], 'autocomplete', [ diff --git a/ext/afform/core/afform.php b/ext/afform/core/afform.php index f2ad086b76..c06facd1e8 100644 --- a/ext/afform/core/afform.php +++ b/ext/afform/core/afform.php @@ -57,6 +57,7 @@ function afform_civicrm_config(&$config) { $dispatcher->addListener('hook_civicrm_check', ['\Civi\Afform\StatusChecks', 'hook_civicrm_check']); $dispatcher->addListener('civi.afform.get', ['\Civi\Api4\Action\Afform\Get', 'getCustomGroupBlocks']); $dispatcher->addSubscriber(new \Civi\Api4\Subscriber\AutocompleteSubscriber()); + $dispatcher->addSubscriber(new \Civi\Afform\Behavior\ContactAutofill()); // Register support for email tokens if (CRM_Extension_System::singleton()->getMapper()->isActiveModule('authx')) { diff --git a/ext/afform/core/ang/af/afEntity.component.js b/ext/afform/core/ang/af/afEntity.component.js index c778856ff2..8178e2b313 100644 --- a/ext/afform/core/ang/af/afEntity.component.js +++ b/ext/afform/core/ang/af/afEntity.component.js @@ -6,8 +6,7 @@ data: '=', actions: '=', modelName: '@name', - label: '@', - autofill: '@' + label: '@' }; // Example usage: ...
...
angular.module('af').component('afEntity', { diff --git a/ext/afform/core/info.xml b/ext/afform/core/info.xml index 9f3d9b79b1..276756a00e 100644 --- a/ext/afform/core/info.xml +++ b/ext/afform/core/info.xml @@ -23,6 +23,7 @@ CRM/Afform + @@ -32,5 +33,6 @@ ang-php@1.0.0 mgd-php@1.1.0 + scan-classes@1.0.0 diff --git a/ext/afform/core/tests/phpunit/Civi/Afform/AfformBehaviorTest.php b/ext/afform/core/tests/phpunit/Civi/Afform/AfformBehaviorTest.php new file mode 100644 index 0000000000..9f5fc6c282 --- /dev/null +++ b/ext/afform/core/tests/phpunit/Civi/Afform/AfformBehaviorTest.php @@ -0,0 +1,24 @@ +installMe(__DIR__)->apply(); + } + + public function testGet() { + $autofill = AfformBehavior::get(FALSE) + ->addWhere('key', '=', 'autofill') + ->execute()->single(); + + $this->assertContains('user', array_column($autofill['modes']['Individual'], 'name')); + } + +} diff --git a/ext/afform/mock/tests/phpunit/api/v4/AfformContactUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/AfformContactUsageTest.php index b3219d514d..4d39f29771 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/AfformContactUsageTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/AfformContactUsageTest.php @@ -69,6 +69,7 @@ EOHTML; $cid = $this->createLoggedInUser(); CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp(); + // Autofill form with current user. See `Civi\Afform\Behavior\ContactAutofill` $prefill = Civi\Api4\Afform::prefill() ->setName($this->formName) ->execute() -- 2.25.1