Skip to content

Commit

Permalink
fixup! Don't use custom field handling of Attachment.create
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Tubach authored and eileenmcnaughton committed Feb 16, 2024
1 parent e07a1dd commit c8fb804
Showing 1 changed file with 10 additions and 41 deletions.
51 changes: 10 additions & 41 deletions ext/afform/core/Civi/Api4/Action/Afform/SubmitFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,23 @@ protected function processForm() {
throw new \CRM_Core_Exception('Entity not found');
}

$attachmentParams = [
$file = civicrm_api3('Attachment', 'create', [
'entity_id' => $entityId,
'entity_table' => CoreUtil::getTableName($apiEntity),
'mime_type' => $_FILES['file']['type'],
'name' => $_FILES['file']['name'],
'options' => [
'move-file' => $_FILES['file']['tmp_name'],
],
];
]);

if ($this->isCustomField($apiEntity, $this->fieldName)) {
$attachmentParams['field_name'] = $this->convertFieldNameToApi3($apiEntity, $this->fieldName);
}
else {
$attachmentParams['entity_table'] = CoreUtil::getTableName($apiEntity);
}

$file = civicrm_api3('Attachment', 'create', $attachmentParams);

if (!$this->isCustomField($apiEntity, $this->fieldName)) {
civicrm_api4($apiEntity, 'update', [
'values' => [
$idField => $entityId,
$this->fieldName => $file['id'],
],
'checkPermissions' => FALSE,
]);
}
civicrm_api4($apiEntity, 'update', [
'values' => [
$idField => $entityId,
$this->fieldName => $file['id'],
],
'checkPermissions' => FALSE,
]);

return [];
}
Expand All @@ -128,25 +118,4 @@ protected function loadEntities() {
$this->_entityIds = $info['civiAfformSubmission']['data'];
}

/**
* @param string $apiEntity
* @param string $fieldName
* @return string
*/
private function convertFieldNameToApi3($apiEntity, $fieldName) {
if (strpos($fieldName, '.')) {
$fields = civicrm_api4($apiEntity, 'getFields', [
'checkPermissions' => FALSE,
'where' => [['name', '=', $fieldName]],
]);
return 'custom_' . $fields[0]['custom_field_id'];
}
return $fieldName;
}

private function isCustomField(string $entityName, string $fieldName): bool {
$field = static::getEntityField($entityName, $fieldName);
return !empty($field['custom_field_id']);
}

}

0 comments on commit c8fb804

Please sign in to comment.