3 * @file content_migrate.filefield.inc
4 * Code to implement hook_content_migrate_field_alter, content_migrate_instance_alter() and content_migrate_data_record_alter()
5 * on behalf of the former filefield and imagefield modules, moved into a separate file for efficiency.
9 * Implements hook_content_migrate_field_alter().
11 * Use this to tweak the conversion of field settings
12 * from the D6 style to the D7 style for specific
13 * situations not handled by basic conversion,
14 * as when field types or settings are changed.
16 function content_migrate_filefield_field_alter(&$field_value, $instance_value) {
18 switch ($instance_value['widget']['module']) {
20 // Module names and types changed.
21 $field_value['module'] = 'image';
22 $field_value['type'] = 'image';
23 // default_image is now a field setting.
24 $field_value['settings']['default_image'] = $instance_value['widget']['settings']['default_image'];
27 // Module names and types changed.
28 $field_value['module'] = 'file';
29 $field_value['type'] = 'file';
35 * Implements hook_content_migrate_instance_alter().
37 * Use this to tweak the conversion of instance or widget settings
38 * from the D6 style to the D7 style for specific
39 * situations not handled by basic conversion, as when
40 * formatter or widget names or settings are changed.
42 function content_migrate_filefield_instance_alter(&$instance_value, $field_value) {
44 switch ($instance_value['widget']['module']) {
47 // Module names and types changed.
48 $instance_value['widget']['module'] = 'image';
49 $instance_value['widget']['type'] = 'image';
51 // Most settings became instance settings instead of widget settings, with a couple name changes.
52 $instance_value['settings']['file_directory'] = $instance_value['widget']['settings']['file_path'];
53 unset($instance_value['widget']['settings']['file_path']);
54 $instance_value['settings']['file_extensions'] = $instance_value['widget']['settings']['file_extensions'];
55 unset($instance_value['widget']['settings']['file_extensions']);
58 'max_resolution' => 0,
59 'min_resolution' => 0,
61 foreach ($settings as
$setting => $default_value) {
62 $instance_value['settings'][$setting] = isset($instance_value['widget']['settings'][$setting]) ?
$instance_value['widget']['settings'][$setting] : $default_value;
63 unset($instance_value['widget']['settings'][$setting]);
66 // What is the difference between alt and custom_alt on the old field?
67 $instance_value['settings']['alt_field'] = $instance_value['widget']['settings']['custom_alt'];
68 unset($instance_value['widget']['settings']['custom_alt']);
69 $instance_value['settings']['title_field'] = $instance_value['widget']['settings']['custom_title'];
70 unset($instance_value['widget']['settings']['custom_title']);
72 // Many settings have no place in the new field array.
73 unset($instance_value['widget']['settings']['title']);
74 unset($instance_value['widget']['settings']['alt']);
75 unset($instance_value['widget']['settings']['max_filesize_per_node']);
76 unset($instance_value['widget']['settings']['title_type']);
78 // default_image is now a field setting.
79 unset($instance_value['widget']['settings']['default_image']);
83 // Module names and types changed.
84 $instance_value['widget']['module'] = 'file';
85 $instance_value['widget']['type'] = 'file_generic';
87 // Some settings had name changes.
88 $instance_value['widget']['settings']['file_directory'] = $instance_value['widget']['settings']['file_path'];
89 unset($instance_value['widget']['settings']['file_path']);
90 $instance_value['widget']['settings']['max_filesize'] = $instance_value['widget']['settings']['max_filesize_per_file'];
91 unset($instance_value['widget']['settings']['max_filesize_per_file']);
100 * Implements hook_content_migrate_data_record_alter().
102 * Tweaks individual records in a field.
104 function content_migrate_filefield_data_record_alter(&$record, $field) {
106 switch($field['type']) {
108 // Map D6 imagefield field columns to D7 image field columns.
109 if (!empty($record[$field['field_name'] .
'_title']) && ($data = unserialize($record[$field['field_name'] .
'_title']))) {
110 $record[$field['field_name'] .
'_alt'] = $data['alt'];
111 $record[$field['field_name'] .
'_title'] = $data['title'];
114 unset($record[$field['field_name'] .
'_alt']);
115 unset($record[$field['field_name'] .
'_title']);
120 // Map D6 filefield field columns to D7 file field columns.
121 if (!empty($record[$field['field_name'] .
'_description']) && ($data = unserialize($record[$field['field_name'] .
'_description']))) {
122 $record[$field['field_name'] .
'_description'] = $data['description'];
125 unset($record[$field['field_name'] .
'_description']);
128 // Copies imagefield data from the old 'files' table into 'files_managed' and sets file_usage
129 // Mostly copied from system_update_7061, which does the same for the D6 core 'upload' module
131 $nid = $record['entity_id'];
132 $fid = $record[$field['field_name'] .
'_fid'];
139 $file = db_select('files', 'f')->fields('f', array('fid', 'uid', 'filename', 'filepath', 'filemime', 'filesize', 'timestamp', 'status'))->condition('fid', $fid)->execute()->fetchObject();
141 $basename = variable_get('file_directory_path', conf_path() .
'/files');
142 $scheme = file_default_scheme() .
'://';
144 // We will convert filepaths to uri using the default scheme
145 // and stripping off the existing file directory path.
146 $file->uri
= $scheme .
str_replace($basename, '', $file->filepath
);
147 $file->uri
= file_stream_wrapper_uri_normalize($file->uri
);
148 unset($file->filepath
);
150 // Insert into the file_managed table.
151 // Each fid should only be stored once in file_managed.
152 db_merge('file_managed')
158 'filename' => $file->filename
,
160 'filemime' => $file->filemime
,
161 'filesize' => $file->filesize,
162 'status' => $file->status
,
163 'timestamp' => $file->timestamp
,
167 // Add the usage entry for the file.
168 file_usage_add($file, 'file', 'node', $nid);