Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afform - add dynamic forms for creating/update custom group data #31484

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ufundo
Copy link
Contributor

@ufundo ufundo commented Nov 15, 2024

Overview

Currently afform blocks are auto generated for custom group data, which you can add to your own forms.

This adds automatic forms which can be used to edit the custom group data.

Comments

I've moved the form generation from the Afform/Get class to a dedicated action on CustomGroup entity, as it gets a bit more involved creating multiple forms for each Group.

Still a bit WIP.

Questions:

  • For multi record custom groups we have to wrangle with af-join - I think it could be nice if we could enable them to be the primary form entity (with a required entity_id field).
  • Currently I've implemented this directly in afform core - because that's where the blocks generation was, and it's nice to keep it together - but I do wonder if the forms should be in civicrm_admin_ui, because there are a bit more involved
  • Permissions: I think the forms should be safe as long at they use RBAC - but not sure exactly what the (default) form level permission should be

Next step:

  • Search forms to display multi value data. This requires creating SearchKits as well.

Copy link

civibot bot commented Nov 15, 2024

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@civibot civibot bot added the master label Nov 15, 2024
@ufundo
Copy link
Contributor Author

ufundo commented Nov 15, 2024

@colemanw just putting this up as WIP on the custom group afforms. Started on the create/update forms as they are a bit simpler, but have made some in-roads into the search listings too.

Got a bit bogged down in hook_civicrm_tabset and the afform implementation thereof though.

I wondered about just using an afform with a url to add the tabs directly, rather than via the afform placement tag?

e.g. the following basically seemed to work

/**
 * Implements hook_civicrm_tabset().
 *
 * Adds afforms as contact summary tabs.
 */
function civicrm_admin_ui_civicrm_tabset($tabsetName, &$tabs, $context) {
  if ($tabsetName !== 'civicrm/contact/view') {
    return;
  }

  $contactTypes = array_merge(['Contact'], \CRM_Contact_BAO_ContactType::basicTypes(FALSE));
  $customGroups = \Civi\Api4\CustomGroup::get(FALSE)
    ->addWhere('is_active', '=', TRUE)
    ->addWhere('style', '!=', 'Inline')
    ->addWhere('extends', 'IN', $contactTypes)
    // TODO: handle single value tabs
    ->addWhere('is_multiple', '=', TRUE)
    ->addSelect('name', 'style', 'extends', 'title', 'icon')
    ->execute();

  foreach ($customGroups as $group) {
    $tabId = 'custom_' . $group['id'];

    // check for old-style tab and remove it
    foreach ($tabs as $i => $tabMeta) {
      if ($tabMeta['id'] === $tabId) {
        unset($tabs[$i]);
        break;
      }
    }

    // only relevant for specific contact types, leave blank for all
    $contactType = ($group['extends'] !== 'Contact') ? $group['extends'] : NULL;
    $url = 'civicrm/contact/custom/' . $group['name'] . '/list#?contact_id=' . $context['contact_id'];
    $tabs[] = [
      'id' => $tabId,
      'title' => $group['title'],
      //'weight' => $afform['summary_weight'] ?? $weight++,
      'icon' => 'crm-i ' . ($group['icon'] ?: 'fa-list-alt'),
      'is_active' => TRUE,
      'contact_type' => $contactType,
      'url' => $url,
      //'template' => 'afform/contactSummary/AfformTab.tpl',
      //'module' => $afform['module_name'],
      //'directive' => $afform['directive_name'],
    ];
    // ensure the module required on that route is loaded
//    Civi::service('angularjs.loader')->addModules('afsearchCustom_' . $group['name']);
  }
}

I got got by the contact summary seemingly force the url to begin /civicrm/contact , so not happy with a generic url for a listing that works to any custom group (I had /civicrm/af/custom/GROUPNAME/list ).

*
* For single value custom groups we generate:
* - a field block with all of the fields from the group
* - a submission form for editing these fields on a parent entity record
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how this would work. Probably not needed unless I'm missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one does work already - because you can pass in the parent entity (e.g. contact id) to the primary af-entity

The multi value ones are trickier, because you want to be able to specify the id of the af-join element by param - but that doesn't work.

Hence my thinking to make the Custom_[abc] entity a primary entity for afform purposes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the multi-record custom groups will need to be treated as primary entities.

Comment on lines +16 to +17
* - a submission form for adding a new Custom record to a parent entity record
* - a submission form for editing a particular Custom record
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these 2 could be the same form actually.
entity_id could be a hidden field passed through the url.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes I see what you mean, good point

@colemanw
Copy link
Member

Got a bit bogged down in hook_civicrm_tabset and the afform implementation thereof though.
I wondered about just using an afform with a url to add the tabs directly, rather than via the afform placement tag?

I don't think you need to reinvent the wheel here. I can answer your questions about hook_civicrm_tabset. I'm sure it can already do what we want here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants