4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace Civi\Api4\Action\Entity
;
22 use Civi\Api4\CustomGroup
;
23 use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable
;
26 * Get the names & docblocks of all APIv4 entities.
28 * Scans for api entities in core + enabled extensions.
30 * Also includes pseudo-entities from multi-record custom groups by default.
32 * @method $this setIncludeCustom(bool $value)
33 * @method bool getIncludeCustom()
35 class Get
extends \Civi\Api4\Generic\BasicGetAction
{
38 * Include custom-field-based pseudo-entities?
42 protected $includeCustom = TRUE;
45 * Scan all api directories to discover entities
47 protected function getRecords() {
49 $toGet = $this->_itemsToGet('name');
50 $locations = array_merge([\Civi
::paths()->getPath('[civicrm.root]/Civi.php')],
51 array_column(\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
53 foreach ($locations as $location) {
54 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($location)) . 'Civi/Api4';
56 foreach (glob("$dir/*.php") as $file) {
58 preg_match('/(\w*)\.php$/', $file, $matches);
59 $entity = '\Civi\Api4\\' . $matches[1];
61 (!$toGet ||
in_array($matches[1], $toGet))
62 && is_a($entity, '\Civi\Api4\Generic\AbstractEntity', TRUE)
64 $info = $entity::getInfo();
65 $entities[$info['name']] = $info;
71 // Fetch custom entities unless we've already fetched everything requested
72 if ($this->includeCustom
&& (!$toGet ||
array_diff($toGet, array_keys($entities)))) {
73 $this->addCustomEntities($entities);
81 * Add custom-field pseudo-entities
84 * @throws \API_Exception
86 private function addCustomEntities(&$entities) {
87 $customEntities = CustomGroup
::get()
88 ->addWhere('is_multiple', '=', 1)
89 ->addWhere('is_active', '=', 1)
90 ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends', 'icon'])
91 ->setCheckPermissions(FALSE)
93 foreach ($customEntities as $customEntity) {
94 $fieldName = 'Custom_' . $customEntity['name'];
95 $baseEntity = '\Civi\Api4\\' . CustomGroupJoinable
::getEntityFromExtends($customEntity['extends']);
96 $entities[$fieldName] = [
98 'title' => $customEntity['title'],
99 'title_plural' => $customEntity['title'],
100 'description' => ts('Custom group for %1', [1 => $baseEntity::getInfo()['title_plural']]),
102 'https://docs.civicrm.org/user/en/latest/organising-your-data/creating-custom-fields/#multiple-record-fieldsets',
103 '\\Civi\\Api4\\CustomGroup',
105 'icon' => $customEntity['icon'],
107 if (!empty($customEntity['help_pre'])) {
108 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
110 if (!empty($customEntity['help_post'])) {
111 $pre = empty($entities[$fieldName]['comment']) ?
'' : $entities[$fieldName]['comment'] . "\n\n";
112 $entities[$fieldName]['comment'] = $pre . $this->plainTextify($customEntity['help_post']);
118 * Convert html to plain text.
123 private function plainTextify($input) {
124 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5
, 'UTF-8');