Skip to content

Commit

Permalink
Merge pull request #20440 from colemanw/apiCalculatedField
Browse files Browse the repository at this point in the history
[REF] APIv4 refactoring to support calculated fields
  • Loading branch information
eileenmcnaughton authored May 31, 2021
2 parents 94f5f09 + e60149f commit 9bc987c
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 224 deletions.
4 changes: 1 addition & 3 deletions Civi/Api4/Action/CustomValue/GetFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

namespace Civi\Api4\Action\CustomValue;

use Civi\Api4\Service\Spec\SpecFormatter;

/**
* Get fields for a custom group.
*/
Expand All @@ -25,7 +23,7 @@ protected function getRecords() {
/** @var \Civi\Api4\Service\Spec\SpecGatherer $gatherer */
$gatherer = \Civi::container()->get('spec_gatherer');
$spec = $gatherer->getSpec('Custom_' . $this->getCustomGroup(), $this->getAction(), $this->includeCustom, $this->values);
return SpecFormatter::specToArray($spec->getFields($fields), $this->loadOptions);
return $this->specToArray($spec->getFields($fields));
}

/**
Expand Down
22 changes: 19 additions & 3 deletions Civi/Api4/Generic/DAOGetFieldsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

namespace Civi\Api4\Generic;

use Civi\Api4\Service\Spec\SpecFormatter;

/**
* @inheritDoc
* @method bool getIncludeCustom()
Expand Down Expand Up @@ -48,7 +46,7 @@ protected function getRecords() {
$this->includeCustom = strpos(implode('', $fieldsToGet), '.') !== FALSE;
}
$spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $this->includeCustom, $this->values);
$fields = SpecFormatter::specToArray($spec->getFields($fieldsToGet), $this->loadOptions, $this->values);
$fields = $this->specToArray($spec->getFields($fieldsToGet));
foreach ($fieldsToGet ?? [] as $fieldName) {
if (empty($fields[$fieldName]) && strpos($fieldName, '.') !== FALSE) {
$fkField = $this->getFkFieldSpec($fieldName, $fields);
Expand All @@ -61,6 +59,24 @@ protected function getRecords() {
return $fields;
}

/**
* @param \Civi\Api4\Service\Spec\FieldSpec[] $fields
*
* @return array
*/
protected function specToArray($fields) {
$fieldArray = [];

foreach ($fields as $field) {
if ($this->loadOptions) {
$field->getOptions($this->values, $this->loadOptions, $this->checkPermissions);
}
$fieldArray[$field->getName()] = $field->toArray();
}

return $fieldArray;
}

/**
* @param string $fieldName
* @param array $fields
Expand Down
27 changes: 2 additions & 25 deletions Civi/Api4/Service/Spec/CustomFieldSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ class CustomFieldSpec extends FieldSpec {
/**
* @var int
*/
protected $customFieldId;
public $customFieldId;

/**
* @var int
*/
protected $customGroup;

/**
* @var string
*/
protected $tableName;
public $customGroup;

/**
* @inheritDoc
Expand Down Expand Up @@ -91,22 +86,4 @@ public function setCustomGroupName($customGroupName) {
return $this;
}

/**
* @return string
*/
public function getCustomTableName() {
return $this->tableName;
}

/**
* @param string $customFieldColumnName
*
* @return $this
*/
public function setCustomTableName($customFieldColumnName) {
$this->tableName = $customFieldColumnName;

return $this;
}

}
Loading

0 comments on commit 9bc987c

Please sign in to comment.