1

I usually create and edit nodes, programmatically, using node_load and the node_save, and addind data to fields in this way:

$node->field_custom['lang'][0]['value'] = 'My value';

But in this way I've to check lang, and even if my node is "it" same fields (as terms, or other fields) need "und" and something like this.

Is there way to create and edit nodes loading the form and the submitting the form with new values?

1 Answer 1

2

Yep, look at drupal_form_submit.

Retrieves, populates, and processes a form. This function allows you to supply values for form elements and submit a form for processing. Compare to drupal_get_form(), which also builds and processes a form, but does not allow you to supply values. There is no return value, but you can check to see if there are errors by calling form_get_errors().

// register a new user
$form_state = array();
$form_state['values']['name'] = 'robo-user';
$form_state['values']['mail'] = '[email protected]';
$form_state['values']['pass']['pass1'] = 'password';
$form_state['values']['pass']['pass2'] = 'password';
$form_state['values']['op'] = t('Create new account');
drupal_form_submit('user_register_form', $form_state);

I find the easiest way to see what the form takes is to use the Net Panel in Firebug. Turn on Persist in Firebug and post your form, then look at the POST keys and values.

1
  • I'll look at this :) Do you think this is a best solution to create or edit nodes? I was looking also a link to load a form for editing
    – arrubiu
    Nov 20, 2012 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.