file_entity.install
9.52 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* @file
* Install, update and uninstall functions for the file_entity module.
*/
/**
* Implements hook_schema().
*/
function file_entity_schema() {
$schema['file_display'] = array(
'description' => 'Stores configuration options for file displays.',
'fields' => array(
// @todo Can be refactored as a compond primary key after
// http://drupal.org/node/924236 is implemented.
'name' => array(
'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
),
'status' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
),
'settings' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
),
),
'primary key' => array('name'),
// Exportable support via CTools.
'export' => array(
'key' => 'name',
'key name' => 'Name',
'primary key' => 'name',
// The {file_display}.status field is used to control whether the display
// is active in the display chain. CTools-level disabling is something
// different, and it's not yet clear how to interpret it for file displays.
// Until that's figured out, prevent CTools-level disabling.
'can disable' => FALSE,
'default hook' => 'file_default_displays',
'identifier' => 'file_display',
'api' => array(
'owner' => 'file_entity',
'api' => 'file_default_displays',
'minimum_version' => 1,
'current_version' => 1,
),
),
);
$schema['image_dimensions'] = array(
'description' => 'Cache images dimensions.',
'fields' => array(
'fid' => array(
'description' => 'File ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'height' => array(
'description' => 'The height of the image in pixels.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'width' => array(
'description' => 'The width of the image in pixels..',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('fid'),
'foreign keys' => array(
'file_managed' => array(
'table' => 'file_managed',
'columns' => array('fid' => 'fid'),
),
),
);
return $schema;
}
/**
* Implements hook_schema_alter().
*/
function file_entity_schema_alter(&$schema) {
$schema['file_managed']['fields']['type'] = array(
'description' => 'The type of this file.',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
// If the FILE_TYPE_NONE constant ever changes, then change the value here
// too, and add an update function to deal with existing records. The
// constant isn't used here, because there may be cases where this function
// runs without the module file loaded.
'default' => 'undefined',
);
$schema['file_managed']['indexes']['file_type'] = array('type');
}
/**
* Implements hook_install().
*/
function file_entity_install() {
$schema = array();
file_entity_schema_alter($schema);
$spec = $schema['file_managed']['fields']['type'];
$indexes_new = array('indexes' => $schema['file_managed']['indexes']);
// If another module (e.g., Media) had added a {file_managed}.type field,
// then change it to the expected specification. Otherwise, add the field.
if (db_field_exists('file_managed', 'type')) {
// db_change_field() will fail if any records have type=NULL, so update
// them to the new default value.
db_update('file_managed')->fields(array('type' => $spec['default']))->isNull('type')->execute();
// Indexes using a field being changed must be dropped prior to calling
// db_change_field(). However, the database API doesn't provide a way to do
// this without knowing what the old indexes are. Therefore, it is the
// responsibility of the module that added them to drop them prior to
// allowing this module to be installed.
db_change_field('file_managed', 'type', 'type', $spec, $indexes_new);
}
else {
db_add_field('file_managed', 'type', $spec, $indexes_new);
}
}
/**
* Implements hook_uninstall().
*/
function file_entity_uninstall() {
db_drop_field('file_managed', 'type');
}
/**
* Create the {file_display} database table.
*/
function file_entity_update_7000() {
$schema['file_display'] = array(
'description' => 'Stores configuration options for file displays.',
'fields' => array(
'name' => array(
'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
),
'status' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
),
'settings' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
),
),
'primary key' => array('name'),
);
db_create_table('file_display', $schema['file_display']);
}
/**
* Move file display configurations from the 'file_displays' variable to the
* {file_display} database table.
*/
function file_entity_update_7001() {
$file_displays = variable_get('file_displays');
if (!empty($file_displays)) {
foreach ($file_displays as $file_type => $file_type_displays) {
if (!empty($file_type_displays)) {
foreach ($file_type_displays as $view_mode => $view_mode_displays) {
if (!empty($view_mode_displays)) {
foreach ($view_mode_displays as $formatter_name => $display) {
if (!empty($display)) {
db_merge('file_display')
->key(array(
'name' => implode('__', array($file_type, $view_mode, $formatter_name)),
))
->fields(array(
'status' => isset($display['status']) ? $display['status'] : 0,
'weight' => isset($display['weight']) ? $display['weight'] : 0,
'settings' => isset($display['settings']) ? serialize($display['settings']) : NULL,
))
->execute();
}
}
}
}
}
}
}
variable_del('file_displays');
}
/**
* Drupal 7.8 disallows empty string as the value for a bundle key, so update
* empty {file_managed}.type records to 'undefined' instead.
*/
function file_entity_update_7002() {
db_update('file_managed')
// Using 'undefined' instead of FILE_TYPE_NONE, because update functions can
// run for disabled modules.
->fields(array('type' => 'undefined'))
->condition('type', '')
->execute();
}
/**
* Register the new display formatter.
*/
function file_entity_update_7100() {
}
/**
* Create the {image_dimensions} database table.
*/
function file_entity_update_7101() {
if (!db_table_exists('image_dimensions')){
$schema['image_dimensions'] = array(
'description' => 'Cache images dimensions.',
'fields' => array(
'fid' => array(
'description' => 'File ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'height' => array(
'description' => 'The height of the image in pixels.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'width' => array(
'description' => 'The width of the image in pixels..',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('fid'),
'foreign keys' => array(
'file_managed' => array(
'table' => 'file_managed',
'columns' => array('fid' => 'fid'),
),
),
);
db_create_table('image_dimensions', $schema['image_dimensions']);
}
}