From e2c263c427119a6cc40f45e67898fa71240d57bf Mon Sep 17 00:00:00 2001 From: colemanw Date: Sat, 12 Aug 2023 15:09:24 -0400 Subject: [PATCH] Afform - Enable creating event from a template This expands autofill functionality to work with create actions in addition to update. --- .../AutocompleteFieldSubscriber.php | 2 +- .../EventAutocompleteProvider.php | 62 +++++++++++++++++++ .../Civi/AfformAdmin/AfformAdminMeta.php | 3 +- .../afGuiEditor/elements/afGuiField-menu.html | 4 +- .../Civi/Afform/AfformMetadataInjector.php | 2 +- ext/afform/core/Civi/Afform/FormDataModel.php | 8 ++- .../Api4/Action/Afform/AbstractProcessor.php | 14 ++--- .../core/Civi/Api4/Action/Afform/Prefill.php | 25 +++++++- .../AfformAutocompleteSubscriber.php | 2 +- ext/afform/core/ang/af/afField.component.js | 8 +-- ext/afform/core/ang/af/afForm.component.js | 6 +- .../Provider/EventCreationSpecProvider.php | 11 ++-- 12 files changed, 116 insertions(+), 31 deletions(-) create mode 100644 Civi/Api4/Service/Autocomplete/EventAutocompleteProvider.php diff --git a/Civi/Api4/Event/Subscriber/AutocompleteFieldSubscriber.php b/Civi/Api4/Event/Subscriber/AutocompleteFieldSubscriber.php index 6afc71a2ed..a832c9c18b 100644 --- a/Civi/Api4/Event/Subscriber/AutocompleteFieldSubscriber.php +++ b/Civi/Api4/Event/Subscriber/AutocompleteFieldSubscriber.php @@ -26,7 +26,7 @@ class AutocompleteFieldSubscriber extends AutoService implements EventSubscriber */ public static function getSubscribedEvents() { return [ - 'civi.api.prepare' => ['onApiPrepare', -50], + 'civi.api.prepare' => ['onApiPrepare', 150], ]; } diff --git a/Civi/Api4/Service/Autocomplete/EventAutocompleteProvider.php b/Civi/Api4/Service/Autocomplete/EventAutocompleteProvider.php new file mode 100644 index 0000000000..43a00c424f --- /dev/null +++ b/Civi/Api4/Service/Autocomplete/EventAutocompleteProvider.php @@ -0,0 +1,62 @@ + ['onApiPrepare', 140], + 'civi.search.defaultDisplay' => ['alterDefaultDisplay', Events::W_LATE], + ]; + } + + /** + * Add is_template filter to event template autocompletes + * @param \Civi\API\Event\PrepareEvent $event + */ + public function onApiPrepare(\Civi\API\Event\PrepareEvent $event): void { + $apiRequest = $event->getApiRequest(); + if (is_object($apiRequest) && is_a($apiRequest, 'Civi\Api4\Generic\AutocompleteAction')) { + [$entityName, $fieldName] = array_pad(explode('.', (string) $apiRequest->getFieldName(), 2), 2, ''); + + if ($entityName === 'Event' && $fieldName === 'template_id') { + $apiRequest->addFilter('is_template', TRUE); + } + } + } + + /** + * Alter default display of events based on the is_template filter. + * + * @param \Civi\Core\Event\GenericHookEvent $e + */ + public static function alterDefaultDisplay(GenericHookEvent $e) { + if ($e->display['type'] !== 'autocomplete' || $e->savedSearch['api_entity'] !== 'Event') { + return; + } + $filters = $e->context['filters'] ?? []; + if (!empty($filters['is_template'])) { + $e->display['settings']['columns'][0]['key'] = 'template_title'; + } + } + +} diff --git a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php index e788dfa74f..cf283b74a7 100644 --- a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php +++ b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php @@ -129,7 +129,8 @@ class AfformAdminMeta { $idField = CoreUtil::getIdFieldName($entityName); $fields[$idField]['readonly'] = FALSE; $fields[$idField]['input_type'] = 'EntityRef'; - $fields[$idField]['is_id'] = TRUE; + // Afform-only (so far) metadata tells the form to update an existing entity autofilled from this value + $fields[$idField]['input_attrs']['autofill'] = 'update'; $fields[$idField]['fk_entity'] = $entityName; $fields[$idField]['label'] = E::ts('Existing %1', [1 => CoreUtil::getInfoItem($entityName, 'title')]); // Mix in alterations declared by afform entities diff --git a/ext/afform/admin/ang/afGuiEditor/elements/afGuiField-menu.html b/ext/afform/admin/ang/afGuiEditor/elements/afGuiField-menu.html index 7e729c1842..9c49cbcccd 100644 --- a/ext/afform/admin/ang/afGuiEditor/elements/afGuiField-menu.html +++ b/ext/afform/admin/ang/afGuiEditor/elements/afGuiField-menu.html @@ -1,4 +1,4 @@ -
  • +