Skip to content

Commit

Permalink
Merge pull request #29771 from colemanw/entityTypeMixin
Browse files Browse the repository at this point in the history
Add mixin entity-types-php v2
  • Loading branch information
totten authored Mar 21, 2024
2 parents 79931b2 + 5d5c59c commit 1c9bb16
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions mixin/entity-types-php@2/mixin.php
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;
}
});

};

0 comments on commit 1c9bb16

Please sign in to comment.