-
Notifications
You must be signed in to change notification settings - Fork 0
CIVIPLMMSR-623: Fix entity type registration on CiviCRM 5.75 #23
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Auto-register entity declarations from `schema/*.entityType.php`. | ||
| * | ||
| * @mixinName entity-types-php | ||
| * @mixinVersion 2.0.0 | ||
| * @since 5.73 | ||
| * | ||
| * Changelog: | ||
| * - v2.0 scans /schema directory instead of /xml/schema/* | ||
| * - v2.0 supports only one entity per file | ||
| * - v2.0 adds 'module' key to each entity | ||
| * | ||
| * @param CRM_Extension_MixInfo $mixInfo | ||
| * On newer deployments, this will be an instance of MixInfo. On older deployments, Civix may polyfill with a work-a-like. | ||
| * @param \CRM_Extension_BootCache $bootCache | ||
| * On newer deployments, this will be an instance of BootCache. On older deployments, Civix may polyfill with a work-a-like. | ||
| */ | ||
| return function ($mixInfo, $bootCache) { | ||
|
|
||
| /** | ||
| * @param \Civi\Core\Event\GenericHookEvent $e | ||
| * @see CRM_Utils_Hook::entityTypes() | ||
| */ | ||
| Civi::dispatcher()->addListener('hook_civicrm_entityTypes', function ($e) use ($mixInfo) { | ||
| // When deactivating on a polyfill/pre-mixin system, listeners may not cleanup automatically. | ||
| if (!$mixInfo->isActive() || !is_dir($mixInfo->getPath('schema'))) { | ||
| return; | ||
| } | ||
|
|
||
| $files = (array) glob($mixInfo->getPath('schema/*.entityType.php')); | ||
| foreach ($files as $file) { | ||
| $entity = include $file; | ||
| $entity['module'] = $mixInfo->longName; | ||
| $e->entityTypes[$entity['name']] = $entity; | ||
| } | ||
| }); | ||
|
|
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,35 @@ function paymentprocessingcore_civicrm_enable(): void { | |
| _paymentprocessingcore_civix_civicrm_enable(); | ||
| } | ||
|
|
||
| /** | ||
| * Implements hook_civicrm_entityTypes(). | ||
| * | ||
| * Directly registers entity types to work around a mixin timing issue on | ||
| * CiviCRM 5.75: the entity-types-php@2 mixin is only loaded for installed | ||
| * extensions, so during installation the mixin listener is absent and | ||
| * EntityRepository cannot find our entities. This direct hook ensures entity | ||
| * types are always registered. When both this and the mixin fire, the result | ||
| * is identical (same data, same keys). | ||
| * | ||
| * @see https://compucorp.atlassian.net/browse/CIVIPLMMSR-623 | ||
| * | ||
| * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes | ||
| */ | ||
| function paymentprocessingcore_civicrm_entityTypes(&$entityTypes): void { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the array type hint to the $entityTypes parameter. This ensures compliance with PHPStan level 9 requirements for proper typing and improves code clarity by explicitly defining the expected type for the reference. function paymentprocessingcore_civicrm_entityTypes(array &$entityTypes): void {References
|
||
| $schemaDir = __DIR__ . '/schema'; | ||
| if (!is_dir($schemaDir)) { | ||
| return; | ||
| } | ||
| $files = (array) glob($schemaDir . '/*.entityType.php'); | ||
| foreach ($files as $file) { | ||
|
Comment on lines
+54
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Casting the result of glob() directly to (array) is problematic because glob() returns false on failure. In PHP, (array) false results in [false], which causes the loop to execute once with an invalid filename, leading to a warning during the include call. Using the null coalescing operator ?: [] is a safer way to handle potential failures. foreach (glob($schemaDir . '/*.entityType.php') ?: [] as $file) { |
||
| $entity = include $file; | ||
| if (is_array($entity) && !empty($entity['name'])) { | ||
| $entity['module'] = E::LONG_NAME; | ||
| $entityTypes[$entity['name']] = $entity; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Implements hook_civicrm_container(). | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The glob() result should be handled safely to avoid issues if it returns false. Additionally, adding validation for the $entity variable (ensuring it is an array and contains a name) improves robustness and maintains consistency with the implementation in paymentprocessingcore.php. This prevents potential errors if a file in the schema directory is malformed or does not return the expected array.
References