Merge pull request #15523 from jamie-tillman/patch-1
[civicrm-core.git] / Civi / Api4 / Generic / DAOGetFieldsAction.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace Civi\Api4\Generic;
23
24 use Civi\Api4\Service\Spec\SpecFormatter;
25
26 /**
27 * Get fields for a DAO-based entity.
28 *
29 * @method $this setIncludeCustom(bool $value)
30 * @method bool getIncludeCustom()
31 */
32 class DAOGetFieldsAction extends BasicGetFieldsAction {
33
34 /**
35 * Include custom fields for this entity, or only core fields?
36 *
37 * @var bool
38 */
39 protected $includeCustom = TRUE;
40
41 /**
42 * Get fields for a DAO-based entity
43 *
44 * @return array
45 */
46 protected function getRecords() {
47 $fields = $this->_itemsToGet('name');
48 /** @var \Civi\Api4\Service\Spec\SpecGatherer $gatherer */
49 $gatherer = \Civi::container()->get('spec_gatherer');
50 // Any fields name with a dot in it is custom
51 if ($fields) {
52 $this->includeCustom = strpos(implode('', $fields), '.') !== FALSE;
53 }
54 $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $this->includeCustom);
55 return SpecFormatter::specToArray($spec->getFields($fields), $this->loadOptions);
56 }
57
58 public function fields() {
59 $fields = parent::fields();
60 $fields[] = [
61 'name' => 'help_pre',
62 'data_type' => 'String',
63 ];
64 $fields[] = [
65 'name' => 'help_post',
66 'data_type' => 'String',
67 ];
68 $fields[] = [
69 'name' => 'custom_field_id',
70 'data_type' => 'Integer',
71 ];
72 $fields[] = [
73 'name' => 'custom_group_id',
74 'data_type' => 'Integer',
75 ];
76 return $fields;
77 }
78
79 }