From 7d51ae3b79297d88d9fcae655822a3c8d0db00ad Mon Sep 17 00:00:00 2001 From: benjamin Date: Mon, 16 Dec 2024 20:54:37 +0000 Subject: [PATCH] add custom view-only afform --- .../Api4/Action/CustomGroup/GetAfforms.php | 56 ++++++++++++++++++- .../{afform.tpl => afformEdit.tpl} | 0 .../afform/customGroups/afformView.tpl | 22 ++++++++ .../Subscriber/CustomGroupEntityLinks.php | 1 + 4 files changed, 77 insertions(+), 2 deletions(-) rename ext/afform/core/templates/afform/customGroups/{afform.tpl => afformEdit.tpl} (100%) create mode 100644 ext/afform/core/templates/afform/customGroups/afformView.tpl diff --git a/ext/afform/core/Civi/Api4/Action/CustomGroup/GetAfforms.php b/ext/afform/core/Civi/Api4/Action/CustomGroup/GetAfforms.php index d611ce89c74..cf6c60c8fe0 100644 --- a/ext/afform/core/Civi/Api4/Action/CustomGroup/GetAfforms.php +++ b/ext/afform/core/Civi/Api4/Action/CustomGroup/GetAfforms.php @@ -26,6 +26,7 @@ class GetAfforms extends \Civi\Api4\Generic\BasicBatchAction { 'afblockCustom', 'afformUpdateCustom', 'afformCreateCustom', + 'afformViewCustom', 'afsearchTabCustom', ]; @@ -70,6 +71,7 @@ protected function doTask($item) { break; case 'form': + $forms[] = $this->generateViewForm($item); $forms[] = $this->generateUpdateForm($item); if ($item['is_multiple']) { $forms[] = $this->generateCreateForm($item); @@ -119,6 +121,56 @@ private function generateFieldBlock($item): array { return $afform; } + private function generateViewForm($item): array { + $afform = [ + 'name' => 'afformViewCustom_' . $item['name'], + 'type' => 'form', + 'title' => E::ts('View %1', [1 => $item['title']]), + 'description' => '', + 'is_public' => FALSE, + // NOTE: we will use RBAC for entities to ensure + // this form does not allow folks who shouldn't + // to edit contacts + 'permission' => ['access CiviCRM'], + 'server_route' => 'civicrm/af/custom/' . $item['name'] . '/view', + 'icon' => $item['icon'], + ]; + if ($this->getLayout) { + + // form entity depends on whether this is a multirecord custom group + $formEntity = $item['is_multiple'] ? + [ + 'type' => 'Custom_' . $item['name'], + 'name' => 'Record', + 'label' => $item['extends'] . ' ' . $item['title'], + 'parent_field' => 'entity_id', + 'parent_field_defn' => [ + 'input_type' => 'Hidden', + 'label' => FALSE, + ], + ] : + [ + 'type' => $item['extends'], + 'name' => $item['extends'] . '1', + 'label' => $item['extends'], + 'parent_field' => 'id', + 'parent_field_defn' => [ + 'input_type' => 'Hidden', + 'label' => FALSE, + ], + ]; + + $afform['layout'] = \CRM_Core_Smarty::singleton()->fetchWith( + 'afform/customGroups/afformView.tpl', + [ + 'formEntity' => $formEntity, + 'group' => $item, + ] + ); + } + return $afform; + } + private function generateUpdateForm($item): array { $afform = [ 'name' => 'afformUpdateCustom_' . $item['name'], @@ -159,7 +211,7 @@ private function generateUpdateForm($item): array { ]; $afform['layout'] = \CRM_Core_Smarty::singleton()->fetchWith( - 'afform/customGroups/afform.tpl', + 'afform/customGroups/afformEdit.tpl', [ 'formEntity' => $formEntity, 'formActions' => [ @@ -200,7 +252,7 @@ private function generateCreateForm($item): array { ], ]; $afform['layout'] = \CRM_Core_Smarty::singleton()->fetchWith( - 'afform/customGroups/afform.tpl', + 'afform/customGroups/afformEdit.tpl', [ 'formEntity' => $formEntity, 'formActions' => [ diff --git a/ext/afform/core/templates/afform/customGroups/afform.tpl b/ext/afform/core/templates/afform/customGroups/afformEdit.tpl similarity index 100% rename from ext/afform/core/templates/afform/customGroups/afform.tpl rename to ext/afform/core/templates/afform/customGroups/afformEdit.tpl diff --git a/ext/afform/core/templates/afform/customGroups/afformView.tpl b/ext/afform/core/templates/afform/customGroups/afformView.tpl new file mode 100644 index 00000000000..783dbd22e04 --- /dev/null +++ b/ext/afform/core/templates/afform/customGroups/afformView.tpl @@ -0,0 +1,22 @@ + + + +
+ + {foreach from=$group.field_names item=field_name} + {* for multiple record fields there is no need to prepend + the group name because it will be the form entity itself *} + + {/foreach} +
+
diff --git a/ext/civicrm_admin_ui/Civi/Api4/Event/Subscriber/CustomGroupEntityLinks.php b/ext/civicrm_admin_ui/Civi/Api4/Event/Subscriber/CustomGroupEntityLinks.php index 246ce6e8712..704cd4762fe 100644 --- a/ext/civicrm_admin_ui/Civi/Api4/Event/Subscriber/CustomGroupEntityLinks.php +++ b/ext/civicrm_admin_ui/Civi/Api4/Event/Subscriber/CustomGroupEntityLinks.php @@ -34,6 +34,7 @@ public function addCustomGroupLinks(\Civi\Core\Event\GenericHookEvent $event) { $groupName = substr($name, 7); $event->entities[$name]['paths']['add'] = "civicrm/af/custom/{$groupName}/create#?entity_id=[entity_id]"; $event->entities[$name]['paths']['update'] = "civicrm/af/custom/{$groupName}/update#?Record=[id]"; + $event->entities[$name]['paths']['view'] = "civicrm/af/custom/{$groupName}/view#?Record=[id]"; } } }