Merge pull request #23446 from colemanw/searchKitDefaultAgg
[civicrm-core.git] / Civi / Api4 / Generic / DAOGetFieldsAction.php
index a8a359b5d17a51e42c8f0a1944047c5848c51a03..e877e6dd8b2b852bc0ff654167fb76fce3386b81 100644 (file)
  +--------------------------------------------------------------------+
  */
 
-/**
- *
- * @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()
@@ -43,6 +39,7 @@ class DAOGetFieldsAction extends BasicGetFieldsAction {
       // 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) {
@@ -98,6 +95,29 @@ class DAOGetFieldsAction extends BasicGetFieldsAction {
     }
   }
 
+  /**
+   * 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[] = [
@@ -125,6 +145,11 @@ class DAOGetFieldsAction extends BasicGetFieldsAction {
       'data_type' => 'Array',
       '@internal' => TRUE,
     ];
+    $fields[] = [
+      'name' => 'sql_renderer',
+      'data_type' => 'Array',
+      '@internal' => TRUE,
+    ];
     return $fields;
   }