#470272 by Garrett Albright: Remove t() from hook_schema.
[project/filefield.git] / filefield_formatter.inc
1 <?php
2 // $Id$
3 /**
4 * @file
5 * FileField: Defines a CCK file field type.
6 *
7 * Uses content.module to store the fid and field specific metadata,
8 * and Drupal's {files} table to store the actual file data.
9 *
10 * This file contains CCK formatter related functionality.
11 */
12
13 /**
14 * Theme function for the 'default' filefield formatter.
15 */
16 function theme_filefield_formatter_default($element) {
17 $file = $element['#item'];
18 $field = content_fields($element['#field_name']);
19 $output = theme('filefield_item', $file, $field);
20 return $output;
21 }
22
23 /**
24 * Theme function for the 'path_plain' formatter.
25 */
26 function theme_filefield_formatter_path_plain($element) {
27 // Inside a View this function may be called with null data. In that case,
28 // just return.
29 if (empty($element['#item'])) {
30 return '';
31 }
32
33 $field = content_fields($element['#field_name']);
34 $item = $element['#item'];
35 // If there is no image on the database, use default.
36 if (empty($item['fid']) && $field['use_default_file']) {
37 $item = $field['default_file'];
38 }
39 if (empty($item['filepath']) && !empty($item['fid'])) {
40 $item = array_merge($item, field_file_load($item['fid']));
41 }
42
43 return empty($item['filepath']) ? '' : file_create_path($item['filepath']);
44 }
45
46 /**
47 * Theme function for the 'url_plain' formatter.
48 */
49 function theme_filefield_formatter_url_plain($element) {
50 // Inside a View this function may be called with null data. In that case,
51 // just return.
52 if (empty($element['#item'])) {
53 return '';
54 }
55
56 $field = content_fields($element['#field_name']);
57 $item = $element['#item'];
58 // If there is no image on the database, use default.
59 if (empty($item['fid']) && $field['use_default_file']) {
60 $item = $field['default_file'];
61 }
62 if (empty($item['filepath']) && !empty($item['fid'])) {
63 $item = array_merge($item, field_file_load($item['fid']));
64 }
65
66 return empty($item['filepath']) ? '' : file_create_url($item['filepath']);
67 }
68
69 /**
70 * Theme function for any file that is managed by FileField.
71 *
72 * It doesn't really format stuff by itself but rather redirects to other
73 * formatters that are telling us they want to handle the concerned file.
74 *
75 * This function checks if the file may be shown and returns an empty string
76 * if viewing the file is not allowed for any reason. If you need to display it
77 * in any case, please use theme('filefield') instead.
78 */
79 function theme_filefield_item($file, $field) {
80 if (filefield_view_access($field['field_name']) && filefield_file_listed($file, $field)) {
81 return theme('filefield_file', $file);
82 }
83 return '';
84 }
85
86 /**
87 * Return whether a file should be listed when viewing the node.
88 *
89 * @param $file
90 * A populated FileField item.
91 * @param $field
92 * A CCK field instance array.
93 */
94 function filefield_file_listed($file, $field) {
95 if (!empty($field['list_field'])) {
96 return (bool)$file['list'];
97 }
98 return TRUE;
99 }
100
101 /**
102 * Theme function for the 'generic' single file formatter.
103 */
104 function theme_filefield_file($file) {
105 // Views may call this function with a NULL value, return an empty string.
106 if (empty($file['fid'])) {
107 return '';
108 }
109
110 $path = $file['filepath'];
111 $url = file_create_url($path);
112 $icon = theme('filefield_icon', $file);
113
114 // Set options as per anchor format described at
115 // http://microformats.org/wiki/file-format-examples
116 // TODO: Possibly move to until I move to the more complex format described
117 // at http://darrelopry.com/story/microformats-and-media-rfc-if-you-js-or-css
118 $options = array(
119 'attributes' => array(
120 'type' => $file['filemime'] . '; length=' . $file['filesize'],
121 ),
122 );
123
124 // Use the description as the link text if available.
125 if (empty($file['data']['description'])) {
126 $link_text = $file['filename'];
127 }
128 else {
129 $link_text = $file['data']['description'];
130 $options['attributes']['title'] = $file['filename'];
131 }
132
133 return '<div class="filefield-file clear-block">'. $icon . l($link_text, $url, $options) .'</div>';
134 }