Autopopulate vocabulary taxonomy terms to a cck field

Apr
2012
25

Some days ago ( 24.April.2012 ) i had to update an existing site . The customer wanted to make some changes like add new data on some cck fields and update some existing modules . According to his proposal i had to give him the option to autocorrect-edit-delete specific cck field values .
So after the update the existing cck field : field_building_location ( location area of a building ) should now be used by the 'admin' user for updating etc. The problem was that there were already like 2000 nodes created and those nodes should not be 'edited' . So the big question was : how do we update the cck field values and letting the 'admin' user to update it without problems without letting him go inside the cck fields option page etc . Basically what i did was to build a new vocabulary with the taxonomy module , the new voc will now store the location data . Then i updated the voc. with all the cck fields of the cck field as they were before the update . So for example if the cck field : field_building_location had in his allowed values area the values : Greece , Turkey , Italy , Romania etc . i copy pasted the location values as new taxonomy terms for the new location vocabulary. After i migrated all the values of the cck field to my new vocabulary i edited the "Allowed values / Php Code" section in order to autopopulate the allowed values with the terms of the voc. So now we have the same list with the same values and no harm done . ( Please consider that the old values will not change or be updated with this code , you need an extra update script , for massive node cck field updates . )
Here is the code ( you need to provide the vocabulary id )

<?php
$sql
= "SELECT * FROM {term_data} WHERE vid = %d order by tid asc";
$result = db_query(db_rewrite_sql($sql), 15);

$terms = array();

while (

$data = db_fetch_object($result)){
 
$term = $data->name;
 
$terms["$term"] = $term;
}
return
$terms;
?>

This is the tricky part

<?php
 $terms
["$term"] = $term;?>

The cck values are stored as text-values and not as term ids . So if i change the above code to $terms[$tid] = $term; i will have issues with the cck field because the previous allowed values list where strings and now tid ( integers ) will be used .
p.s : If you have any admin access permissions issues click here

this is an arrow pointing back to the top of the page