Commit 938c86d5 authored by KarenS's avatar KarenS
Browse files

Issue #1092840 by dww, Allowed values for list_number and list_boolean not converted to arrays.

parent dc638ad7
......@@ -213,3 +213,55 @@ function content_migrate_content_migrate_data_record_alter(&$record, $field) {
content_migrate_filefield_data_record_alter($record, $field);
}
/**
* Helper function for migrating D6-style allowed values into D7 arrays.
*
* We could just use list_extract_allowed_values() for this, but we don't
* necessarily want a dependency on list.module while the migration is
* happening and core's function isn't very forgiving for the kinds of values
* that can be found in legacy databases. For example, core considers this
* string invalid:
* 0
* 1|Some checkbox label
* even though that was valid (or at least it worked) in D6.
*
* @param string $legacy_values
* D6-style string containing key|value pairs delimited by newlines.
* @param string $field_type
* The type of field to build the allowed_values array for.
*
* @return array
* Array of keys and values.
*
* @see list_allowed_values_string()
* @see list_extract_allowed_values()
*/
function content_migrate_extract_allowed_values($legacy_values, $field_type) {
$allowed_values = array();
if ($field_type == 'list_boolean') {
// Initialize these so the order is proper, even if both keys aren't
// defined in the legacy string.
$allowed_values = array(0 => 0, 1 => 1);
}
if (!empty($legacy_values)) {
foreach (explode("\n", $legacy_values) as $value) {
$value = trim($value);
if (empty($value)) continue;
if (strstr($value, '|')) {
$parts = explode('|', $value);
$allowed_values[trim($parts[0])] = trim($parts[1]);
}
elseif (is_numeric($value)) {
$allowed_values[$value] = $value;
}
else {
$allowed_values[] = $value;
}
}
}
return $allowed_values;
}
......@@ -14,7 +14,6 @@
* as when field types or settings are changed.
*/
function content_migrate_number_field_alter(&$field_value, $instance_value) {
switch ($field_value['type']) {
case 'number_integer':
......@@ -50,6 +49,13 @@ function content_migrate_number_field_alter(&$field_value, $instance_value) {
$field_value['module'] = 'list';
break;
}
// The allowed values list should now be stored as an array.
$allowed_values = array();
if (!empty($field_value['settings']['allowed_values'])) {
$allowed_values = content_migrate_extract_allowed_values($field_value['settings']['allowed_values'], $field_value['type']);
}
$field_value['settings']['allowed_values'] = $allowed_values;
break;
}
......
......@@ -29,4 +29,4 @@ function content_migrate_optionwidgets_instance_alter(&$instance_value, $field_v
break;
}
}
\ No newline at end of file
}
......@@ -26,22 +26,6 @@ function content_migrate_text_field_alter(&$field_value, $instance_value) {
$field_value['settings']['max_length'] = 255;
}
// The allowed values list should now be stored as an array.
$allowed_values = array();
if (!empty($field_value['settings']['allowed_values'])) {
$allowed_values;
foreach (explode("\n", $field_value['settings']['allowed_values']) as $value) {
if (empty($value)) continue;
if (strstr($value, '|')) {
$parts = explode('|', $value);
$allowed_values[trim($parts[0])] = trim($parts[1]);
}
else {
$allowed_values[] = trim($value);
}
}
}
// Text fields using optionwidgets are
// now List fields.
switch ($instance_value['widget']['type']) {
......@@ -49,18 +33,33 @@ function content_migrate_text_field_alter(&$field_value, $instance_value) {
case 'optionwidgets_select':
$field_value['type'] = 'list_text';
$field_value['module'] = 'list';
$field_value['settings']['allowed_values'] = $allowed_values;
break;
case 'optionwidgets_onoff':
$field_value['type'] = 'list_boolean';
$field_value['module'] = 'list';
$field_value['settings']['allowed_values'] = $allowed_values;
break;
break;
case 'text_textarea':
$field_value['type'] = 'text_long';
unset($field_value['settings']['max_length']);
break;
break;
}
// The allowed values list should now be stored as an array.
switch ($field_value['type']) {
case 'list_text':
case 'list_boolean':
$allowed_values = array();
if (!empty($field_value['settings']['allowed_values'])) {
$allowed_values = content_migrate_extract_allowed_values($field_value['settings']['allowed_values'], $field_value['type']);
}
$field_value['settings']['allowed_values'] = $allowed_values;
break;
}
break;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment