CCK Date Field with node.save

Events happening in the community are now at Drupal community events on www.drupal.org.
cdraptor's picture

I've run into a problem with trying to update a CCK date field using the node.save. Here is the node.load so you can see the overall structure of our event node
stdClass Object
(
[nid] => 49
[vid] => 50
[type] => event
[status] => 1
[created] => 1185652917
[changed] => 1185652917
[comment] => 2
[promote] => 0
[sticky] => 0
[revision_timestamp] => 1185652917
[title] => Dang Drupal Date Field
[body] => Testing from Drupal Entry

[teaser] => Testing from Drupal Entry
[log] =>
[format] => 1
[uid] => 1
[name] => whoelse
[picture] =>
[data] => a:0:{}
[last_comment_timestamp] => 1185652917
[last_comment_name] =>
[comment_count] => 0
[taxonomy] => Array
(
)

[files] => Array
(
)

[field_time] => Array
(
[0] => Array
(
[value] => 2007-08-01T01:00:00
[value2] => 2007-08-01T01:50:00
[timezone] => EST
[offset] => 0
)

)

[field_placeid] => Array
(
[0] => Array
(
[value] => 9
)

)

[locations] => Array
(
)

[location] => Array
(
)

[body_value] => Testing from Drupal Entry
)

Now here is some sample Flex 2 code

        var edit:Object = new Object;

        var fromDate:Date = new Date(2007,10,10,5,0,0,0);
        var toDate:Date = new Date(2007,10,10,6,0,0,0);

        edit.type = "event";            
        edit.title = "HELP ME";
        edit.body = "trying to get the event times to save - why doesn't it";

        edit.uid = 2;
        edit.name = "cdurham";

        edit.field_placeid = new Array({value:9});
        edit.field_time = new Array({value:"10/20/2007 - 5:30:00pm", value2:"10/20/2007 - 6:00:00pm", timezone:"EST", offset:0});
        //edit.field_time = new Array();


        edit.locations = new Array();
        edit.location = new Array();

        eventnode.save(edit);

I have no problem saving to the field_placeid, setting the value 9, comment the edit.field_time and uncomment setting it to an empty new Array() I can save the event node fine, without the time. What I am getting back from the date module validation is a return that From Date is not valid, I hacked up the code a bit to spit out the value it's complaining about only to find it seems to be empty. So I am at somewhat of a loss at how I am supposed to pass.

[field_time] => Array
(
[0] => Array
(
[value] => 2007-08-01T01:00:00
[value2] => 2007-08-01T01:50:00
[timezone] => EST
[offset] => 0
)

What is the date module looking for, any ideas

Comments

You said a field comes back empty

Chris Charlton's picture

You said a field comes back empty. Which field? (I see your field_time array output has both value & value2)

Chris Charlton, Author & Drupal Community Leader, Enterprise Level Consultant

I teach you how to build Drupal Themes http://tinyurl.com/theme-drupal and provide add-on software at http://xtnd.us

What I did was look in the

cdraptor's picture

What I did was look in the date.module and modified the Error message to include the $item['value'] which was showing that it was empty, so I am under the assumption that either "value" is not what input is expected, or somewhere between service node.save -> node.save -> pass off to CCK -> date.module it loses the value. Looks like I will be digging through the drupal code.

Would it solve the problem

g10's picture

Would it solve the problem when using the same formatting for the date?
from flex you pass mm/dd/yyyy - h:mm:ss while the format of the date field is yyyy-mm-ddThh:mm:ss

"date.module can store dates in two ways, as an iso date (YYYY-MM-DDTHH:MM:SS) or a unix timestamp"
from http://drupal.org/project/date

sidenote: I had a similar problem with a text cck field that uses a select list widget which I was unable to solve (the values where either 'false|no' or 'yes|true'… but neither 'false' nor 'no' nor 'false|no' was accepted/saved)…
so better to avoid the select list widget

Saving Select Lists

gblicharz15's picture

I investigated the HTML source code generated by the node view, and noticed that the structure for CCK select lists was different than CCK text fields. I also noticed that the form input names revealed the structure that needed to be passed back using node.save().

Instead of creating a new array and embedding the object, such as:

edit.field_placeid = new Array({value:9});

You send an object with a "key" value:

edit.field_placeid = {key:9};

I tried to put in the

cdraptor's picture

I tried to put in the universal format which after reading the Date Module that should work, it did not again that same thing. I may try the unix timestamp but I really think somewhere the variables are getting lost along the way in the code somewhere. I need to use this so a bunch of debugging down through is what I'm forced to do

Try adding

karens's picture

Try adding node_submit($node) before node_save($node). Lots of things happen on hook_nodeapi($op = submit) in CCK and that hook doesn't get called if you just do node_save(). Many CCK fields will fail to work right if you don't do node_submit() when you try to update values programmatically.

Karen thanks for the

cdraptor's picture

Karen thanks for the feedback, but here's what is happening - here is the code from the services node_service.module

function node_service_save($edit) {

// validate node
node_validate($edit);
if ($errors = form_get_errors()) {
return services_error(implode("\n", $errors));
}

$node = node_submit($edit);
node_save($node);
watchdog('content', t('@type: updated %title.', array('@type' => t($node->type), '%title' => $node->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
return $node;
}

It does not make it down to the node_submit call, in the node_validate I get an error back on the date, I commented out the whole validate and it saves the node, with an error message about the date field and the date field is not saved.

Sorry for not getting back

karens's picture

Sorry for not getting back to you earlier, I've been buried.

Some of the problems with what I see here (if I'm understanding it right) include:

1) node_save() is expecting a complete node object and you're only providing the field info. CCK pulls information from the node that it needs, most importantly the node type, and won't know what to do if that is missing.

2) You're passing in dates that are formatted the way they are in the widget (I'm guessing because I can't tell what widget you're using) instead of the way they are formatted in the field. CCK processing is complex, so the most reliable way of programmatically creating nodes is to create them the way they look in the database, then just do node_submit() and node_save(), don't try to replicate what's going on in the widget. Node_validate() is a step where the data is still in the widget format. By node_save() it has been munged back into the format the database is expecting. I'm guessing that your messages that the data is not valid come from the fact that the module is not getting data in the format it expects it to be at that stage of the processing.

If you look at the code in date_copy.module you can see how to construct a process to programmatically create a node and save it (that's exactly what that module does).

So What Actually Works

jomesili's picture

I not yet clear as to the right format or method for saving dates, could you please give a format or a short tutorial as to how the widget should be and the way to pass the selected Date through AMF so that it is saved by Drupal? I am Using Flex 3, AMFPHP and Drupal.

same problem when updateing date field

h_dries's picture

I have the same problem.
With node_save(), creation succeeds, but update fails. The fields remain unaltered in the db.
Tried node_submit() to no avail.

Btw, the right value to save in the db is of this format:

2009-10-01T10:55:00

(in your date function make sure to escape the T as in:
date('Y-m-d\Tg:i:s',$start)
)

Did you guys figure out how

gbernier's picture

Did you guys figure out how to update the date fields programmitically? I've been trying to use drupal_execute and using the following:

$node->field_featured_date[0]['value'] = date_format($date, 'j F Y'); // This is the format provide on the form when you submit it from the edit page
//$node->field_featured_date[0]['date_type'] = "date_combo";

        // Finally set some things to control the form.
        $form_state['values']['name'] = $node->name;
        $form_state['values']['op'] = t('Save');

        drupal_execute('article_node_form', $form_state, $node);

For some reason the date doesn't change as expected, depending on the format it either goes 1000 years in the future or 1000 years in the past. If someone has figured it out I'd be very appreciative. Like the OP I was able to save other CCK fields like image and normal text this way but the date just refuses to co-operate.

This is what I ended up

gbernier's picture

This is what I ended up doing

// Load the node based on the nid
$node = node_load($nid);
$node->field_featured_date[0]['value'] = $featured_date;

//Have to go this route instead of drupal execute because the cck date module will destroy the day you put in
$node = node_submit($node);

//Create a new revision
_node_save_revision($node, $user->uid);

//Saves the node and new revison to the database
node_save($node);

It by passes the cck validate functions and lets you put your values right to the database. I used the save_revisions so it would keep track of the changes this part is optional.

_node_save_revision required

carbncl's picture

Warning, without _node_save_revision the node update will work but CCK date will create new date entry linked to vid == 0!

i guess another solution would be to retrieve current vid before calling node_save.

Thank you, you saved me some time. :)

Yup - when I use node_save to

rsvelko's picture

Yup - when I use node_save to update an existing node I get the old nid and vid and also have to set $node->revision to a non empty value. Took me an hour or so to figure out...
HTH, hth :)

Late Solution:

emanaton's picture

Old thread, I know, buuut I googled my way here, so I'll leave a note for those that follow.

I got the date fields working with the drupal_execute scheme using the following format; note that my field has a from and to defined, hence the 'value2' here:

<?php
  $form_state
['values']['field_dates'][0] = array(
   
'value' => array(
     
'date' => '09/01/2011',
     
'time' => '03:00',
    ),

   
'value2' => array(
     
'date' => '09/29/2011',
     
'time' => '04:00',
    ),
  );
?>

Happy coding!

Sean P. O. MacCath-Moran
www.emanaton.com

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: