| 1 |
<?php
|
| 2 |
// $Id: upload.tokens.inc,v 1.9 2009/07/12 16:27:54 eaton Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Builds placeholder replacement tokens for uploaded files attached to nodes.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_token_info().
|
| 11 |
*/
|
| 12 |
function upload_token_info() {
|
| 13 |
$results['tokens']['node'] = array(
|
| 14 |
'upload' => array(
|
| 15 |
'name' => t('File attachment'),
|
| 16 |
'description' => t('The first file attached to a node, if one exists.'),
|
| 17 |
'type' => 'file',
|
| 18 |
)
|
| 19 |
);
|
| 20 |
return $results;
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implement hook_tokens().
|
| 25 |
*/
|
| 26 |
function upload_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
| 27 |
$replacements = array();
|
| 28 |
|
| 29 |
if ($type == 'node' && !empty($data['node'])) {
|
| 30 |
$node = $data['node'];
|
| 31 |
|
| 32 |
foreach ($tokens as $name => $original) {
|
| 33 |
if ($name == 'upload') {
|
| 34 |
$upload = array_shift($node->files);
|
| 35 |
$replacements[$original] = file_create_url($upload->filepath);
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
if (($upload_tokens = token_find_with_prefix($tokens, 'upload')) && !empty($node->files) && $upload = array_shift($node->files)) {
|
| 40 |
$replacements += token_generate('file', $upload_tokens, array('file' => $upload), $options);
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
return $replacements;
|
| 45 |
}
|