4

How to create a node by programatically submitting the existing node add form, and most importantly using it's validation process instead of re-implementing all of the property/field validation logic so we piggyback off of the existing code.

7
  • possible duplicate of Creating new content/node Also, your reason is wrong - you can use hook_form_alter() to add validators to default node form. Last but not least, it's covered by the docs and their comments on Drupal.org undernode_save
    – Mołot
    Jun 21, 2013 at 12:46
  • Why do think these two questions are related? Jun 21, 2013 at 13:44
  • 1
    Altering node form? What are you talking about mate? Jun 21, 2013 at 14:01
  • 3
    @Mołot Definitely not a dup of the other one IMO; this is specifically asking how to create a node by programatically submitting the existing node add form, and most importantly using it's validation process. It's fairly common practice to do this, for example in service resource callbacks. In such cases there's no point re-implementing all of the property/field validation logic so we piggyback off of the existing code.
    – Clive
    Jun 21, 2013 at 14:09
  • 1
    @FranciscoLuz The mention of node_save() is a bit confusing, I would remove that entirely. The fact that you need to use the default node validation is enough to warrant the question in its own right. The form submission process calls node_save() internally so to say you can't use it might be a bit misleading. Also there aren't any more or less hooks fired by using the form submit method, those are all fired from node_save()
    – Clive
    Jun 21, 2013 at 14:10

1 Answer 1

9

The advantage of this method over node_save is that if you have field validation done by modules like field_validation or your own custom validations that you have previously implemented, the node saving will fail if the data sent does not comply with those validations.

global $user;
module_load_include('inc', 'node', 'node.pages');

$node = (object) array(
  'uid' => $user->uid,
  'name' => $user->name,
  'type' => 'MYCONTENTTYPE',
  'language' => LANGUAGE_NONE,
);
node_object_prepare($node);

// Get default values from attached fields.
$fields = field_info_instances('node');
$form_state = array();
foreach($fields['MYCONTENTTYPE'] as $field_name => $values) {
  $form_state['values'][$field_name] = array(LANGUAGE_NONE => array());
}

// Add the $form_state field values.
$form_state['values']['title'] = 'my title';
$form_state['values']['body'][LANGUAGE_NONE][0] = array(
  'value' => 'my body',
  'format' => 'full_html',
);
$form_state['values']['field_MYFIELD'][LANGUAGE_NONE][0]['value'] = 'blah blah';

// Without this line, not quite sure why, it wont work.
$form_state['values']['op'] = t('Save');

drupal_form_submit('MYCONTENTTYPE_node_form', $form_state, $node);

// Tell watch dog if any of the fields fail validation.
$errors = form_get_errors();
if (!empty($errors)) {
  foreach ($errors as $field_name => $message) {
    watchdog('MYMODULE', '%field: %message', array('%message'=> $message, '%field' => $field_name));
  }
}
8
  • 1
    What if you already have the node object with proper field values ready to be inserted? Do you still have to manually enter the field values in the $form_state['value'] array?
    – Mike
    Jul 11, 2013 at 7:55
  • @Mike No, you don't. I've put it up there only to illustrate how it must be structured, the drupal_form_submit() won't discriminate on how the $form_state['value'] array was put together or where it came from as soon as it is structure like that. Jul 11, 2013 at 15:32
  • So how do you do it? I have a user object and I want to create the $form_state['values'] array automatically
    – Mike
    Jul 12, 2013 at 18:59
  • @Mike I don't see the relation between the user object and the $form_state array, could you elaborate a bit more. Jul 13, 2013 at 1:21
  • IN you example you use write $form_state['values']['title'] = 'title';
    – Mike
    Jul 17, 2013 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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