4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2019
38 namespace Civi\Api4\Generic
;
40 use Civi\API\Exception\NotImplementedException
;
41 use Civi\Api4\Utils\ActionUtil
;
44 * Get fields for an entity.
46 * @method $this setLoadOptions(bool $value)
47 * @method bool getLoadOptions()
48 * @method $this setAction(string $value)
50 class BasicGetFieldsAction
extends BasicGetAction
{
53 * Fetch option lists for fields?
57 protected $loadOptions = FALSE;
62 protected $action = 'get';
65 * To implement getFields for your own entity:
67 * 1. From your entity class add a static getFields method.
68 * 2. That method should construct and return this class.
69 * 3. The 3rd argument passed to this constructor should be a function that returns an
70 * array of fields for your entity's CRUD actions.
71 * 4. For non-crud actions that need a different set of fields, you can override the
72 * list from step 3 on a per-action basis by defining a fields() method in that action.
73 * See for example BasicGetFieldsAction::fields() or GetActions::fields().
75 * @param Result $result
76 * @throws \Civi\API\Exception\NotImplementedException
78 public function _run(Result
$result) {
80 $actionClass = ActionUtil
::getAction($this->getEntityName(), $this->getAction());
82 catch (NotImplementedException
$e) {
84 if (isset($actionClass) && method_exists($actionClass, 'fields')) {
85 $values = $actionClass->fields();
88 $values = $this->getRecords();
90 $this->padResults($values);
91 $result->exchangeArray($this->queryArray($values));
95 * Ensure every result contains, at minimum, the array keys as defined in $this->fields.
97 * Attempt to set some sensible defaults for some fields.
99 * In most cases it's not necessary to override this function, even if your entity is really weird.
100 * Instead just override $this->fields and thes function will respect that.
102 * @param array $values
104 protected function padResults(&$values) {
105 $fields = array_column($this->fields(), 'name');
106 foreach ($values as &$field) {
107 $defaults = array_intersect_key([
108 'title' => empty($field['name']) ?
NULL : ucwords(str_replace('_', ' ', $field['name'])),
109 'entity' => $this->getEntityName(),
111 'options' => !empty($field['pseudoconstant']),
112 'data_type' => \CRM_Utils_Array
::value('type', $field, 'String'),
113 ], array_flip($fields));
115 if (!$this->loadOptions
&& isset($defaults['options'])) {
116 $field['options'] = (bool) $field['options'];
118 $field +
= array_fill_keys($fields, NULL);
125 public function getAction() {
126 // For actions that build on top of other actions, return fields for the simpler action
129 'replace' => 'create',
131 return $sub[$this->action
] ??
$this->action
;
134 public function fields() {
138 'data_type' => 'String',
142 'data_type' => 'String',
145 'name' => 'description',
146 'data_type' => 'String',
149 'name' => 'default_value',
150 'data_type' => 'String',
153 'name' => 'required',
154 'data_type' => 'Boolean',
157 'name' => 'required_if',
158 'data_type' => 'String',
162 'data_type' => 'Array',
165 'name' => 'data_type',
166 'data_type' => 'String',
169 'name' => 'input_type',
170 'data_type' => 'String',
173 'name' => 'input_attrs',
174 'data_type' => 'Array',
177 'name' => 'fk_entity',
178 'data_type' => 'String',
181 'name' => 'serialize',
182 'data_type' => 'Integer',
186 'data_type' => 'String',