Issue #1164530 Add messaging system to the content migration process. Display message...
[project/cck.git] / modules / content_migrate / includes / content_migrate.values.inc
1 <?php
2 /**
3 * @file content_migrate.values.inc
4 * Code to produce D7-style field and instance values from the data in D6 tables.
5 * Separated into a separate file for efficiency.
6 */
7 /**
8 * Create a D7-style field array from data stored
9 * in the D6 content field tables.
10 *
11 * @param $field_name
12 * Optionally request only a specific field name.
13 */
14 function _content_migrate_get_field_values($field_name = NULL) {
15 $field_values = &drupal_static(__FUNCTION__);
16 if (!is_array($field_values)) {
17 $field_values = array();
18 }
19 if (empty($field_values) && db_table_exists('content_node_field')) {
20 $field_values = array();
21 $query = db_select('content_node_field', 'nf', array('fetch' => PDO::FETCH_ASSOC));
22 $node_instance_alias = $query->join('content_node_field_instance', 'ni', 'ni.field_name=nf.field_name');
23 $result = $query
24 ->fields($node_instance_alias, array('type_name', 'widget_module', 'widget_type', 'widget_settings', 'display_settings'))
25 ->fields('nf')
26 ->orderBy('type_name')
27 ->execute();
28
29 foreach ($result as $row) {
30 $field_value = $row;
31
32 $query2 = db_select('content_node_field_instance', 'nfi', array('fetch' => PDO::FETCH_ASSOC));
33 $result2 = $query2
34 ->fields('nfi')
35 ->orderBy('type_name')
36 ->condition('field_name', $field_value['field_name'])
37 ->execute();
38
39 $widget_types = array();
40 foreach ($result2 as $row2) {
41 $widget_types[] = $row2['widget_type'];
42 }
43 $widget_types = array_unique($widget_types);
44 if (count($widget_types) > 1) {
45 $field_value['messages'][] = '<span class="error">' . t("Caution: The '@field' field is a shared field that uses different widgets. The '@widget' widget is sometimes used to determine the destination field type for the new field. The migration may not work correctly if other widgets used by this shared field would create different results.", array('@field' => $field_value['field_name'], '@widget' => $row['widget_type'])) .'</span>';
46 }
47
48 // Pass instance settings to the field, in case they are needed.
49 $instance_value = array(
50 'bundle' => $row['type_name'],
51 'widget' => array(
52 'type' => $row['widget_type'],
53 'module' => $row['widget_module'],
54 'settings' => unserialize($row['widget_settings']),
55 ),
56 'display' => array(
57 'settings' => unserialize($row['display_settings']),
58 ),
59 );
60 unset($field_value['widget_type'], $field_value['widget_module'], $field_value['widget_settings'], $field_value['display_settings']);
61
62 // All Drupal 6 fields were attached to nodes.
63 $field_value['entity_types'] = array();
64
65 if ($field_value['multiple'] == 0) {
66 $field_value['cardinality'] = 1;
67 }
68 elseif ($field_value['multiple'] == 1) {
69 $field_value['cardinality'] = FIELD_CARDINALITY_UNLIMITED;
70 }
71 else {
72 $field_value['cardinality'] = $field_value['multiple'];
73 }
74
75 // We need column information for the old table.
76 $field_value['columns'] = unserialize($field_value['db_columns']);
77
78 // Field settings.
79 $default_settings = field_info_field_settings($row['type']);
80 $field_value['settings'] = array_merge($default_settings, unserialize($field_value['global_settings']));
81
82 unset($field_value['multiple'], $field_value['global_settings'], $field_value['required'], $field_value['db_columns']);
83
84 // Let modules change these values.
85 drupal_alter('content_migrate_field', $field_value, $instance_value);
86
87 unset($field_value['allowed_values_php']);
88
89 // We retain $field_value['columns'] and $field_value['db_storage']
90 // even though they are not used or different in D7
91 // so we can find the old table information.
92
93 // Add field definiation to $field_values array.
94 $field_values[$field_value['field_name']] = $field_value;
95 }
96 }
97 if (!empty($field_name)) {
98 return $field_values[$field_name];
99 }
100 return $field_values;
101 }
102
103 /**
104 * Create a D7-style instance array from data stored
105 * in the D6 content field tables.
106 *
107 * @param $bundle
108 * Optionally request only instances of a specific bundle.
109 * @param $field_name
110 * Optionally request only instances of a specific field_name.
111 */
112 function _content_migrate_get_instance_values($bundle = NULL, $field_name = NULL) {
113 $instance_values = &drupal_static(__FUNCTION__);
114
115 if (empty($instance_values) && db_table_exists('content_node_field_instance')) {
116
117 $allowed_fields = field_info_field_types();
118 $allowed_widgets = field_info_widget_types();
119 $allowed_formatters = field_info_formatter_types();
120
121 $instance_values = array();
122
123 $query = db_select('content_node_field_instance', 'ni', array('fetch' => PDO::FETCH_ASSOC));
124 $node_field_alias = $query->join('content_node_field', 'nf', 'ni.field_name=nf.field_name');
125 $result = $query
126 ->fields('ni')
127 ->fields($node_field_alias, array('type', 'module', 'required', 'global_settings'))
128 ->orderBy('label', 'ASC')
129 ->execute();
130
131 foreach ($result as $row) {
132 $instance_value = $row;
133 $instance_value['messages'] = array();
134
135 // Pass field settings to the instance, in case they are needed.
136 $field_value = array(
137 'require' => $instance_value['required'],
138 'type' => $instance_value['type'],
139 'module' => $instance_value['module'],
140 'settings' => unserialize($instance_value['global_settings']),
141 );
142 unset($instance_value['type'], $instance_value['module'], $instance_value['required'], $instance_value['global_settings']);
143
144 // All Drupal 6 instances were attached to nodes.
145 $instance_value['entity_type'] = 'node';
146
147 // Unserialize arrays.
148 foreach (array('widget_settings', 'display_settings', 'global_settings') as $key) {
149 $instance_value[$key] = (!empty($instance_value[$key])) ? (array) unserialize($instance_value[$key]) : array();
150 }
151
152 // Build instance values.
153 $instance_value['bundle'] = $instance_value['type_name'];
154 $instance_value['default_value'] = !empty($instance_value['widget_settings']['default_value']) ? $instance_value['widget_settings']['default_value'] : '';
155
156 // Core does not support this, but retain it so
157 // another module can do something with it
158 // in drupal_alter.
159 if (isset($instance_value['widget_settings']['default_value_php'])) {
160 $instance_value['widget']['settings']['default_value_php'] = $instance_value['widget_settings']['default_value_php'];
161 }
162
163 // Build widget values.
164 $instance_value['widget'] = array();
165
166 // TODO Some widget types have been renamed in D7.
167 $instance_value['widget']['type'] = $instance_value['widget_type'];
168 $instance_value['widget']['weight'] = $instance_value['weight'];
169 $instance_value['widget']['module'] = $instance_value['widget_module'];
170 $instance_value['widget']['active'] = $instance_value['widget_active'];
171
172 $default_settings = field_info_widget_settings($field_value['type']);
173 $instance_value['widget']['settings'] = array_merge($default_settings, $instance_value['widget_settings']);
174
175 // Build display values.
176 $instance_value['display'] = array();
177 $label = $instance_value['display_settings']['label'];
178 $missing_formatters = array();
179 foreach ($instance_value['display_settings'] as $context => $settings) {
180 if ($context == 'label') {
181 continue;
182 }
183 // @TODO Multigroup fields have some unexpected elements, we need to work out what should happen to them.
184 if ($context == 'parent' || $context == 'weight') {
185 continue;
186 }
187 $instance_value['display'][$context]['label'] = $label['format'];
188
189 // The format used in D6 may not match the formatter in D7.
190 // Fix it using drupal_alter().
191 $instance_value['display'][$context]['type'] = $settings['format'];
192 $instance_value['display'][$context]['settings'] = field_info_formatter_settings($settings['format']);
193 $instance_value['display'][$context]['weight'] = $instance_value['weight'];
194 $instance_value['display'][$context]['module'] = $instance_value['widget_module'];
195
196 }
197
198 // Unset unneeded values.
199 unset($instance_value['type_name'], $instance_value['global_settings'], $instance_value['widget_settings'], $instance_value['display_settings'], $instance_value['widget_module'], $instance_value['widget_active']);
200
201 // Unset some values that don't exist on all fields.
202 if (isset($instance_value['widget']['settings']['default_value'])) unset($instance_value['widget']['settings']['default_value']);
203
204 // Let modules change these values.
205 drupal_alter('content_migrate_instance', $instance_value, $field_value);
206
207 // Get rid of this value once CCK or some other module has handled it.
208 if (isset($instance_value['widget']['settings']['default_value_php'])) unset($instance_value['widget']['settings']['default_value_php']);
209
210 // Warn about missing or invalid widgets.
211 if (!array_key_exists($instance_value['widget']['type'], $allowed_widgets)) {
212 $instance_value['messages'][] = t("Missing widget: The '@widget' widget is not available for the @field field, it will be set to the default widget.", array('@widget' => $instance_value['widget']['type'], '@field' => $instance_value['field_name']));
213 }
214
215 // Warn about missing or invalid formatters.
216 foreach ($instance_value['display'] as $context => $settings) {
217 if ($settings['type'] != 'default' && !array_key_exists($settings['type'], $allowed_formatters)) {
218 $missing_formatters[$settings['type']][] = $context;
219 }
220 }
221 foreach ($missing_formatters as $formatter => $context) {
222 $instance_value['messages'][] = t("Missing formatter: The '@formatter' formatter used in @view_mode view modes for the @field field is not available, these displays will be reset to the default formatter.", array('@formatter' => $formatter, '@view_mode' => count($context), '@field' => $instance_value['field_name']));
223 }
224
225 // Add instance information to instance array.
226 $instance_values['instances'][$instance_value['bundle']][$instance_value['field_name']] = $instance_value;
227 $instance_values['fields'][$instance_value['field_name']][$instance_value['bundle']] = $instance_value;
228
229 }
230 }
231 if (!empty($bundle)) {
232 if (!empty($field_name)) {
233 return $instance_values['instances'][$bundle][$field_name];
234 }
235 else {
236 return $instance_values['instances'][$bundle];
237 }
238 }
239 elseif (!empty($field_name)) {
240 return $instance_values['fields'][$field_name];
241 }
242 return $instance_values;
243 }