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\Utils\ReflectionUtils
;
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 $getDocs = $this->_isFieldSelected('description', 'comment', 'see');
51 $locations = array_merge([\Civi
::paths()->getPath('[civicrm.root]/Civi.php')],
52 array_column(\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
54 foreach ($locations as $location) {
55 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($location)) . 'Civi/Api4';
57 foreach (glob("$dir/*.php") as $file) {
59 preg_match('/(\w*)\.php$/', $file, $matches);
61 (!$toGet ||
in_array($matches[1], $toGet))
62 && is_a('\Civi\Api4\\' . $matches[1], '\Civi\Api4\Generic\AbstractEntity', TRUE)
64 $entity = ['name' => $matches[1]];
66 $this->addDocs($entity);
68 $entities[$matches[1]] = $entity;
74 // Fetch custom entities unless we've already fetched everything requested
75 if ($this->includeCustom
&& (!$toGet ||
array_diff($toGet, array_keys($entities)))) {
76 $this->addCustomEntities($entities);
84 * Add custom-field pseudo-entities
87 * @throws \API_Exception
89 private function addCustomEntities(&$entities) {
90 $customEntities = CustomGroup
::get()
91 ->addWhere('is_multiple', '=', 1)
92 ->addWhere('is_active', '=', 1)
93 ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends'])
94 ->setCheckPermissions(FALSE)
96 foreach ($customEntities as $customEntity) {
97 $fieldName = 'Custom_' . $customEntity['name'];
98 $entities[$fieldName] = [
100 'description' => $customEntity['title'] . ' custom group - extends ' . $customEntity['extends'],
102 'https://docs.civicrm.org/user/en/latest/organising-your-data/creating-custom-fields/#multiple-record-fieldsets',
103 '\\Civi\\Api4\\CustomGroup',
106 if (!empty($customEntity['help_pre'])) {
107 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
109 if (!empty($customEntity['help_post'])) {
110 $pre = empty($entities[$fieldName]['comment']) ?
'' : $entities[$fieldName]['comment'] . "\n\n";
111 $entities[$fieldName]['comment'] = $pre . $this->plainTextify($customEntity['help_post']);
117 * Convert html to plain text.
122 private function plainTextify($input) {
123 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5
, 'UTF-8');
127 * Add info from code docblock.
131 private function addDocs(&$entity) {
132 $reflection = new \
ReflectionClass("\\Civi\\Api4\\" . $entity['name']);
133 $entity +
= ReflectionUtils
::getCodeDocs($reflection, NULL, ['entity' => $entity['name']]);
134 unset($entity['package'], $entity['method']);