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
22 namespace Civi\Api4\Generic
;
24 use Civi\API\Exception\NotImplementedException
;
25 use Civi\Api4\Utils\ActionUtil
;
28 * Get fields for an entity.
30 * @method $this setLoadOptions(bool $value)
31 * @method bool getLoadOptions()
32 * @method $this setAction(string $value)
33 * @method $this addValue(string $value)
34 * @method $this setValues(array $values)
35 * @method array getValues()
37 class BasicGetFieldsAction
extends BasicGetAction
{
40 * Fetch option lists for fields?
44 protected $loadOptions = FALSE;
47 * Fields will be returned appropriate to the specified action (get, create, delete, etc.)
51 protected $action = 'get';
54 * Fields will be returned appropriate to the specified values (e.g. ['contact_type' => 'Individual'])
58 protected $values = [];
61 * To implement getFields for your own entity:
63 * 1. From your entity class add a static getFields method.
64 * 2. That method should construct and return this class.
65 * 3. The 3rd argument passed to this constructor should be a function that returns an
66 * array of fields for your entity's CRUD actions.
67 * 4. For non-crud actions that need a different set of fields, you can override the
68 * list from step 3 on a per-action basis by defining a fields() method in that action.
69 * See for example BasicGetFieldsAction::fields() or GetActions::fields().
71 * @param Result $result
72 * @throws \Civi\API\Exception\NotImplementedException
74 public function _run(Result
$result) {
76 $actionClass = ActionUtil
::getAction($this->getEntityName(), $this->getAction());
78 catch (NotImplementedException
$e) {
80 if (isset($actionClass) && method_exists($actionClass, 'fields')) {
81 $values = $actionClass->fields();
84 $values = $this->getRecords();
86 $this->padResults($values);
87 $result->exchangeArray($this->queryArray($values));
91 * Ensure every result contains, at minimum, the array keys as defined in $this->fields.
93 * Attempt to set some sensible defaults for some fields.
95 * In most cases it's not necessary to override this function, even if your entity is really weird.
96 * Instead just override $this->fields and thes function will respect that.
98 * @param array $values
100 protected function padResults(&$values) {
101 $fields = array_column($this->fields(), 'name');
102 foreach ($values as &$field) {
103 $defaults = array_intersect_key([
104 'title' => empty($field['name']) ?
NULL : ucwords(str_replace('_', ' ', $field['name'])),
105 'entity' => $this->getEntityName(),
107 'options' => !empty($field['pseudoconstant']),
108 'data_type' => \CRM_Utils_Array
::value('type', $field, 'String'),
109 ], array_flip($fields));
111 if (!$this->loadOptions
&& isset($defaults['options'])) {
112 $field['options'] = (bool) $field['options'];
114 $field +
= array_fill_keys($fields, NULL);
121 public function getAction() {
122 // For actions that build on top of other actions, return fields for the simpler action
125 'replace' => 'create',
127 return $sub[$this->action
] ??
$this->action
;
130 public function fields() {
134 'data_type' => 'String',
138 'data_type' => 'String',
141 'name' => 'description',
142 'data_type' => 'String',
145 'name' => 'default_value',
146 'data_type' => 'String',
149 'name' => 'required',
150 'data_type' => 'Boolean',
153 'name' => 'required_if',
154 'data_type' => 'String',
158 'data_type' => 'Array',
161 'name' => 'data_type',
162 'data_type' => 'String',
165 'name' => 'input_type',
166 'data_type' => 'String',
169 'name' => 'input_attrs',
170 'data_type' => 'Array',
173 'name' => 'fk_entity',
174 'data_type' => 'String',
177 'name' => 'serialize',
178 'data_type' => 'Integer',
182 'data_type' => 'String',