tags:

views:

41

answers:

1

I'm trying to programmatically store CCK type nodes using drupal_execute(). Empty nodes get created; none of the CCK fields is saved. Evidently the first line in drupal_process_form() is responsible:

$form_state['values'] = array();

So my question is why does 'values' get erased, and more importantly, how can I save CCK fields using drupal_execute()?

I tried save_node() too which worked well; however I'd prefer the former since it invokes validation.

+2  A: 

The values array gets cleared in drupal_process_form() because it will be populated from the $_POST global later on. If you take a look at drupal_execute(), you will see that it does the opposite, that is, populating the $_POST global from the passed in $form_state['values'].

I think this is done to allow the form processing to work the same way as it would for a 'normal' form submit, so drupal_execute mainly sets the stage so that everything looks more or less the same as with a 'normal' submit later on.

This means that your approach is basically correct and should work - if it doesn't, there must be something else going wrong. I can not guess what this might be, but you could check this thread for some discussions on your approach, and the CCK field topic specifically (it is a mixed thread discussing the node_save() approach as well, but the linked part might fit your specific situation).

Henrik Opel
Thanks for the explanation. I think I've come across that thread before; it's been helpful. I'm dealing with over 200 fields in a single type (this is just a prototype, it should be normalized eventually) at the moment, and it's rather difficult to locate the culprit. I noticed that form_get_errors() emits messages regarding only the standard fields such as title; is there perhaps a different way to extract validation errors?
Stan
@Stan: There is no different way that I know of. Are you sure it's not just a timing (weight) issue, i.e. that your call to `form_get_errors()` occurs before CCKs validation functions where called?
Henrik Opel
@Henrik Opel: Well `form_get_errors()` is called right after `drupal_execute()`. I'll use `node_save()` for now or until the number of fields becomes more manageable.
Stan

related questions