Prepare a menu item for selecting the field name, providing the search text & replace text.
/**
* Implements hook_menu().
*/
function custom_menu() {
$items = array();
/* To replace content like URL's in body fields */
$items['admin/config/development/replace-content'] = array(
'title' => 'Replace Content',
'description' => t("To replace contents like URL's in Body field when Database deployed from different environment"),
'page callback' => 'drupal_get_form',
'page arguments' => array('replace_content'),
'access arguments' => array('replace content'),
);
return $items;
}
/**
* Configuration form for Replacing Contents in the Database based on the selected fields
*/
function replace_content($form, &$form_state){
global $base_url;
$fieldsList = field_info_field_map();
$field_arr = array();
if(is_array($fieldsList) && count($fieldsList)){
foreach($fieldsList as $fieldName => $fieldDetails){
$field_arr[$fieldName] = $fieldName;
}
}
$form['search_parameter'] = array(
'#type' => 'textfield',
'#title' => t('Enter text to search in body field'),
'#default_value' => '
http://www.google.com',
'#required' => TRUE
);
$form['replace_parameter'] = array(
'#type' => 'textfield',
'#title' => t('Enter text to replace in body field'),
'#default_value' => $base_url,
'#required' => TRUE
);
$form['search_in_fields'] = array(
'#type' => 'select',
'#title' => 'Fields',
'#options' => $field_arr,
'#description' => t('Select the fields in which the content has to get replace'),
'#multiple' => TRUE,
'#required' => TRUE
);
$form['submit'] = array( '#type' => 'submit', '#value' => t('Replace'));
return $form;
}
/* Replace content submit function */
function replace_content_submit($form, &$form_state){
$values = $form_state['values'];
$search_parameter = $values['search_parameter'];
$replace_parameter = $values['replace_parameter'];
$search_in_fields = $values['search_in_fields'];
$affected_rows_exists = FALSE;
foreach($search_in_fields as $selected_field){
$table_name = 'field_data_' . $selected_field;
$revision_table_name = 'field_revision_' . $selected_field;
$field_name = $selected_field . '_value';
if(db_table_exists($table_name)){
$affected_rows = db_update($table_name)
->expression($field_name, "replace($field_name, :search, :replace)",
array(':search' => $search_parameter, ':replace' =>
$replace_parameter))
->execute();
if($affected_rows){
$affected_rows_exists = TRUE;
$row_text = (count($affected_rows)>1) ? 'rows' : 'row';
drupal_set_message(t($affected_rows . ' ' . $row_text . ' got affected in ' . $table_name . '.'));
}
$affected_revision_rows = db_update($revision_table_name)
->expression($field_name, "replace($field_name, :search, :replace)",
array(':search' => $search_parameter, ':replace' =>
$replace_parameter))
->execute();
if($affected_revision_rows){
$affected_rows_exists = TRUE;
$row_text = (count($affected_revision_rows)>1) ? 'rows' : 'row';
drupal_set_message(t($affected_revision_rows .
' ' . $row_text . ' got affected in ' . $revision_table_name . '.'));
}
}else{
exit;
}
}
if(!$affected_rows_exists){
drupal_set_message(t('None of the rows got affected.'));
}
}