summaryrefslogtreecommitdiff
path: root/modules/upload/upload.tokens.inc
blob: 92c18367a9272c1ff6bde9e723908465755b1604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
// $Id$

/**
 * @file
 * Builds placeholder replacement tokens for uploaded files attached to nodes.
 */

/**
 * Implement hook_token_info().
 */
function upload_token_info() {
  $results['tokens']['node'] = array(
  'upload' => array(
    'name' => t('File attachment'),
    'description' => t('The first file attached to a node, if one exists.'),
    'type' => 'file',
    )
  );
  return $results;
}

/**
 * Implement hook_tokens().
 */
function upload_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];

    foreach ($tokens as $name => $original) {
      if ($name == 'upload') {
        $upload = array_shift($node->files);
        $replacements[$original] = file_create_url($upload->filepath);
      }
    }

    if (($upload_tokens = token_find_with_prefix($tokens, 'upload')) && !empty($node->files) && $upload = array_shift($node->files)) {
      $replacements += token_generate('file', $upload_tokens, array('file' => $upload), $options);
    }
  }

  return $replacements;
}