Commit 6d662c92 authored by DamienMcKenna's avatar DamienMcKenna Committed by colan

Issue #1913072 by DamienMcKenna, mikehues: Fix checkboxes using the new architecture.

parent 52a40bc9
......@@ -176,7 +176,7 @@ function content_migrate_new_columns($field) {
* @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.
* The type of field to build the allowed_values array for. Optional.
*
* @return array
* Array of keys and values.
......@@ -184,15 +184,9 @@ function content_migrate_new_columns($field) {
* @see list_allowed_values_string()
* @see list_extract_allowed_values()
*/
function content_migrate_extract_allowed_values($legacy_values, $field_type) {
function content_migrate_extract_allowed_values($legacy_values, $field_type = NULL) {
$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);
......@@ -207,5 +201,36 @@ function content_migrate_extract_allowed_values($legacy_values, $field_type) {
}
}
// Fix boolean fields. In D6 the boolean selector used the first value as the
// 'no' option, the second as the 'yes' option; if there was only one value
// the field did not work correctly and it was not possible to save a 'yes'
// value. As a result, it is safe to always use the first value as 'no' and
// the second value as 'yes'.
if ($field_type == 'list_boolean') {
// Work with a copy of the fields.
$new_values = $allowed_values;
// D7 requires boolean fields to have the keys 0 and 1.
$allowed_values = array(
0 => 0,
1 => 1,
);
// Get the keys of the converted data.
$keys = array_keys($new_values);
// Replace the 'no' value. If no value was found, it will default to '0'.
if (!empty($keys)) {
$key = array_shift($keys);
$allowed_values[0] = $new_values[$key];
}
// Replace the 'yes' value. If no value was found, it will default to '1'.
if (!empty($keys)) {
$key = array_shift($keys);
$allowed_values[1] = $new_values[$key];
}
}
return $allowed_values;
}
......@@ -145,4 +145,45 @@ function text_content_migrate_data_record_alter(&$record, $field, $instance) {
}
break;
}
switch ($field['type']) {
case 'list_boolean':
static $legacy_values;
if (is_null($legacy_values)) {
$legacy_values = array();
}
// D6 only supported SQL storage, so it's ok to do this.
$field_name = $field['field_name'] . '_value';
// Replace the legacy value with the new values, which are always numeric.
// The legacy values are inserted via content_migrate_text_field_alter().
if (!empty($record[$field_name])) {
if (!isset($legacy_values[$field['field_name']])) {
// Default to an empty array.
$legacy_values[$field['field_name']] = array();
// Load the D6 field's specs.
$field_specs = db_select('content_node_field', 'nf')
->fields('nf')
->condition('nf.field_name', $field['field_name'])
->execute()
->fetchObject();
$field_specs->global_settings = unserialize($results->global_settings);
// Convert the legacy allowed values.
if (!empty($field_specs->global_settings['allowed_values'])) {
$legacy = content_migrate_extract_allowed_values($field_specs->global_settings['allowed_values'], NULL);
$legacy_values[$field['field_name']] = $legacy;
}
}
$legacy_keys = array_keys($legacy_values[$field['field_name']]);
// Casting this as an integer ensures the value is always either 0 or 1.
$record[$field_name] = (int) array_search($record[$field_name], $legacy_keys);
}
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