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

Switch eck_sub_types OptionGroup to a managed entity #32

Merged
merged 3 commits into from
Mar 4, 2022

Conversation

colemanw
Copy link
Collaborator

@colemanw colemanw commented Mar 3, 2022

This answers @jensschuppe's question about how to create an option group using the CiviCRM Managed system.

NOTES:

  • Placing it in a .mgd.php file is standard. Use hook_civicrm_managed for "custom group extends" optionValues #30 worked directly in the hook code because it needed some logic to loop through the entitiy types. But normally the hook just scans this directory for .mgd.php files and returns their contents.
  • Before this PR I think the optionGroup was getting created on install, but I don't see anywhere it was getting deleted on uninstall. This PR fixes that, because I set cleanup => always so it will get auto-deleted on uninstall.
  • After this change the CRM_Eck_CustomData class is no longer used so I removed it. I assume it was copied over from another extension because it's quite large.
  • As usual, the only tricky part about this is managing upgrades. Once again this will crash the upgrader unless you manually delete the existing record. But again, there's only one site in the world using this extension, so maybe that's not too hard to do?
  • I autogenerated the contents of the .mgd.php file with the API Explorer. It's pretty cool:

image

@colemanw
Copy link
Collaborator Author

colemanw commented Mar 3, 2022

This core patch should help make this sort of upgrade problem much easier in the future: civicrm/civicrm-core#22883

@jensschuppe jensschuppe added enhancement New feature or request status:needs review Code needs review and testing labels Mar 4, 2022
@jensschuppe
Copy link
Collaborator

Sorry for 92fc980 and 89dafe1 - explicitly creating managed entity records doesn't seems robust enough if extension upgrades are not running before a cache clear / menu rebuild that causes managed entities be reconciled. I'll merge this unchanged.

@jensschuppe jensschuppe merged commit d48f75e into systopia:master Mar 4, 2022
@colemanw
Copy link
Collaborator Author

colemanw commented Mar 4, 2022

@jensschuppe right, unfortunately that doesn't work because the managed reconciliation happens before the upgrader runs.
I'm not quite sure why. But I think civicrm/civicrm-core#22883 is the best solution in the future.

@colemanw colemanw deleted the managedSubTypeGroup branch March 4, 2022 13:57
@jensschuppe
Copy link
Collaborator

jensschuppe commented Mar 4, 2022

This is what needs to be done before reconciling managed entities with this PR:

// Create managed entity for previously unmanaged eck_sub_types OptionGroup.
\Civi\Api4\Managed::create(FALSE)
  ->setValues([
                'module' => 'de.systopia.eck',
                'name' => 'OptionGroup_eck_sub_types',
                'cleanup' => 'always',
                'entity_type' => 'OptionGroup',
                'entity_id' => \Civi\Api4\OptionGroup::get(FALSE)
                  ->addSelect('id')
                  ->addWhere('name', '=', 'eck_sub_types')
                  ->execute()
                  ->single()['id'],
              ])
  ->execute();

@jensschuppe
Copy link
Collaborator

It depends on how you update the extension. Just dropping in the new code and running upgrades manually via the UI doesn't cause managed entities be reconciled until a manually triggered cache clear / menu rebuild. I'm not sure about cv though.

That might still be a problem causing managed entities not being created when they're missing (i.e. come new with an update or in a more complicated scenario as ours). But that's a thing for core, I guess. @colemanw do you know when managed entities are being reconciled in the first place?

@colemanw
Copy link
Collaborator Author

colemanw commented Mar 4, 2022

My understanding is that managed entities get reconciled every time you do a full cache-clear, and at the end of every upgrade. I'm not sure why it's been happening for me before the upgrade runs (I've been using cv). This extension also triggers it manually whenever an entityType is saved or deleted:

de.systopia.eck/eck.php

Lines 279 to 281 in 2d3756d

// Refresh managed entities which are autogenerated based on EckEntities
// @see eck_civicrm_managed()
\CRM_Core_ManagedEntities::singleton()->reconcile();

@jensschuppe jensschuppe added status:fixed The issue has been resolved (usually by committing/merging code) and removed status:needs review Code needs review and testing labels Mar 4, 2022
@jensschuppe jensschuppe added this to the 1.0 milestone Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request status:fixed The issue has been resolved (usually by committing/merging code)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants