summaryrefslogtreecommitdiffstats
path: root/filefield_field.inc
blob: ff0fcf81a24e4a2946f869a6cc74094fcbd5df93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php

/**
 * @file
 * FileField field hooks and callbacks.
 */


function filefield_field_settings_form($field) {
  $form = array();
  $form['list_default'] = array(
    '#type' => 'radios',
    '#title' => t('Default list value'),
    '#options' => array(1 => t('Listed'), 0 => t('Hidden')),
    '#default_value' => $field['list_default'] === '' ? 1 : (int) $field['list_default'],
    '#required' => true,
    '#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.'),
  );
  $form['force_list_default'] = array(
    '#type' => 'radios',
    '#title' => t('How should the list value be handled?'),
    '#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.)')),
    '#default_value' => $field['force_list_default'] === '' ? 0 : (int) $field['force_list_default'],
    '#required' => true,
  );
  $form['show_description'] = array(
    '#type' => 'radios',
    '#title' => t('Description field'),
    '#default_value' => $field['show_description'] === '' ? 1 : (int) $field['show_description'],
    '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
    '#description' => t('When enabled, will display a text field where users may enter a description about the uploaded file.'),
  );

  return $form;
}

function filefield_field_settings_validate($field) {
}

function filefield_field_settings_save($field) {
  return array('force_list_default', 'list_default', 'show_description', 'file_formatters');
}

function filefield_field_settings_database_columns($field) {
  return array(
    'fid' => array('type' => 'int', 'not null' => FALSE),
    'list' => array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE),
    'data' => array('type' => 'text', 'serialize' => true),
  );
}

function filefield_field_settings_views_data($field) {
  $data = content_views_field_views_data($field);
  $db_info = content_database_info($field);
  $table_alias = content_views_tablename($field);

  // Set our own field handler so that we can hook the file formatter
  // configuration table into the options form.

  // By defining the relationship, we already have a "Has file" filter
  // plus all the filters that Views already provides for files.
  // No need for having a filter by ourselves.
  unset($data[$table_alias][$field['field_name'] .'_fid']['filter']);

  // Add a relationship for related file.
  $data[$table_alias][$field['field_name'] .'_fid']['relationship'] = array(
    'base' => 'files',
    'field' => $db_info['columns']['fid']['column'],
    'handler' => 'views_handler_relationship',
  );
  return $data;
}



/**
 * Implementation of CCK's hook_field().
 */
function filefield_field_load($node, $field, &$items, $teaser, $page) {
  if (empty($items)) {
    return array();
  }
  foreach ($items as $delta => $item) {
    // Despite hook_content_is_empty(), CCK still doesn't filter out
    // empty items from $op = 'load', so we need to do that ourselves.
    if (empty($item['fid']) || !($file = field_file_load($item['fid']))) {
      unset($items[$delta]);
    }
    else {
      $item['data'] = unserialize($item['data']);
      $items[$delta] = array_merge($item, $file);
    }
  }
  $items = array_values($items); // compact deltas
  return array($field['field_name'] => $items);
}

function filefield_field_insert($node, $field, &$items, $teaser, $page) {
  return filefield_field_update($node, $field, $items, $teaser, $page);
}

function filefield_field_update($node, $field, &$items, $teaser, $page) {
  foreach ($items as $delta => $item) {
    $items[$delta] = field_file_save($node, $item);
    // Remove items from the array if they have been deleted.
    if (empty($items[$delta])) unset($items[$delta]);
  }
  $items = array_values($items); // compact deltas
}

function filefield_field_delete_revision($node, $field, &$items, $teaser, $page) {
  foreach ($items as $delta => $item) {
    // For hook_file_references, remember that this is being deleted.
    $item['field_name'] = $field['field_name'];
    if (field_file_delete($item)) unset($items[$delta]);
    $items = array_values($items); // compact deltas
  }
}


function filefield_field_delete($node, $field, &$items, $teaser, $page) {
  foreach ($items as $delta => $item) {
    // For hook_file_references, remember that this is being deleted.
    $item['field_name'] = $field['field_name'];
    field_file_delete($item);
  }
}

function filefield_field_sanitize($node, $field, &$items, $teaser, $page) {
  foreach ($items as $delta => $item) {
    // Cleanup $items during node preview.
    if (empty($item['fid']) || !empty($item['delete'])) {
      unset($items[$delta]);
      continue;
    }
    // Load the complete file if a filepath is not available.
    if (!empty($item['fid']) && empty($item['filepath'])) {
      $items[$delta] = array_merge($item, field_file_load($item['fid']));
    }
    // Add nid so formatters can create a link to the node.
    $items[$delta]['nid'] = $node->nid;
  }
}