1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Token integration for the file_entity module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_token_info().
|
10
|
*/
|
11
|
function file_entity_token_info() {
|
12
|
// File type tokens.
|
13
|
$info['types']['file-type'] = array(
|
14
|
'name' => t('File type'),
|
15
|
'description' => t('Tokens associated with file types.'),
|
16
|
'needs-data' => 'file_type',
|
17
|
);
|
18
|
$info['tokens']['file-type']['name'] = array(
|
19
|
'name' => t('Name'),
|
20
|
'description' => t('The name of the file type.'),
|
21
|
);
|
22
|
$info['tokens']['file-type']['machine-name'] = array(
|
23
|
'name' => t('Machine-readable name'),
|
24
|
'description' => t('The unique machine-readable name of the file type.'),
|
25
|
);
|
26
|
$info['tokens']['file-type']['count'] = array(
|
27
|
'name' => t('File count'),
|
28
|
'description' => t('The number of files belonging to the file type.'),
|
29
|
);
|
30
|
$info['tokens']['file-type']['edit-url'] = array(
|
31
|
'name' => t('Edit URL'),
|
32
|
'description' => t("The URL of the file type's edit page."),
|
33
|
);
|
34
|
|
35
|
// File tokens.
|
36
|
$info['tokens']['file']['type'] = array(
|
37
|
'name' => t('File type'),
|
38
|
'description' => t('The file type of the file.'),
|
39
|
'type' => 'file-type',
|
40
|
);
|
41
|
$info['tokens']['file']['download-url'] = array(
|
42
|
'name' => t('Download URL'),
|
43
|
'description' => t('The URL to download the file directly.'),
|
44
|
'type' => 'url',
|
45
|
);
|
46
|
|
47
|
return $info;
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Implements hook_token_info_alter().
|
52
|
*/
|
53
|
function file_entity_token_info_alter(&$info) {
|
54
|
$info['tokens']['file']['name']['description'] = t('The name of the file.');
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Implements hook_tokens().
|
59
|
*/
|
60
|
function file_entity_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
61
|
$replacements = array();
|
62
|
|
63
|
$url_options = array('absolute' => TRUE);
|
64
|
if (isset($options['language'])) {
|
65
|
$url_options['language'] = $options['language'];
|
66
|
$language_code = $options['language']->language;
|
67
|
}
|
68
|
else {
|
69
|
$language_code = NULL;
|
70
|
}
|
71
|
|
72
|
$sanitize = !empty($options['sanitize']);
|
73
|
|
74
|
// File tokens.
|
75
|
if ($type == 'file' && !empty($data['file'])) {
|
76
|
$file = $data['file'];
|
77
|
|
78
|
foreach ($tokens as $name => $original) {
|
79
|
switch ($name) {
|
80
|
case 'type':
|
81
|
if ($file_type = file_type_load($file->type)) {
|
82
|
$replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
|
83
|
}
|
84
|
break;
|
85
|
|
86
|
case 'download-url':
|
87
|
$uri = file_entity_download_uri($file);
|
88
|
$replacements[$original] = url($uri['path'], $uri['options'] + $url_options);
|
89
|
break;
|
90
|
}
|
91
|
}
|
92
|
|
93
|
// Chained token relationships.
|
94
|
if (($file_type_tokens = token_find_with_prefix($tokens, 'type')) && $file_type = file_type_load($file->type)) {
|
95
|
$replacements += token_generate('file-type', $file_type_tokens, array('file_type' => $file_type), $options);
|
96
|
}
|
97
|
if ($download_url_tokens = token_find_with_prefix($tokens, 'download-url')) {
|
98
|
$replacements += token_generate('url', $download_url_tokens, file_entity_download_uri($file), $options);
|
99
|
}
|
100
|
}
|
101
|
|
102
|
// File type tokens.
|
103
|
if ($type == 'file-type' && !empty($data['file_type'])) {
|
104
|
$file_type = $data['file_type'];
|
105
|
|
106
|
foreach ($tokens as $name => $original) {
|
107
|
switch ($name) {
|
108
|
case 'name':
|
109
|
$replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
|
110
|
break;
|
111
|
|
112
|
case 'machine-name':
|
113
|
// This is a machine name so does not ever need to be sanitized.
|
114
|
$replacements[$original] = $file_type->type;
|
115
|
break;
|
116
|
|
117
|
case 'count':
|
118
|
$query = db_select('file_managed');
|
119
|
$query->condition('type', $file_type->type);
|
120
|
$query->addTag('file_type_file_count');
|
121
|
$count = $query->countQuery()->execute()->fetchField();
|
122
|
$replacements[$original] = (int) $count;
|
123
|
break;
|
124
|
|
125
|
case 'edit-url':
|
126
|
$replacements[$original] = url('admin/structure/file-types/manage/' . $file_type->type . '/fields', $url_options);
|
127
|
break;
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
|
132
|
return $replacements;
|
133
|
}
|