Documentation

Attachment.php

"Attachment" is a pseudo-entity which represents a record in civicrm_file combined with a record in civicrm_entity_file as well as the underlying file content.

For core fields use "entity_table", for custom fields use "field_name"

// Create an attachment for a core field
$result = civicrm_api3('Attachment', 'create', array(
  'entity_table' => 'civicrm_activity',
  'entity_id' => 123,
  'name' => 'README.txt',
  'mime_type' => 'text/plain',
  'content' => 'Please to read the README',
));
$attachment = $result['values'][$result['id']];
echo sprintf("<a href='%s'>View %s</a>", $attachment['url'], $attachment['name']);
// Create an attachment for a custom file field
$result = civicrm_api3('Attachment', 'create', array(
  'field_name' => 'custom_6',
  'entity_id' => 123,
  'name' => 'README.txt',
  'mime_type' => 'text/plain',
  'content' => 'Please to read the README',
));
$attachment = $result['values'][$result['id']];
echo sprintf("<a href='%s'>View %s</a>", $attachment['url'], $attachment['name']);
// Move an existing file and save as an attachment
$result = civicrm_api3('Attachment', 'create', array(
  'entity_table' => 'civicrm_activity',
  'entity_id' => 123,
  'name' => 'README.txt',
  'mime_type' => 'text/plain',
  'options' => array(
     'move-file' => '/tmp/upload1a2b3c4d',
   ),
));
$attachment = $result['values'][$result['id']];
echo sprintf("<a href='%s'>View %s</a>", $attachment['url'], $attachment['name']);

Notes:

  • File content is not returned by default. One must specify 'return => content'.
  • Features which deal with local file system (e.g. passing "options.move-file" or returning a "path") are only valid when executed as a local API (ie "check_permissions"==false)

Table of Contents

Functions

_civicrm_api3_attachment_create_spec()  : mixed
Adjust metadata for "create" action.
civicrm_api3_attachment_create()  : array<string|int, mixed>
Create an Attachment.
_civicrm_api3_attachment_get_spec()  : mixed
Adjust metadata for get action.
civicrm_api3_attachment_get()  : array<string|int, mixed>
Get Attachment.
_civicrm_api3_attachment_delete_spec()  : mixed
Adjust metadata for Attachment delete action.
civicrm_api3_attachment_delete()  : array<string|int, mixed>
Delete Attachment.
__civicrm_api3_attachment_find()  : CRM_Core_DAO
Attachment find helper.
_civicrm_api3_attachment_parse_params()  : array<string|int, mixed>
Attachment parsing helper.
_civicrm_api3_attachment_format_result()  : array<string|int, mixed>
Attachment result formatting helper.
_civicrm_api3_attachment_getfields()  : array<string|int, mixed>
Attachment getfields helper.

Functions

_civicrm_api3_attachment_create_spec()

Adjust metadata for "create" action.

_civicrm_api3_attachment_create_spec(array<string|int, mixed> &$spec) : mixed
Parameters
$spec : array<string|int, mixed>

List of fields.

civicrm_api3_attachment_create()

Create an Attachment.

civicrm_api3_attachment_create(array<string|int, mixed> $params) : array<string|int, mixed>
Parameters
$params : array<string|int, mixed>
Tags
throws
CRM_Core_Exception

validation errors

see
DynamicFKAuthorization
Return values
array<string|int, mixed>

_civicrm_api3_attachment_get_spec()

Adjust metadata for get action.

_civicrm_api3_attachment_get_spec(array<string|int, mixed> &$spec) : mixed
Parameters
$spec : array<string|int, mixed>

List of fields.

civicrm_api3_attachment_get()

Get Attachment.

civicrm_api3_attachment_get(array<string|int, mixed> $params) : array<string|int, mixed>
Parameters
$params : array<string|int, mixed>
Tags
throws
CRM_Core_Exception

validation errors

Return values
array<string|int, mixed>

per APIv3

_civicrm_api3_attachment_delete_spec()

Adjust metadata for Attachment delete action.

_civicrm_api3_attachment_delete_spec(array<string|int, mixed> &$spec) : mixed
Parameters
$spec : array<string|int, mixed>

civicrm_api3_attachment_delete()

Delete Attachment.

civicrm_api3_attachment_delete(array<string|int, mixed> $params) : array<string|int, mixed>
Parameters
$params : array<string|int, mixed>
Tags
throws
CRM_Core_Exception
Return values
array<string|int, mixed>

__civicrm_api3_attachment_find()

Attachment find helper.

__civicrm_api3_attachment_find(array<string|int, mixed> $params, int|null $id, array<string|int, mixed> $file, array<string|int, mixed> $entityFile, bool $isTrusted) : CRM_Core_DAO
Parameters
$params : array<string|int, mixed>
$id : int|null

the user-supplied ID of the attachment record

$file : array<string|int, mixed>

The user-supplied vales for the file (mime_type, description, upload_date).

$entityFile : array<string|int, mixed>

The user-supplied values of the entity-file (entity_table, entity_id).

$isTrusted : bool
Tags
throws
CRM_Core_Exception
Return values
CRM_Core_DAO

_civicrm_api3_attachment_parse_params()

Attachment parsing helper.

_civicrm_api3_attachment_parse_params(array<string|int, mixed> $params) : array<string|int, mixed>
Parameters
$params : array<string|int, mixed>
Tags
throws
CRM_Core_Exception

validation errors

Return values
array<string|int, mixed>

(0 => int $id, 1 => array $file, 2 => array $entityFile, 3 => string $name, 4 => string $content, 5 => string $moveFile, 6 => $isTrusted, 7 => bool $returnContent)

  • array $file: whitelisted fields that can pass through directly to civicrm_file
  • array $entityFile: whitelisted fields that can pass through directly to civicrm_entity_file
  • string $name: the printable name
  • string $moveFile: the full path to a local file whose content should be loaded
  • bool $isTrusted: whether we trust the requester to do sketchy things (like moving files or reassigning entities)
  • bool $returnContent: whether we are expected to return the full content of the file

_civicrm_api3_attachment_format_result()

Attachment result formatting helper.

_civicrm_api3_attachment_format_result(CRM_Core_DAO_File $fileDao, CRM_Core_DAO_EntityFile $entityFileDao, bool $returnContent, bool $isTrusted) : array<string|int, mixed>
Parameters
$fileDao : CRM_Core_DAO_File

Maybe "File" or "File JOIN EntityFile".

$entityFileDao : CRM_Core_DAO_EntityFile

Maybe "EntityFile" or "File JOIN EntityFile".

$returnContent : bool

Whether to return the full content of the file.

$isTrusted : bool

Whether the current request is trusted to perform file-specific operations.

Return values
array<string|int, mixed>

_civicrm_api3_attachment_getfields()

Attachment getfields helper.

_civicrm_api3_attachment_getfields() : array<string|int, mixed>
Return values
array<string|int, mixed>

list of fields (indexed by name)


        
On this page

Search results