From 35be577cf61367d9748d08cdbb705ceffc64a4db Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 27 Apr 2022 18:55:47 +0200 Subject: [PATCH] APIv4 - Move filtering of component entities higher up Filtering out component entities at a low level was causing errors when trying to get basic info about an entity from a disabled component. It's better to do the filtering in the Entity.get action instead. --- Civi/Api4/Action/Entity/Get.php | 8 ++++++-- Civi/Api4/Provider/ActionObjectProvider.php | 16 ++-------------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/Civi/Api4/Action/Entity/Get.php b/Civi/Api4/Action/Entity/Get.php index 9a8ccbb5f9..13b96573fe 100644 --- a/Civi/Api4/Action/Entity/Get.php +++ b/Civi/Api4/Action/Entity/Get.php @@ -28,11 +28,15 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { protected $includeCustom; /** - * Returns all APIv4 entities + * Returns all APIv4 entities from core, enabled components and enabled extensions. */ protected function getRecords() { $provider = \Civi::service('action_object_provider'); - return $provider->getEntities(); + return array_filter($provider->getEntities(), function($entity) { + // Only include DAO entities from enabled components + $daoName = $entity['dao'] ?? NULL; + return (!$daoName || !defined("{$daoName}::COMPONENT") || \CRM_Core_Component::isEnabled($daoName::COMPONENT)); + }); } } diff --git a/Civi/Api4/Provider/ActionObjectProvider.php b/Civi/Api4/Provider/ActionObjectProvider.php index 2c284e3cc0..4d6dda02df 100644 --- a/Civi/Api4/Provider/ActionObjectProvider.php +++ b/Civi/Api4/Provider/ActionObjectProvider.php @@ -152,7 +152,8 @@ class ActionObjectProvider implements EventSubscriberInterface, ProviderInterfac if (!$entities) { // Load entities declared in API files foreach ($this->getAllApiClasses() as $className) { - $this->loadEntity($className, $entities); + $info = $className::getInfo(); + $entities[$info['name']] = $info; } // Allow extensions to modify the list of entities $event = GenericHookEvent::create(['entities' => &$entities]); @@ -164,19 +165,6 @@ class ActionObjectProvider implements EventSubscriberInterface, ProviderInterfac return $entities; } - /** - * @param \Civi\Api4\Generic\AbstractEntity $className - * @param array $entities - */ - private function loadEntity($className, array &$entities) { - $info = $className::getInfo(); - $daoName = $info['dao'] ?? NULL; - // Only include DAO entities from enabled components - if (!$daoName || !defined("{$daoName}::COMPONENT") || \CRM_Core_Component::isEnabled($daoName::COMPONENT)) { - $entities[$info['name']] = $info; - } - } - /** * Scan all api directories to discover entities * @return \Civi\Api4\Generic\AbstractEntity[] -- 2.25.1