I am using the following function in a custom module, in a hook_nodeapi:
foreach ($node->field_daily_report_equipment_use as $key => $item) {
$equipnode = node_load($item['nid']);
if(certain conditions) {
$comment = '';
$equipmentnode = '';
$comment['comment'] = 'Usage is now' . $node->field_daily_report_equip_usage[$key]['value'];
$comment['uid'] = $node->uid;
$comment['nid'] = $node->field_daily_report_equipment_use[$key]['nid'];
$equipmentnode['field_equipmentlastusagecount'][0]['value'] = $node->field_daily_report_equip_usage[$key]['value'];
$equipmentnode['field_equipmentlastusagerecorded'][0]['value']['date'] = date('n/j/Y', strtotime($node->field_daily_report_date[0]['value']));
$equipmentnode['field_equipmentlastsrvtime'][0]['value']['date'] = date('n/j/Y', strtotime($equipnode->field_equipmentlastsrvtime[0]['value']));
comment_driven_save($equipmentnode, $comment, $errors);
}
}
For the first run through the foreach (or if there is only one row in the array) the comment_driven_save works perfectly. However, on subsequent runs, the submitted comment is malformed, with the comment "2", submittted by "Anonymous", and comment_driven properties not being applied to the node (in fact, no new node revision is created).
I've dumped the $comment and $equipmentnode variables prior to comment_driven_save on all runs, and the data is good (correct comment, uid, nid, and driven properties).
Any ideas? Is there something obviously wrong in my code (certainly a possibility)?
Comments
Comment #1
obrienmd commentedAlso of note - Because the comment is Anonymous, it is queued for approval rather than published immediately... Not that this really matters in this case.
Comment #2
arhak commentedthis will require some debugging to figure why is drupal_execute failing
perhaps related to #260934: Static caching: when drupal_execute()ing multiple forms with same $form_id in a page request, only the first one is validated
there are also (but unlikely) #766146: Support multiple forms with same $form_id on the same page, #398110: node_submit resets $node->uid
Comment #3
obrienmd commentedYikes, this seems quite complicated! Will try to debug more ASAP, but not sure I yet have the technical ability to provide much help...
Comment #4
obrienmd commentedSomeone on IRC mentioned clearning the static cache by initializing $_POST before each run of the loop (and saving the $_POST before the loop executes / restoring it after the loop executes). I tried this, but ended up with the same result.
Comment #5
obrienmd commentedArhak, and recommendations? I would think that my effort in #4 would have invalidated #260934 (as $_POST holds the cache)... I'll have to do more digging on #766146.
Comment #6
arhak commentedI think cdriven can't be pushed much farther without a rewrite to change sensitive pieces of code.
It is time to get more hands on it, a few interested developer and several even more interested users to test and report useful feedback for a lot of configurations, to check whether they hold or not (since writing actual tests would be a titanic goal)