4 * FileField: Defines a CCK file field type.
6 * Uses content.module to store the fid and field specific metadata,
7 * and Drupal's {files} table to store the actual file data.
9 * This file contains CCK formatter related functionality.
13 * Theme function for the 'default' filefield formatter.
15 function theme_filefield_formatter_default($element) {
16 $file = $element['#item'];
17 $field = content_fields($element['#field_name']);
18 $output = theme('filefield_item', $file, $field);
23 * Theme function for the 'path_plain' formatter.
25 function theme_filefield_formatter_path_plain($element) {
26 // Inside a View this function may be called with null data. In that case,
28 if (empty($element['#item'])) {
32 $field = content_fields($element['#field_name']);
33 $item = $element['#item'];
34 // If there is no image on the database, use default.
35 if (empty($item['fid']) && $field['use_default_file']) {
36 $item = $field['default_file'];
38 if (empty($item['filepath']) && !empty($item['fid'])) {
39 $item = array_merge($item, field_file_load($item['fid']));
41 return empty($item['filepath']) ?
'' : check_plain(file_create_path($item['filepath']));
45 * Theme function for the 'url_plain' formatter.
47 function theme_filefield_formatter_url_plain($element) {
48 // Inside a View this function may be called with null data. In that case,
50 if (empty($element['#item'])) {
54 $field = content_fields($element['#field_name']);
55 $item = $element['#item'];
56 // If there is no image on the database, use default.
57 if (empty($item['fid']) && $field['use_default_file']) {
58 $item = $field['default_file'];
60 if (empty($item['filepath']) && !empty($item['fid'])) {
61 $item = array_merge($item, field_file_load($item['fid']));
64 if (empty($item['filepath'])) {
68 return file_create_url(field_file_urlencode_path($item['filepath']));
72 * Theme function for any file that is managed by FileField.
74 * It doesn't really format stuff by itself but rather redirects to other
75 * formatters that are telling us they want to handle the concerned file.
77 * This function checks if the file may be shown and returns an empty string
78 * if viewing the file is not allowed for any reason. If you need to display it
79 * in any case, please use theme('filefield_file') instead.
81 function theme_filefield_item($file, $field) {
82 if (filefield_view_access($field['field_name']) && filefield_file_listed($file, $field)) {
83 return theme('filefield_file', $file);
89 * Return whether a file should be listed when viewing the node.
92 * A populated FileField item.
94 * A CCK field instance array.
96 function filefield_file_listed($file, $field) {
97 if (!empty($field['list_field'])) {
98 return !empty($file['list']);
104 * Theme function for the 'generic' single file formatter.
106 function theme_filefield_file($file) {
107 // Views may call this function with a NULL value, return an empty string.
108 if (empty($file['fid'])) {
112 $path = $file['filepath'];
113 $url = file_create_url($path);
114 $icon = theme('filefield_icon', $file);
116 // Set options as per anchor format described at
117 // http://microformats.org/wiki/file-format-examples
118 // TODO: Possibly move to until I move to the more complex format described
119 // at http://darrelopry.com/story/microformats-and-media-rfc-if-you-js-or-css
121 'attributes' => array(
122 'type' => $file['filemime'] .
'; length=' .
$file['filesize'],
126 // Use the description as the link text if available.
127 if (empty($file['data']['description'])) {
128 $link_text = $file['filename'];
131 $link_text = $file['data']['description'];
132 $options['attributes']['title'] = $file['filename'];
135 return '<div class="filefield-file">'.
$icon .
l($link_text, $url, $options) .
'</div>';