summaryrefslogtreecommitdiff
path: root/modules/upload/upload.tokens.inc
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2009-08-19 20:19:37 +0000
committerDries Buytaert <dries@buytaert.net>2009-08-19 20:19:37 +0000
commit40003c8307ca64da0cd1ab0ee4d87f4812be8088 (patch)
tree05aa427a035a991cc82b68e42d5eb2282273bac4 /modules/upload/upload.tokens.inc
parente998857eb8a5ef4d2ebe381a57c19b1b355fe4ef (diff)
downloadbrdo-40003c8307ca64da0cd1ab0ee4d87f4812be8088.tar.gz
brdo-40003c8307ca64da0cd1ab0ee4d87f4812be8088.tar.bz2
- Patch #113614 by eaton, fago, et al: add centralized token/placeholder subsituation to core.
Diffstat (limited to 'modules/upload/upload.tokens.inc')
-rw-r--r--modules/upload/upload.tokens.inc45
1 files changed, 45 insertions, 0 deletions
diff --git a/modules/upload/upload.tokens.inc b/modules/upload/upload.tokens.inc
new file mode 100644
index 000000000..92c18367a
--- /dev/null
+++ b/modules/upload/upload.tokens.inc
@@ -0,0 +1,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;
+}