content_migrate.module in Content Construction Kit (CCK) 7.3
Code For D6 to D7 field data update.
Modules can implement hook_content_migrate_field_alter(), hook_content_migrate_instance_alter(), and hook_content_migrate_data_record_alter() to make changes to the settings or data as it is migrated.
File
modules/content_migrate/content_migrate.moduleView source
<?php
/**
* @file
* Code For D6 to D7 field data update.
*
* Modules can implement hook_content_migrate_field_alter(),
* hook_content_migrate_instance_alter(), and hook_content_migrate_data_record_alter()
* to make changes to the settings or data as it is migrated.
*/
define('CONTENT_DB_STORAGE_PER_FIELD', 0);
define('CONTENT_DB_STORAGE_PER_CONTENT_TYPE', 1);
/**
* Implements hook_menu().
*/
function content_migrate_menu() {
$items['admin/structure/content_migrate'] = array(
'title' => 'Migrate fields',
'description' => 'Migrate field settings and data from the Drupal 6 version to the Drupal 7 version.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'content_migrate_select',
),
'access arguments' => array(
'administer content types',
),
'file' => 'includes/content_migrate.admin.inc',
);
return $items;
}
/**
* 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) {
module_load_include('inc', 'content_migrate', 'includes/content_migrate.values');
return _content_migrate_get_field_values($field_name);
}
/**
* 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) {
module_load_include('inc', 'content_migrate', 'includes/content_migrate.values');
return _content_migrate_get_instance_values($bundle, $field_name);
}
/**
* Helper function for finding the table name
* used to store the D6 field data.
*
* @param $field_value
* @param $instance_value
*/
function content_migrate_old_table($field_value, $instance_value) {
$storage = content_migrate_storage_type($field_value);
switch ($storage) {
case CONTENT_DB_STORAGE_PER_CONTENT_TYPE:
$name = $instance_value['bundle'];
return "content_type_{$name}";
case CONTENT_DB_STORAGE_PER_FIELD:
$name = $field_value['field_name'];
return "content_{$name}";
}
}
/**
* Helper function for finding the type of table
* used for storing the D6 field data.
*
* @param $field_value
* @param $instance_value
*/
function content_migrate_storage_type($field_value) {
$storage = CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
if (isset($field_value['db_storage'])) {
return $field_value['db_storage'];
}
elseif ($field_value['cardinality'] != 1 || $field_value['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
$storage = CONTENT_DB_STORAGE_PER_FIELD;
}
else {
$instance_values = content_migrate_get_instance_values(NULL, $field_value['field_name']);
foreach ($instance_values as $bundle => $instance_value) {
if (isset($instance_value['messages'])) {
$messages = array_merge($messages, $instance_value['messages']);
unset($instance_values[$bundle]['messages']);
}
}
if (count($instance_values) > 1) {
$storage = CONTENT_DB_STORAGE_PER_FIELD;
}
}
return $storage;
}
/**
* Helper function to find the table for a
* D7 field array.
*
* @param $field
*/
function content_migrate_new_table($field) {
if (empty($field['storage']['details'])) {
return 'field_data_' . $field['field_name'];
}
$data = $field['storage']['details']['sql'][FIELD_LOAD_CURRENT];
return key($data);
}
function content_migrate_new_revision($field) {
if (empty($field['storage']['details'])) {
return 'field_revision_' . $field['field_name'];
}
$data = $field['storage']['details']['sql'][FIELD_LOAD_REVISION];
return key($data);
}
/**
* Helper function for finding the column names
* used for storing the D6 field data.
*
* @param $field_value
* @param $instance_value
*/
function content_migrate_old_columns($field_value, $instance_value) {
$columns = array();
foreach ($field_value['columns'] as $column_name => $values) {
$columns[$column_name] = $field_value['field_name'] . '_' . $column_name;
}
return $columns;
}
/**
* Helper function for figuring out column names
* to be used when storing D7 field data.
*
* @param unknown_type $field
* @return unknown
*/
function content_migrate_new_columns($field) {
$columns = array();
if (empty($field['storage']['details'])) {
foreach ($field['columns'] as $column_name => $values) {
$columns[$column_name] = $field['field_name'] . '_' . $column_name;
}
return $columns;
}
else {
// There is only one table in this array, but the columns for all tables are the same so it doesn't matter.
foreach ($field['storage']['details']['sql'][FIELD_LOAD_CURRENT] as $table => $columns) {
return $columns;
}
}
}
/**
* 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. Optional.
*
* @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 = 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;
}
Functions
Name![]() |
Description |
---|---|
content_migrate_extract_allowed_values | Helper function for migrating D6-style allowed values into D7 arrays. |
content_migrate_get_field_values | Create a D7-style field array from data stored in the D6 content field tables. |
content_migrate_get_instance_values | Create a D7-style instance array from data stored in the D6 content field tables. |
content_migrate_menu | Implements hook_menu(). |
content_migrate_new_columns | Helper function for figuring out column names to be used when storing D7 field data. |
content_migrate_new_revision | |
content_migrate_new_table | Helper function to find the table for a D7 field array. |
content_migrate_old_columns | Helper function for finding the column names used for storing the D6 field data. |
content_migrate_old_table | Helper function for finding the table name used to store the D6 field data. |
content_migrate_storage_type | Helper function for finding the type of table used for storing the D6 field data. |
Constants
Name![]() |
Description |
---|---|
CONTENT_DB_STORAGE_PER_CONTENT_TYPE | |
CONTENT_DB_STORAGE_PER_FIELD | @file Code For D6 to D7 field data update. |