一个简单的草稿脚本即可解救!我们会更新字段数据和字段修订表,并在手动更改字段设置之前用新值替换旧值。
如果我们在当前字段设置中有类似这样的内容:
&date=today|today
&date=last2days|last2days
并希望将其替换为以下内容:
date=today|today
date=last2days|last2days
我们首先运行drush脚本,然后在管理界面中更改字段设置。
注意:该代码用于机器名称为的字段field_foo_bar
。
$field_name = 'foo_bar';
print "for {$field_name}...\n";
replace_field("&date=today", "date=today", $field_name);
replace_field("&date=last2days", "date=last2days", $field_name);
function replace_field($old_value, $new_value, $field_name, $entity_type='node') {
print "Replacing {$old_value} with {$new_value}...\n";
$data_count = replace_options_data_field($field_name, $entity_type, $old_value, $new_value);
$revision_count = replace_options_revision_field($field_name, $entity_type, $old_value, $new_value);
print $data_count + $revision_count . " entries replaced.\n";
}
function replace_options_data_field($field_name, $entity_type, $old_value, $new_value) {
$num_updated = db_update('field_data_field_' . $field_name)
->fields(array(
'field_' . $field_name . '_value' => $new_value,
))
->condition('entity_type', $entity_type)
->condition('field_' . $field_name . '_value', $old_value)
->execute();
return $num_updated;
}
function replace_options_revision_field($field_name, $entity_type, $old_value, $new_value) {
$num_updated = db_update('field_revision_field_' . $field_name)
->fields(array(
'field_' . $field_name . '_value' => $new_value,
))
->condition('entity_type', $entity_type)
->condition('field_' . $field_name . '_value', $old_value)
->execute();
return $num_updated;
}