From: Coleman Watts Date: Thu, 24 Feb 2022 02:59:44 +0000 (-0500) Subject: APIv4 - Get dynamic list of entity types X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=15244a15dc1e00ff1b1f264df02c2b8824a8951f;p=civicrm-core.git APIv4 - Get dynamic list of entity types Switches APIv4 Entity.get to dynamically fetch a list of entity types instead of using a hardcoded list. --- diff --git a/Civi/Api4/Entity.php b/Civi/Api4/Entity.php index 9fc1dd7348..b65738f434 100644 --- a/Civi/Api4/Entity.php +++ b/Civi/Api4/Entity.php @@ -35,7 +35,7 @@ class Entity extends Generic\AbstractEntity { * @return Generic\BasicGetFieldsAction */ public static function getFields($checkPermissions = TRUE) { - return (new Generic\BasicGetFieldsAction('Entity', __FUNCTION__, function() { + return (new Generic\BasicGetFieldsAction('Entity', __FUNCTION__, function(Generic\BasicGetFieldsAction $getFields) { return [ [ 'name' => 'name', @@ -53,15 +53,7 @@ class Entity extends Generic\AbstractEntity { 'name' => 'type', 'data_type' => 'Array', 'description' => 'Base class for this entity', - 'options' => [ - 'AbstractEntity' => 'AbstractEntity', - 'DAOEntity' => 'DAOEntity', - 'CustomValue' => 'CustomValue', - 'BasicEntity' => 'BasicEntity', - 'SortableEntity' => 'SortableEntity', - 'ManagedEntity' => 'ManagedEntity', - 'EntityBridge' => 'EntityBridge', - ], + 'options' => $getFields->getLoadOptions() ? self::getEntityTypes() : TRUE, ], [ 'name' => 'description', @@ -160,4 +152,20 @@ class Entity extends Generic\AbstractEntity { ]; } + /** + * Collect the 'type' values from every entity. + * + * @return array + */ + private static function getEntityTypes() { + $provider = \Civi::service('action_object_provider'); + $entityTypes = []; + foreach ($provider->getEntities() as $entity) { + foreach ($entity['type'] ?? [] as $type) { + $entityTypes[$type] = $type; + } + } + return $entityTypes; + } + }