Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Drush script skeleton to find string values in a node field and migrate them to other fields.
<?php
/**
* @file endler-migrate-field-data.php
*
* Drush script to migrate some string from one set of node fields to another.
*
* @author Justin Endler
*/
function update_node_fields($entity_id, $value) {
$edit = node_load($entity_id);
// Populate new field with value. Match the stucture of the particular field.
$edit->field_some_new_field[LANGUAGE_NONE][0]['value'] = $value;
$edit->field_some_new_field[LANGUAGE_NONE][0]['safe_value'] = $value;
// Do something with the old field value.
$edit->field_some_other_field[LANGUAGE_NONE][0]['safe_value'] = str_replace($value, '', $edit->field_some_other_field[LANGUAGE_NONE][0]['safe_value']);
$edit->field_some_other_field[LANGUAGE_NONE][0]['value'] = $edit->field_some_other_field[LANGUAGE_NONE][0]['safe_value'];
node_save($edit);
}
$result = db_query("SELECT f.entity_id, f.field_some_other_field_value FROM {field_some_other_field} f");
$regex_pattern = '/some pattern/';
foreach ($result as $row) {
if (!empty($row->field_some_other_field_value)) {
// Look for pattern.
$match = preg_match($regex_pattern, $row->field_some_other_field_value, $matches);
if ($match === 1) {
update_node_fields($row->entity_id, $value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.