content_migrate.values.inc 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
/**
 * @file content_migrate.values.inc
 * Code to produce D7-style field and instance values from the data in D6 tables.
 * Separated into a separate file for efficiency.
 */
/**
 * Create a D7-style field array from data stored
 * in the D6 content field tables.
 *
 * @param $field_name 
 *   Optionally request only a specific field name.
 */
function _content_migrate_get_field_values($field_name = NULL) {
  $field_values = &drupal_static(__FUNCTION__);
  if (!is_array($field_values)) {
    $field_values = array();
  }
  if (empty($field_values) && db_table_exists('content_node_field')) {
    $field_values = array();
    $query = db_select('content_node_field', 'nf', array('fetch' => PDO::FETCH_ASSOC));
    $node_instance_alias = $query->join('content_node_field_instance', 'ni', 'ni.field_name=nf.field_name');
    $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
28
29
30
31
      ->execute();
    
    foreach ($result as $row) {
      $field_value = $row;
    
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
      $query2 = db_select('content_node_field_instance', 'nfi', array('fetch' => PDO::FETCH_ASSOC));
      $result2 = $query2
        ->fields('nfi')
        ->orderBy('type_name')
        ->condition('field_name', $field_value['field_name'])
        ->execute();

      $widget_types = array();
      foreach ($result2 as $row2) {
        $widget_types[] = $row2['widget_type'];
      }
      $widget_types = array_unique($widget_types);
      if (count($widget_types) > 1) {
        $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>';
      }

48
49
      // Pass instance settings to the field, in case they are needed.
      $instance_value = array(
50
        'bundle' => $row['type_name'],
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
        'widget' => array(
          'type' => $row['widget_type'],
          'module' => $row['widget_module'],
          'settings' => unserialize($row['widget_settings']),
        ),
        'display' => array(
           'settings' => unserialize($row['display_settings']),
        ),
      );
      unset($field_value['widget_type'], $field_value['widget_module'], $field_value['widget_settings'], $field_value['display_settings']);
      
      // All Drupal 6 fields were attached to nodes.
      $field_value['entity_types'] = array();
      
      if ($field_value['multiple'] == 0) {
        $field_value['cardinality'] = 1;
      }
      elseif ($field_value['multiple'] == 1) {
        $field_value['cardinality'] = FIELD_CARDINALITY_UNLIMITED;
      }
      else {
        $field_value['cardinality'] = $field_value['multiple'];
      }
      
      // We need column information for the old table.
      $field_value['columns'] = unserialize($field_value['db_columns']);
    
      // Field settings.
      $default_settings = field_info_field_settings($row['type']);
      $field_value['settings'] = array_merge($default_settings, unserialize($field_value['global_settings']));
    
      unset($field_value['multiple'], $field_value['global_settings'], $field_value['required'], $field_value['db_columns']);
            
      // Let modules change these values.
      drupal_alter('content_migrate_field', $field_value, $instance_value);
      
      unset($field_value['allowed_values_php']);
      
      // We retain $field_value['columns'] and $field_value['db_storage']
      // even though they are not used or different in D7 
      // so we can find the old table information.
      
      // Add field definiation to $field_values array.
      $field_values[$field_value['field_name']] = $field_value;
    }
  }
  if (!empty($field_name)) {
    return $field_values[$field_name];
  }
  return $field_values;
}

/**
 * Create a D7-style instance array from data stored
 * in the D6 content field tables.
 *
 * @param $bundle
 *   Optionally request only instances of a specific bundle.
 * @param $field_name
 *   Optionally request only instances of a specific field_name.
 */
function _content_migrate_get_instance_values($bundle = NULL, $field_name = NULL) {
  $instance_values = &drupal_static(__FUNCTION__);
114

115
  if (empty($instance_values) && db_table_exists('content_node_field_instance')) {
116
117
118
119
120

    $allowed_fields = field_info_field_types();
    $allowed_widgets = field_info_widget_types();
    $allowed_formatters = field_info_formatter_types();
  
121
    $instance_values = array();
122
  
123
124
125
126
127
128
129
130
131
132
    $query = db_select('content_node_field_instance', 'ni', array('fetch' => PDO::FETCH_ASSOC));
    $node_field_alias = $query->join('content_node_field', 'nf', 'ni.field_name=nf.field_name');
    $result = $query
      ->fields('ni')
      ->fields($node_field_alias, array('type', 'module', 'required', 'global_settings'))
      ->orderBy('label', 'ASC')
      ->execute();
    
    foreach ($result as $row) { 
      $instance_value = $row;
133
134
      $instance_value['messages'] = array();

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
      // Pass field settings to the instance, in case they are needed.
      $field_value = array(
        'require' => $instance_value['required'],
        'type' => $instance_value['type'],
        'module' => $instance_value['module'],
        'settings' => unserialize($instance_value['global_settings']),
      );
      unset($instance_value['type'], $instance_value['module'], $instance_value['required'], $instance_value['global_settings']);      

      // All Drupal 6 instances were attached to nodes.
      $instance_value['entity_type'] = 'node';
    
      // Unserialize arrays.
      foreach (array('widget_settings', 'display_settings', 'global_settings') as $key) {
        $instance_value[$key] = (!empty($instance_value[$key])) ? (array) unserialize($instance_value[$key]) : array();
      }

      // Build instance values.
      $instance_value['bundle'] = $instance_value['type_name'];
      $instance_value['default_value'] = !empty($instance_value['widget_settings']['default_value']) ? $instance_value['widget_settings']['default_value'] : '';
      
      // Core does not support this, but retain it so 
      // another module can do something with it
      // in drupal_alter.
      if (isset($instance_value['widget_settings']['default_value_php'])) {
        $instance_value['widget']['settings']['default_value_php'] = $instance_value['widget_settings']['default_value_php'];
      }

      // Build widget values.
      $instance_value['widget'] = array();
      
      // TODO Some widget types have been renamed in D7.
      $instance_value['widget']['type']     = $instance_value['widget_type'];
      $instance_value['widget']['weight']   = $instance_value['weight'];
      $instance_value['widget']['module']   = $instance_value['widget_module'];
      $instance_value['widget']['active']   = $instance_value['widget_active'];
      
      $default_settings = field_info_widget_settings($field_value['type']);
      $instance_value['widget']['settings'] = array_merge($default_settings, $instance_value['widget_settings']);
    
      // Build display values.
      $instance_value['display'] = array();
      $label = $instance_value['display_settings']['label'];
178
      $missing_formatters = array();
179
      foreach ($instance_value['display_settings'] as $context => $settings) {
180
181
182
        if ($context == 'label') {
          continue;
        }
183
184
185
186
187
188
189
190
191
192
        // @TODO Multigroup fields have some unexpected elements, we need to work out what should happen to them.
        if ($context == 'parent' || $context == 'weight') {
          continue;
        }
        $instance_value['display'][$context]['label'] = $label['format'];
        
        // The format used in D6 may not match the formatter in D7.
        // Fix it using drupal_alter().
        $instance_value['display'][$context]['type'] = $settings['format'];
        $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
199
200
201
202
203
204
205
206
207
208
209
      // Unset unneeded values.
      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']);
      
      // Unset some values that don't exist on all fields.
      if (isset($instance_value['widget']['settings']['default_value'])) unset($instance_value['widget']['settings']['default_value']);

      // Let modules change these values.
      drupal_alter('content_migrate_instance', $instance_value, $field_value);
      
      // Get rid of this value once CCK or some other module has handled it.
      if (isset($instance_value['widget']['settings']['default_value_php'])) unset($instance_value['widget']['settings']['default_value_php']);

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
      // Translate D6 core build modes. Some hook_content_migrate_instance_alter()
      // implementations (e.g. date) need the old indexes to retrieve additional
      // data, so we do this after the hook has run.
      $mapping = array(
        2 => 'search_index',
        3 => 'search_result',
        4 => 'rss',
        5 => 'print',
      );
      $new_displays = array();
      foreach ($instance_value['display'] as $context => $settings) {
        $new_context = isset($mapping[$context]) ? $mapping[$context] : $context;
        $new_displays[$new_context] = $settings;
      }
      // Additionally, set 'default' view mode to the settings used by
      // 'full'.
      $new_displays['default'] = $new_displays['full'];
      $instance_value['display'] = $new_displays;

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
      // Warn about missing or invalid widgets.
      if (!array_key_exists($instance_value['widget']['type'], $allowed_widgets)) {
        $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']));
      }

      // Warn about missing or invalid formatters.
      foreach ($instance_value['display'] as $context => $settings) {
        if ($settings['type'] != 'default' && !array_key_exists($settings['type'], $allowed_formatters)) {
          $missing_formatters[$settings['type']][] = $context;
        }
      }
      foreach ($missing_formatters as $formatter => $context) {
        $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']));
      }

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
      // Add instance information to instance array.
      $instance_values['instances'][$instance_value['bundle']][$instance_value['field_name']] = $instance_value;
      $instance_values['fields'][$instance_value['field_name']][$instance_value['bundle']] = $instance_value;
      
    }
  }
  if (!empty($bundle)) {
    if (!empty($field_name)) {
      return $instance_values['instances'][$bundle][$field_name];
    }
    else {
      return $instance_values['instances'][$bundle];
    }
  }
  elseif (!empty($field_name)) {
    return $instance_values['fields'][$field_name];
  }
  return $instance_values;
}