Skip to content

Commit

Permalink
add custom view-only afform
Browse files Browse the repository at this point in the history
  • Loading branch information
ufundo committed Dec 17, 2024
1 parent f6a64d1 commit 7d51ae3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
56 changes: 54 additions & 2 deletions ext/afform/core/Civi/Api4/Action/CustomGroup/GetAfforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GetAfforms extends \Civi\Api4\Generic\BasicBatchAction {
'afblockCustom',
'afformUpdateCustom',
'afformCreateCustom',
'afformViewCustom',
'afsearchTabCustom',
];

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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' => [
Expand Down
22 changes: 22 additions & 0 deletions ext/afform/core/templates/afform/customGroups/afformView.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<af-form ctrl="afform">
<af-entity
type="{$formEntity.type}"
name="{$formEntity.name}"
label="{$formEntity.label}"
actions='{ldelim}create: false, update: true{rdelim}'
security="RBAC"
url-autofill="1"
/>

<fieldset af-fieldset="{$formEntity.name}" class="af-container">
<af-field
name="{$formEntity.parent_field}"
defn='{$formEntity.parent_field_defn|@json_encode}'
/>
{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 *}
<af-field name="{if !$group.is_multiple}{$group.name}.{/if}{$field_name}" defn="{ldelim}input_type: 'DisplayOnly'{rdelim}" />
{/foreach}
</fieldset>
</af-form>
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
}
}
}
Expand Down

0 comments on commit 7d51ae3

Please sign in to comment.