}
$prefix = $pathArray ? implode('.', $pathArray) . '.' : '';
// Cache field info for retrieval by $this->getField()
- $joinEntity = $lastLink->getEntity();
foreach ($lastLink->getEntityFields() as $fieldObject) {
- $fieldArray = ['entity' => $joinEntity] + $fieldObject->toArray();
+ $fieldArray = $fieldObject->toArray();
$fieldArray['sql_name'] = '`' . $lastLink->getAlias() . '`.`' . $fieldArray['column_name'] . '`';
$fieldArray['is_custom'] = $isCustom;
$fieldArray['is_join'] = TRUE;
* @param $targetTable
* @param $alias
* @param bool $isMultiRecord
- * @param string $entity
* @param string $columns
*/
- public function __construct($targetTable, $alias, $isMultiRecord, $entity, $columns) {
- $this->entity = $entity;
+ public function __construct($targetTable, $alias, $isMultiRecord, $columns) {
$this->columns = $columns;
parent::__construct($targetTable, 'entity_id', $alias);
$this->joinType = $isMultiRecord ?
self::JOIN_TYPE_ONE_TO_MANY : self::JOIN_TYPE_ONE_TO_ONE;
+ // Only multi-record groups are considered an api "entity"
+ if (!$isMultiRecord) {
+ $this->entity = NULL;
+ }
}
/**
if (!$this->entityFields) {
$fields = CustomField::get()
->setCheckPermissions(FALSE)
- ->setSelect(['custom_group.name', '*'])
+ ->setSelect(['custom_group.name', 'custom_group.extends', '*'])
->addWhere('custom_group.table_name', '=', $this->getTargetTable())
->execute();
foreach ($fields as $field) {
- $this->entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, $this->getEntity());
+ $this->entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, $this->getEntityFromExtends($field['custom_group.extends']));
}
}
return $this->entityFields;
return $this->columns[$fieldName];
}
+ /**
+ * Translate custom_group.extends to entity name.
+ *
+ * Custom_group.extends pretty much maps 1-1 with entity names, except for a couple oddballs.
+ * @see \CRM_Core_SelectValues::customGroupExtends
+ *
+ * @param $extends
+ * @return string
+ * @throws \API_Exception
+ * @throws \Civi\API\Exception\UnauthorizedException
+ */
+ private function getEntityFromExtends($extends) {
+ if (strpos($extends, 'Participant') === 0) {
+ return 'Participant';
+ }
+ if ($extends === 'Contact' || in_array($extends, \CRM_Contact_BAO_ContactType::basicTypes(TRUE))) {
+ return 'Contact';
+ }
+ return $extends;
+ }
+
}
$target = $map->getTableByName($link->getTargetTable());
$tableName = $link->getBaseTable();
// Exclude custom field tables
- if (strpos($link->getTargetTable(), 'civicrm_value_') !== 0) {
+ if (strpos($link->getTargetTable(), 'civicrm_value_') !== 0 && strpos($link->getBaseTable(), 'civicrm_value_') !== 0) {
$plural = str_replace('civicrm_', '', $this->getPlural($tableName));
$joinable = new Joinable($tableName, $link->getBaseColumn(), $plural);
$joinable->setJoinType($joinable::JOIN_TYPE_ONE_TO_MANY);
}
foreach ($links as $alias => $link) {
- $joinable = new CustomGroupJoinable($link['tableName'], $alias, $link['isMultiple'], $entity, $link['columns']);
+ $joinable = new CustomGroupJoinable($link['tableName'], $alias, $link['isMultiple'], $link['columns']);
$baseTable->addTableLink('id', $joinable);
}
}