Skip to content

Commit

Permalink
Merge pull request #22853 from colemanw/api4CreateEvent
Browse files Browse the repository at this point in the history
[REF] APIv4 - Use new class_args metadata to remove special handing
  • Loading branch information
colemanw authored Mar 7, 2022
2 parents 20fdeee + b27094b commit f5e5ea2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
25 changes: 11 additions & 14 deletions Civi/Api4/Event/Subscriber/CreateApi4RequestSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Civi\Api4\Event\Subscriber;

use Civi\API\Events;
use Civi\Api4\Utils\CoreUtil;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand All @@ -37,20 +36,18 @@ public static function getSubscribedEvents() {
* @param \Civi\Api4\Event\CreateApi4RequestEvent $event
*/
public function onApiRequestCreate(\Civi\Api4\Event\CreateApi4RequestEvent $event) {
// Multi-record custom data entities
if (strpos($event->entityName, 'Custom_') === 0) {
$groupName = substr($event->entityName, 7);
if (CoreUtil::isCustomEntity($groupName)) {
$event->className = 'Civi\Api4\CustomValue';
$event->args = [$groupName];
}
// Most entities match the name of the class
$className = 'Civi\Api4\\' . $event->entityName;
if (class_exists($className)) {
$event->className = $className;
return;
}
else {
// Because "Case" is a reserved php keyword
$className = 'Civi\Api4\\' . ($event->entityName === 'Case' ? 'CiviCase' : $event->entityName);
if (class_exists($className)) {
$event->className = $className;
}
// Lookup non-standard entities requiring arguments or with a mismatched classname
$provider = \Civi::service('action_object_provider');
$info = $provider->getEntities()[$event->entityName] ?? NULL;
if ($info) {
$event->className = $info['class'];
$event->args = $info['class_args'] ?? [];
}
}

Expand Down
8 changes: 5 additions & 3 deletions Civi/Api4/Provider/CustomEntityProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Civi\Api4\CustomValue;
use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable;
use Civi\Api4\Utils\CoreUtil;
use Civi\Core\Event\GenericHookEvent;

class CustomEntityProvider {
Expand All @@ -30,14 +29,17 @@ public static function addCustomEntities(GenericHookEvent $e) {
$group = \CRM_Core_DAO::executeQuery($select);
while ($group->fetch()) {
$entityName = 'Custom_' . $group->name;
$baseEntity = CoreUtil::getApiClass(CustomGroupJoinable::getEntityFromExtends($group->extends));
$baseEntity = CustomGroupJoinable::getEntityFromExtends($group->extends);
// Lookup base entity info using DAO methods not CoreUtil to avoid early-bootstrap issues
$baseEntityDao = \CRM_Core_DAO_AllCoreTables::getFullName($baseEntity);
$baseEntityTitle = $baseEntityDao ? $baseEntityDao::getEntityTitle(TRUE) : $baseEntity;
$e->entities[$entityName] = [
'name' => $entityName,
'title' => $group->title,
'title_plural' => $group->title,
'table_name' => $group->table_name,
'class_args' => [$group->name],
'description' => ts('Custom group for %1', [1 => $baseEntity::getInfo()['title_plural']]),
'description' => ts('Custom group for %1', [1 => $baseEntityTitle]),
'paths' => [
'view' => "civicrm/contact/view/cd?reset=1&gid={$group->id}&recId=[id]&multiRecordDisplay=single",
],
Expand Down

0 comments on commit f5e5ea2

Please sign in to comment.