From b27094b76a14098e64235daae04e39b9c4d563bf Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 28 Feb 2022 13:19:47 -0500 Subject: [PATCH] APIv4 - Use new class_args metadata to remove special handing for CustomValue and CiviCase entities. Before: When creating an API request, special logic was needed for CustomValue and CiviCase apis. After: Metadata used. --- .../CreateApi4RequestSubscriber.php | 25 ++++++++----------- Civi/Api4/Provider/CustomEntityProvider.php | 8 +++--- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Civi/Api4/Event/Subscriber/CreateApi4RequestSubscriber.php b/Civi/Api4/Event/Subscriber/CreateApi4RequestSubscriber.php index 519498efd0..505dc5f9e2 100644 --- a/Civi/Api4/Event/Subscriber/CreateApi4RequestSubscriber.php +++ b/Civi/Api4/Event/Subscriber/CreateApi4RequestSubscriber.php @@ -12,7 +12,6 @@ namespace Civi\Api4\Event\Subscriber; use Civi\API\Events; -use Civi\Api4\Utils\CoreUtil; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** @@ -37,20 +36,18 @@ class CreateApi4RequestSubscriber implements EventSubscriberInterface { * @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'] ?? []; } } diff --git a/Civi/Api4/Provider/CustomEntityProvider.php b/Civi/Api4/Provider/CustomEntityProvider.php index 11b324cd7f..c24a707968 100644 --- a/Civi/Api4/Provider/CustomEntityProvider.php +++ b/Civi/Api4/Provider/CustomEntityProvider.php @@ -13,7 +13,6 @@ namespace Civi\Api4\Provider; use Civi\Api4\CustomValue; use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable; -use Civi\Api4\Utils\CoreUtil; use Civi\Core\Event\GenericHookEvent; class CustomEntityProvider { @@ -30,14 +29,17 @@ class CustomEntityProvider { $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", ], -- 2.25.1