You are here

function content_migrate_extract_allowed_values in Content Construction Kit (CCK) 7.3

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.

Parameters

string $legacy_values: D6-style string containing key|value pairs delimited by newlines.

string $field_type: The type of field to build the allowed_values array for. Optional.

Return value

array Array of keys and values.

See also

list_allowed_values_string()

list_extract_allowed_values()

3 calls to content_migrate_extract_allowed_values()
number_content_migrate_field_alter in modules/content_migrate/modules/content_migrate.number.inc
Implements hook_content_migrate_field_alter().
text_content_migrate_data_record_alter in modules/content_migrate/modules/content_migrate.text.inc
Implements hook_content_migrate_data_record_alter().
text_content_migrate_field_alter in modules/content_migrate/modules/content_migrate.text.inc
Implements hook_content_migrate_field_alter().

File

modules/content_migrate/content_migrate.module, line 187
Code For D6 to D7 field data update.

Code

function content_migrate_extract_allowed_values($legacy_values, $field_type = NULL) {
  $allowed_values = array();
  if (!empty($legacy_values)) {
    foreach (explode("\n", $legacy_values) as $value) {
      $value = trim($value);
      if (empty($value) && $value !== '0') {
        continue;
      }
      if (strstr($value, '|')) {
        $parts = explode('|', $value);
        $allowed_values[trim($parts[0])] = trim($parts[1]);
      }
      else {
        $allowed_values[$value] = $value;
      }
    }
  }

  // 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;
}