+--------------------------------------------------------------------+
*/
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC https://civicrm.org/licensing
- */
-
-
namespace Civi\Api4\Generic;
+use Civi\Api4\Utils\CoreUtil;
+use Civi\Api4\Utils\FormattingUtil;
+
/**
* @inheritDoc
* @method bool getIncludeCustom()
*/
class DAOGetFieldsAction extends BasicGetFieldsAction {
- /**
- * Include custom fields for this entity, or only core fields?
- *
- * @var bool
- */
- protected $includeCustom = TRUE;
-
/**
* Get fields for a DAO-based entity.
*
*/
protected function getRecords() {
$fieldsToGet = $this->_itemsToGet('name');
+ $typesToGet = $this->_itemsToGet('type');
/** @var \Civi\Api4\Service\Spec\SpecGatherer $gatherer */
$gatherer = \Civi::container()->get('spec_gatherer');
- // Any fields name with a dot in it is either custom or an implicit join
- if ($fieldsToGet) {
- $this->includeCustom = strpos(implode('', $fieldsToGet), '.') !== FALSE;
+ $includeCustom = TRUE;
+ if ($typesToGet) {
+ $includeCustom = in_array('Custom', $typesToGet, TRUE);
}
- $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $this->includeCustom, $this->values);
+ elseif ($fieldsToGet) {
+ // Any fields name with a dot in it is either custom or an implicit join
+ $includeCustom = strpos(implode('', $fieldsToGet), '.') !== FALSE;
+ }
+ $this->formatValues();
+ $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $includeCustom, $this->values);
$fields = $this->specToArray($spec->getFields($fieldsToGet));
foreach ($fieldsToGet ?? [] as $fieldName) {
if (empty($fields[$fieldName]) && strpos($fieldName, '.') !== FALSE) {
}
}
+ /**
+ * Special handling for pseudoconstant replacements.
+ *
+ * Normally this would involve calling getFields... but this IS getFields.
+ *
+ * @throws \API_Exception
+ */
+ private function formatValues() {
+ foreach (array_keys($this->values) as $key) {
+ if (strpos($key, ':')) {
+ [$fieldName, $suffix] = explode(':', $key);
+ $context = FormattingUtil::$pseudoConstantContexts[$suffix] ?? NULL;
+ if (!$context) {
+ throw new \API_Exception('Illegal expression');
+ }
+ $baoName = CoreUtil::getBAOFromApiName($this->getEntityName());
+ $options = $baoName::buildOptions($fieldName, $context);
+ $this->values[$fieldName] = FormattingUtil::replacePseudoconstant($options, $this->values[$key], TRUE);
+ unset($this->values[$key]);
+ }
+ }
+ }
+
public function fields() {
$fields = parent::fields();
$fields[] = [
'name' => 'custom_group_id',
'data_type' => 'Integer',
];
+ $fields[] = [
+ 'name' => 'sql_filters',
+ 'data_type' => 'Array',
+ '@internal' => TRUE,
+ ];
+ $fields[] = [
+ 'name' => 'sql_renderer',
+ 'data_type' => 'Array',
+ '@internal' => TRUE,
+ ];
return $fields;
}