diff --git a/ext/afform/core/Civi/Api4/Action/Afform/SubmitFile.php b/ext/afform/core/Civi/Api4/Action/Afform/SubmitFile.php index ea0ba2965bc1..44d510b7d285 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/SubmitFile.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/SubmitFile.php @@ -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 []; } @@ -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']); - } - }