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
;
25 * Get the names & docblocks of all APIv4 entities.
27 * Scans for api entities in core + enabled extensions.
29 * Also includes pseudo-entities from multi-record custom groups by default.
31 * @method $this setIncludeCustom(bool $value)
32 * @method bool getIncludeCustom()
34 class Get
extends \Civi\Api4\Generic\BasicGetAction
{
37 * Include custom-field-based pseudo-entities?
41 protected $includeCustom = TRUE;
44 * Scan all api directories to discover entities
46 protected function getRecords() {
48 $toGet = $this->_itemsToGet('name');
49 $locations = array_merge([\Civi
::paths()->getPath('[civicrm.root]/Civi.php')],
50 array_column(\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
52 foreach ($locations as $location) {
53 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($location)) . 'Civi/Api4';
55 foreach (glob("$dir/*.php") as $file) {
57 preg_match('/(\w*)\.php$/', $file, $matches);
58 $entity = '\Civi\Api4\\' . $matches[1];
60 (!$toGet ||
in_array($matches[1], $toGet))
61 && is_a($entity, '\Civi\Api4\Generic\AbstractEntity', TRUE)
63 $info = $entity::getInfo();
64 $entities[$info['name']] = $info;
70 // Fetch custom entities unless we've already fetched everything requested
71 if ($this->includeCustom
&& (!$toGet ||
array_diff($toGet, array_keys($entities)))) {
72 $this->addCustomEntities($entities);
80 * Add custom-field pseudo-entities
83 * @throws \API_Exception
85 private function addCustomEntities(&$entities) {
86 $customEntities = CustomGroup
::get()
87 ->addWhere('is_multiple', '=', 1)
88 ->addWhere('is_active', '=', 1)
89 ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends', 'icon'])
90 ->setCheckPermissions(FALSE)
92 foreach ($customEntities as $customEntity) {
93 $fieldName = 'Custom_' . $customEntity['name'];
94 $entities[$fieldName] = [
96 'title' => $customEntity['title'],
97 'description' => 'Custom group - extends ' . $customEntity['extends'],
99 'https://docs.civicrm.org/user/en/latest/organising-your-data/creating-custom-fields/#multiple-record-fieldsets',
100 '\\Civi\\Api4\\CustomGroup',
102 'icon' => $customEntity['icon'],
104 if (!empty($customEntity['help_pre'])) {
105 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
107 if (!empty($customEntity['help_post'])) {
108 $pre = empty($entities[$fieldName]['comment']) ?
'' : $entities[$fieldName]['comment'] . "\n\n";
109 $entities[$fieldName]['comment'] = $pre . $this->plainTextify($customEntity['help_post']);
115 * Convert html to plain text.
120 private function plainTextify($input) {
121 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5
, 'UTF-8');