api attachment.get gives fatal error for custom file fields on cases
I don't really believe in custom fields on cases, but if you happen to have one of type file, api attachment get fails on it with a fatal error "Failed to match record to related entity".
- Create a custom field of type file for cases.
- Create a case and upload a file in the custom field.
- Run something like
cv ev "var_dump(civicrm_api3('attachment', 'get', ['id' => 1]));"
where 1 is the corresponding id from civicrm_file. You'll get a fatal error. - You can also see it by:
- Going to Find Cases.
- Select the case you created.
- Choose export from the actions dropdown.
- Choose selected fields (ignore the E_WARNINGs).
- Pick your custom field.
- Click Download File.
- Aside: I don't know why you would do this csv export because if it were working the column output is a time-limited url, so maybe is useful as input to another process, but I'm not sure what else.
It happens because the api goes through a couple central functions including Civi\API\Subscriber\DynamicFKAuthorization which tries to do a lookup by calling CRM_Core_BAO_CustomGroup::getTableNameByEntityName() to find out what table the custom field "extends", but it doesn't know about cases.
This appears to be a quick fix, but I'm not sure what weirdness this might introduce elsewhere, or whether the cases omission is on purpose.
diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php
index 3a0dac33f1..435982d336 100644
--- a/CRM/Core/BAO/CustomGroup.php
+++ b/CRM/Core/BAO/CustomGroup.php
@@ -1164,6 +1164,10 @@ ORDER BY civicrm_custom_group.weight,
$tableName = 'civicrm_grant';
break;
+ case 'Case':
+ $tableName = 'civicrm_case';
+ break;
+