CCK Date Field with node.save

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by cdraptor on July 30, 2007 at 12:04am

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月01日T01:00:00
[value2] => 2007年08月01日T01: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月01日T01:00:00
[value2] => 2007年08月01日T01:50:00
[timezone] => EST
[offset] => 0
)

What is the date module looking for, any ideas

Comments

You said a field comes back empty

Posted by Chris Charlton on July 31, 2007 at 6:07pm

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

Posted by cdraptor on July 31, 2007 at 11:18pm

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

Posted by g10 on August 1, 2007 at 12:24pm

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

Posted by gblicharz15 on April 8, 2008 at 9:51pm

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

Posted by cdraptor on August 1, 2007 at 6:26pm

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

Posted by karens on August 7, 2007 at 12:31am

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

Posted by cdraptor on August 9, 2007 at 5:55pm

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

Posted by karens on August 30, 2007 at 5:19pm

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

Posted by jomesili on September 21, 2009 at 2:01am

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

Posted by h_dries on November 2, 2009 at 11:21am

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月01日T10: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

Posted by gbernier on November 13, 2009 at 6:57pm

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

Posted by gbernier on November 13, 2009 at 7:18pm

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

Posted by carbncl (not verified) on May 23, 2010 at 10:02am
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

Posted by rsvelko on July 28, 2010 at 11:50am

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:

Posted by emanaton on September 14, 2011 at 3:23am

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:

AltStyle によって変換されたページ (->オリジナル) /