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\Generic
;
22 use Civi\API\Exception\NotImplementedException
;
25 * Lists information about fields for the $ENTITY entity.
27 * This field information is also known as "metadata."
29 * Note that different actions may support different lists of fields.
30 * By default this will fetch the field list relevant to `get`,
31 * but a different list may be returned if you specify another action.
33 * @method $this setLoadOptions(bool|array $value)
34 * @method bool|array getLoadOptions()
35 * @method $this setAction(string $value)
36 * @method $this setValues(array $values)
37 * @method array getValues()
39 class BasicGetFieldsAction
extends BasicGetAction
{
42 * Fetch option lists for fields?
44 * This parameter can be either a boolean or an array of attributes to return from the option list:
46 * - If `FALSE`, each field's `options` property will be a boolean indicating whether the field has an option list
47 * - If `TRUE`, `options` will be returned as a flat array of the option list's `[id => label]`
48 * - If an array, `options` will be a non-associative array of requested properties:
49 * id, name, label, abbr, description, color, icon
50 * e.g. `loadOptions: ['id', 'name', 'label']` will return an array like `[[id: 1, name: 'Meeting', label: 'Meeting'], ...]`
51 * (note that names and labels are generally ONLY the same when the site's language is set to English).
55 protected $loadOptions = FALSE;
58 * Fields will be returned appropriate to the specified action (get, create, delete, etc.)
62 protected $action = 'get';
65 * Fields will be returned appropriate to the specified values (e.g. ['contact_type' => 'Individual'])
69 protected $values = [];
72 * To implement getFields for your own entity:
74 * 1. From your entity class add a static getFields method.
75 * 2. That method should construct and return this class.
76 * 3. The 3rd argument passed to this constructor should be a function that returns an
77 * array of fields for your entity's CRUD actions.
78 * 4. For non-crud actions that need a different set of fields, you can override the
79 * list from step 3 on a per-action basis by defining a fields() method in that action.
80 * See for example BasicGetFieldsAction::fields() or GetActions::fields().
82 * @param Result $result
83 * @throws \Civi\API\Exception\NotImplementedException
85 public function _run(Result
$result) {
87 $actionClass = \Civi\API\Request
::create($this->getEntityName(), $this->getAction(), ['version' => 4]);
89 catch (NotImplementedException
$e) {
91 if (isset($actionClass) && method_exists($actionClass, 'fields')) {
92 $values = $actionClass->fields();
95 $values = $this->getRecords();
97 $this->formatResults($values);
98 $this->queryArray($values, $result);
102 * Ensure every result contains, at minimum, the array keys as defined in $this->fields.
104 * Attempt to set some sensible defaults for some fields.
106 * Format option lists.
108 * In most cases it's not necessary to override this function, even if your entity is really weird.
109 * Instead just override $this->fields and this function will respect that.
111 * @param array $values
113 protected function formatResults(&$values) {
114 $fields = array_column($this->fields(), 'name');
115 // Enforce field permissions
116 if ($this->checkPermissions
) {
117 foreach ($values as $key => $field) {
118 if (!empty($field['permission']) && !\CRM_Core_Permission
::check($field['permission'])) {
119 unset($values[$key]);
123 foreach ($values as &$field) {
124 $defaults = array_intersect_key([
125 'title' => empty($field['name']) ?
NULL : ucwords(str_replace('_', ' ', $field['name'])),
126 'entity' => $this->getEntityName(),
128 'options' => !empty($field['pseudoconstant']),
129 'data_type' => \CRM_Utils_Array
::value('type', $field, 'String'),
130 ], array_flip($fields));
132 if (isset($defaults['options'])) {
133 $field['options'] = $this->formatOptionList($field['options']);
135 $field +
= array_fill_keys($fields, NULL);
140 * Transforms option list into the format specified in $this->loadOptions
145 private function formatOptionList($options) {
146 if (!$this->loadOptions ||
!is_array($options)) {
147 return (bool) $options;
153 $first = reset($options);
154 // Flat array requested
155 if ($this->loadOptions
=== TRUE) {
156 // Convert non-associative to flat array
157 if (is_array($first) && isset($first['id'])) {
158 foreach ($options as $option) {
159 $formatted[$option['id']] = $option['label'] ??
$option['name'] ??
$option['id'];
165 // Non-associative array of multiple properties requested
166 foreach ($options as $id => $option) {
167 // Transform a flat list
168 if (!is_array($option)) {
175 $formatted[] = array_intersect_key($option, array_flip($this->loadOptions
));
183 public function getAction() {
184 // For actions that build on top of other actions, return fields for the simpler action
187 'replace' => 'create',
189 return $sub[$this->action
] ??
$this->action
;
193 * Add an item to the values array
194 * @param string $fieldName
195 * @param mixed $value
198 public function addValue(string $fieldName, $value) {
199 $this->values
[$fieldName] = $value;
204 * @param bool $includeCustom
207 public function setIncludeCustom(bool $includeCustom) {
208 // Be forgiving if the param doesn't exist and don't throw an exception
209 if (property_exists($this, 'includeCustom')) {
210 $this->includeCustom
= $includeCustom;
215 public function fields() {
219 'data_type' => 'String',
223 'data_type' => 'String',
226 'name' => 'description',
227 'data_type' => 'String',
230 'name' => 'default_value',
231 'data_type' => 'String',
234 'name' => 'required',
235 'data_type' => 'Boolean',
238 'name' => 'required_if',
239 'data_type' => 'String',
243 'data_type' => 'Array',
246 'name' => 'data_type',
247 'data_type' => 'String',
250 'name' => 'input_type',
251 'data_type' => 'String',
254 'name' => 'input_attrs',
255 'data_type' => 'Array',
258 'name' => 'fk_entity',
259 'data_type' => 'String',
262 'name' => 'serialize',
263 'data_type' => 'Integer',
267 'data_type' => 'String',