I'm creating shop like app with Flex frontend and Drupal 7 backend for our offline retail sotres (it started from simple products availability DB, so I don't wanted to pay for monster all-in-one commercial soft). The point is now it grew up to many lines of code and I wanted to move to AMFservice for Services 3 module to send typed data to Flex...
So the problem is all my custom created fields for nodes is names as 'field_NAME' and it meens I need them in Flex ValueObjects to be same named and the code loses it's redability... use product.title, but ca'nt use product.price... use user.name, but can't use user.phone...
I definenly need to get those 'field_' prefixes out! So you here is a function to remove them.
/** * Removes "field_" prefix from all custom created fields in system (including users, nodes, terms, etc.). * @author: Kirill "SlyK" Kuzmitskyy [toopro.org] * @return String telling what tables, fields and data was renamed. */ function _toopro_remove_field_prefix() { $html = ''; $s = drupal_get_complete_schema(TRUE);//print_r($s); $html = 'Tables that was changed:
- '; foreach ($s as $tname => $tshema) if(strpos($tname,'field_data_field_')!==FALSE || strpos($tname,'field_revision_field_')!==FALSE) { if(!db_query("show tables like '$tname'")->rowCount()) continue; //if no such table - it was renamed already - movo to the next $fname = str_replace(array('field_data_field_','field_revision_field_'), array('',''), $tname); $tblNewNamw = str_replace("_field_$fname", "_$fname", $tname); $html .= "
- $tname renamed to $tblNewNamw. Fields renamed:
- ";//html output //RENAME FIELDS foreach ($tshema['fields'] as $tblfname=>$tblfdef) if(strpos($tblfname, "field_{$fname}")!==FALSE){ $renTo = str_replace("field_{$fname}", $fname, $tblfname); //get new name for the field $html .= "
- $tblfname renamed to $renTo
- "; //html output db_change_field($tname, $tblfname, $renTo, $tblfdef); //rename field }$html .= '
- '; }$html.='
'; //RENAME INTABLE VALUES (fields definitions and instances) db_query("update `field_config` set field_name = replace(field_name, 'field_','')"); db_query("update `field_config_instance` set field_name = replace(field_name, 'field_','')"); $html .= 'Fields definitions and instances in "field_config" and "field_config_instance" was renamed.'; drupal_get_complete_schema(TRUE); //update schema cache after renaming fields config data return $html; }
The function will return the list of renamed tables, field and values.
Schema tables changes (rename table fields and table name itself) is done using Schmea API.
P.S. Don't forget to make DB backup.
- Log in to post comments