The functionality for autofilling the current user is unchanged, but now encapsulated in a Behavior.
--- /dev/null
+<?php
+namespace Civi\Afform\Behavior;
+
+use Civi\Afform\AbstractBehavior;
+use Civi\Afform\Event\AfformPrefillEvent;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use CRM_Afform_ExtensionUtil as E;
+
+class ContactAutofill extends AbstractBehavior implements EventSubscriberInterface {
+
+ /**
+ * @return array
+ */
+ public static function getSubscribedEvents() {
+ return [
+ 'civi.afform.prefill' => ['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]);
+ }
+ }
+ }
+ }
+
+}
$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);
}
}
- /**
- * 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', [
$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')) {
data: '=',
actions: '=',
modelName: '@name',
- label: '@',
- autofill: '@'
+ label: '@'
};
// Example usage: <af-form><af-entity name="Person" type="Contact" /> ... <fieldset af-fieldset="Person"> ... </fieldset></af-form>
angular.module('af').component('afEntity', {
<namespace>CRM/Afform</namespace>
</civix>
<classloader>
+ <psr0 prefix="CRM_" path=""/>
<psr4 prefix="Civi\" path="Civi"/>
<psr0 prefix="CRM_" path="."/>
</classloader>
<mixins>
<mixin>ang-php@1.0.0</mixin>
<mixin>mgd-php@1.1.0</mixin>
+ <mixin>scan-classes@1.0.0</mixin>
</mixins>
</extension>
--- /dev/null
+<?php
+namespace Civi\Afform;
+
+use Civi\Api4\AfformBehavior;
+use Civi\Test\HeadlessInterface;
+
+/**
+ * @group headless
+ */
+class AfformBehaviorTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface {
+
+ public function setUpHeadless() {
+ return \Civi\Test::headless()->installMe(__DIR__)->apply();
+ }
+
+ public function testGet() {
+ $autofill = AfformBehavior::get(FALSE)
+ ->addWhere('key', '=', 'autofill')
+ ->execute()->single();
+
+ $this->assertContains('user', array_column($autofill['modes']['Individual'], 'name'));
+ }
+
+}
$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()