Date errors upon programatically inserting multiple nodes in Drupal
I am building a Drupal module that (among other things) creates multiple nodes of type 'individual_match' when a node of type 'team_match' is created. A team_match is a collection of related individual matches.
I used the method explained on Advomatic, which I found in an old Stack Overflow post. It works great (meaning it creates the node) but it returns a lot of error messages, all pertaining to a null date.
Here is the relevant code from the hook_nodeapi function:
case 'insert':
// insert individual matches when a team match is created
if($node->type == 'team_match') {
// Create node of type 'individual_match'
// will be a loop, once date errors are sorted out
$form_state = array();
$tmp_node = new stdClass();
$tmp_node->type = 'individual_match';
// create form for new node
$form_state['values']['type'] = $tmp_node->type;
$form_state['values']['status'] = 1;
$form_state['values']['title'] = $node->title;
$form_state['values']['uid'] = $node->uid;
$form_state['values']['name'] = $node->name;
$form_state['values']['op'] = t('Save'); // This seems to be a required value.
// Create the content.
drupal_execute($tmp_node->type.'_node_form', $form_state, &$tmp_node);
}
break;
And here are the error messages:
- warning: date_offset_get() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 491.
- warning: date_format() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 494.
- warning: date_format() expects par开发者_开发百科ameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 495.
- warning: date_offset_get() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 497.
- warning: date_timezone_set() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 498.
- warning: date_timezone_set() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 499.
- warning: date_format() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 500.
- warning: date_format() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 501.
- warning: date_timezone_set() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 509.
- warning: date_timezone_set() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 510.
- warning: date_format() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 511.
- warning: date_format() expects parameter 1 to be DateTime, null given in /var/www/vhosts/example.com/httpdocs/sites/all/modules/date/date/date_elements.inc on line 514.
This proved to be a required CCK datetime field on the 'individual_match' content type. Setting it in the function, or not making it required, solves this problem.
精彩评论