Moved filefield_token to a subdirectory.
[project/filefield.git] / filefield_field.inc
1 <?php
2
3 /**
4 * @file
5 * FileField field hooks and callbacks.
6 */
7
8
9 function filefield_field_settings_form($field) {
10 $form = array();
11 $form['list_default'] = array(
12 '#type' => 'radios',
13 '#title' => t('Default list value'),
14 '#options' => array(1 => t('Listed'), 0 => t('Hidden')),
15 '#default_value' => $field['list_default'] === '' ? 1 : (int) $field['list_default'],
16 '#required' => true,
17 '#description' => t('The list option determines whether files are visible on node views. This will be used as the default value for the list option.'),
18 );
19 $form['force_list_default'] = array(
20 '#type' => 'radios',
21 '#title' => t('How should the list value be handled?'),
22 '#options' => array(0 => t('User Configurable. (Users will be able to set the list value per file.)'), 1 => t('Enforce Default. (The default list value will be used for all files, and the list checkbox will not be displayed to users.)')),
23 '#default_value' => $field['force_list_default'] === '' ? 0 : (int) $field['force_list_default'],
24 '#required' => true,
25 );
26 $form['show_description'] = array(
27 '#type' => 'radios',
28 '#title' => t('Description field'),
29 '#default_value' => $field['show_description'] === '' ? 1 : (int) $field['show_description'],
30 '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
31 '#description' => t('When enabled, will display a text field where users may enter a description about the uploaded file.'),
32 );
33
34 return $form;
35 }
36
37 function filefield_field_settings_validate($field) {
38 }
39
40 function filefield_field_settings_save($field) {
41 return array('force_list_default', 'list_default', 'show_description', 'file_formatters');
42 }
43
44 function filefield_field_settings_database_columns($field) {
45 return array(
46 'fid' => array('type' => 'int', 'not null' => FALSE),
47 'list' => array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE),
48 'data' => array('type' => 'text', 'serialize' => true),
49 );
50 }
51
52 function filefield_field_settings_views_data($field) {
53 $data = content_views_field_views_data($field);
54 $db_info = content_database_info($field);
55 $table_alias = content_views_tablename($field);
56
57 // Set our own field handler so that we can hook the file formatter
58 // configuration table into the options form.
59
60 // By defining the relationship, we already have a "Has file" filter
61 // plus all the filters that Views already provides for files.
62 // No need for having a filter by ourselves.
63 unset($data[$table_alias][$field['field_name'] .'_fid']['filter']);
64
65 // Add a relationship for related file.
66 $data[$table_alias][$field['field_name'] .'_fid']['relationship'] = array(
67 'base' => 'files',
68 'field' => $db_info['columns']['fid']['column'],
69 'handler' => 'views_handler_relationship',
70 );
71 return $data;
72 }
73
74
75
76 /**
77 * Implementation of CCK's hook_field().
78 */
79 function filefield_field_load($node, $field, &$items, $teaser, $page) {
80 if (empty($items)) {
81 return array();
82 }
83 foreach ($items as $delta => $item) {
84 // Despite hook_content_is_empty(), CCK still doesn't filter out
85 // empty items from $op = 'load', so we need to do that ourselves.
86 if (empty($item['fid']) || !($file = field_file_load($item['fid']))) {
87 unset($items[$delta]);
88 }
89 else {
90 $item['data'] = unserialize($item['data']);
91 $items[$delta] = array_merge($item, $file);
92 }
93 }
94 $items = array_values($items); // compact deltas
95 return array($field['field_name'] => $items);
96 }
97
98 function filefield_field_insert($node, $field, &$items, $teaser, $page) {
99 return filefield_field_update($node, $field, $items, $teaser, $page);
100 }
101
102 function filefield_field_update($node, $field, &$items, $teaser, $page) {
103 foreach ($items as $delta => $item) {
104 $items[$delta] = field_file_save($node, $item);
105 // Remove items from the array if they have been deleted.
106 if (empty($items[$delta])) unset($items[$delta]);
107 }
108 $items = array_values($items); // compact deltas
109 }
110
111 function filefield_field_delete_revision($node, $field, &$items, $teaser, $page) {
112 foreach ($items as $delta => $item) {
113 // For hook_file_references, remember that this is being deleted.
114 $item['field_name'] = $field['field_name'];
115 if (field_file_delete($item)) unset($items[$delta]);
116 $items = array_values($items); // compact deltas
117 }
118 }
119
120
121 function filefield_field_delete($node, $field, &$items, $teaser, $page) {
122 foreach ($items as $delta => $item) {
123 // For hook_file_references, remember that this is being deleted.
124 $item['field_name'] = $field['field_name'];
125 field_file_delete($item);
126 }
127 }
128
129 function filefield_field_sanitize($node, $field, &$items, $teaser, $page) {
130 foreach ($items as $delta => $item) {
131 // Cleanup $items during node preview.
132 if (empty($item['fid']) || !empty($item['delete'])) {
133 unset($items[$delta]);
134 continue;
135 }
136 // Load the complete file if a filepath is not available.
137 if (!empty($item['fid']) && empty($item['filepath'])) {
138 $items[$delta] = array_merge($item, field_file_load($item['fid']));
139 }
140 // Add nid so formatters can create a link to the node.
141 $items[$delta]['nid'] = $node->nid;
142 }
143 }
144
145