0
votes

Can anyone tell me how I can update a cck field programmatically?

I am trying the following code:

$node->location[field_location][0][city]    =   'tracker city';
    $node->location[field_location][0][latitude]    =   8.888888;
    $node->location[field_location][0][longitude]   =   9.999999;

without any success. I am aware I could directly interact with the database but I am looking for a different solution.

Can anyone help?

2
Where are you putting that code? That will update the value of the field in the node object, but you will still need to save the node after the change. - loganfsmyth
The code is in a custom module in hook_nodeapi. I am checking for presave and then executing the code. I also tried calling node_save after my code but the just breaks the site and results in a white screen with the message "Connection terminated unexpectedly" - sisko
It is possible that your module's weight in the 'system' table is less than the weight of the 'location' module. This would cause your module's 'presave' to be overwritten by the location module. I am guessing both your module and the weight of the 'location' module are set to 0 in the 'system' table. Try changing your module's weight to 1. - tyler.frankenstein

2 Answers

0
votes

You should be able to put this in your presave, and have it save properly. I'm not 100% sure that city, latitude, and longitude are the correct keys, but I'm leaving them since you had them.

$node->field_location[0][city]      = 'tracker city';
$node->field_location[0][latitude]  = 8.888888;
$node->field_location[0][longitude] = 8.999999;

If that does not work, you should install the Devel module and use the Node 'Devel' tab to view the node object and see exactly what the proper format is.

0
votes
<?php
$node->language = LANGUAGE_NONE;
$node->field_custom_name[$node->language][0]['value'] = 'This is a custom field value';
?>

See here for more info.