APIv4 - Get dynamic list of entity types
[civicrm-core.git] / Civi / Api4 / Entity.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11 namespace Civi\Api4;
12
13 /**
14 * Retrieves information about all Api4 entities.
15 *
16 * @see \Civi\Api4\Generic\AbstractEntity
17 *
18 * @searchable none
19 * @since 5.19
20 * @package Civi\Api4
21 */
22 class Entity extends Generic\AbstractEntity {
23
24 /**
25 * @param bool $checkPermissions
26 * @return Action\Entity\Get
27 */
28 public static function get($checkPermissions = TRUE) {
29 return (new Action\Entity\Get('Entity', __FUNCTION__))
30 ->setCheckPermissions($checkPermissions);
31 }
32
33 /**
34 * @param bool $checkPermissions
35 * @return Generic\BasicGetFieldsAction
36 */
37 public static function getFields($checkPermissions = TRUE) {
38 return (new Generic\BasicGetFieldsAction('Entity', __FUNCTION__, function(Generic\BasicGetFieldsAction $getFields) {
39 return [
40 [
41 'name' => 'name',
42 'description' => 'Entity name',
43 ],
44 [
45 'name' => 'title',
46 'description' => 'Localized title (singular)',
47 ],
48 [
49 'name' => 'title_plural',
50 'description' => 'Localized title (plural)',
51 ],
52 [
53 'name' => 'type',
54 'data_type' => 'Array',
55 'description' => 'Base class for this entity',
56 'options' => $getFields->getLoadOptions() ? self::getEntityTypes() : TRUE,
57 ],
58 [
59 'name' => 'description',
60 'description' => 'Description from docblock',
61 ],
62 [
63 'name' => 'comment',
64 'description' => 'Comments from docblock',
65 ],
66 [
67 'name' => 'icon',
68 'description' => 'crm-i icon class associated with this entity',
69 ],
70 [
71 'name' => 'dao',
72 'description' => 'Class name for dao-based entities',
73 ],
74 [
75 'name' => 'primary_key',
76 'data_type' => 'Array',
77 'description' => 'Name of unique identifier field(s) (e.g. [id])',
78 ],
79 [
80 'name' => 'label_field',
81 'description' => 'Field to show when displaying a record',
82 ],
83 [
84 'name' => 'order_by',
85 'description' => 'Default column to sort results',
86 ],
87 [
88 'name' => 'searchable',
89 'description' => 'How should this entity be presented in search UIs',
90 'options' => [
91 'primary' => ts('Primary'),
92 'secondary' => ts('Secondary'),
93 'bridge' => ts('Bridge'),
94 'none' => ts('None'),
95 ],
96 ],
97 [
98 'name' => 'paths',
99 'data_type' => 'Array',
100 'description' => 'System paths for accessing this entity',
101 ],
102 [
103 'name' => 'see',
104 'data_type' => 'Array',
105 'description' => 'Any @see annotations from docblock',
106 ],
107 [
108 'name' => 'since',
109 'data_type' => 'String',
110 'description' => 'Version this API entity was added',
111 ],
112 [
113 'name' => 'class',
114 'data_type' => 'String',
115 'description' => 'PHP class name',
116 ],
117 [
118 'name' => 'bridge',
119 'data_type' => 'Array',
120 'description' => 'Connecting fields for EntityBridge types',
121 ],
122 [
123 'name' => 'ui_join_filters',
124 'data_type' => 'Array',
125 'description' => 'When joining entities in the UI, which fields should be presented by default in the ON clause',
126 ],
127 [
128 'name' => 'group_weights_by',
129 'data_type' => 'Array',
130 'description' => 'For sortable entities, what field groupings are used to order by weight',
131 ],
132 ];
133 }))->setCheckPermissions($checkPermissions);
134 }
135
136 /**
137 * @param bool $checkPermissions
138 * @deprecated
139 * @return Action\Entity\GetLinks
140 */
141 public static function getLinks($checkPermissions = TRUE) {
142 return (new Action\Entity\GetLinks('Entity', __FUNCTION__))
143 ->setCheckPermissions($checkPermissions);
144 }
145
146 /**
147 * @return array
148 */
149 public static function permissions() {
150 return [
151 'default' => ['access CiviCRM'],
152 ];
153 }
154
155 /**
156 * Collect the 'type' values from every entity.
157 *
158 * @return array
159 */
160 private static function getEntityTypes() {
161 $provider = \Civi::service('action_object_provider');
162 $entityTypes = [];
163 foreach ($provider->getEntities() as $entity) {
164 foreach ($entity['type'] ?? [] as $type) {
165 $entityTypes[$type] = $type;
166 }
167 }
168 return $entityTypes;
169 }
170
171 }