Drupal 7, validate node as per interface

87 Views Asked by At

I created a webservice, it works correctly, I was doing the node creation operation and it works correctly. I need to validate the node I'm about to save in the same way it is validated during the interface insertion form.

I have tried with

drupal_form_submit($nodeType . '_node_form', $form_state, (object) $node);

it keeps giving me errors its node reference fields

Could you suggest other ways to do the same validation that is done by the interface on a programmatically created node?

The error on the node reference field is:

" field_ente : this entry cannot be referenced. "

The node (6310) exists correctly and if I try to do the node_save, it is saved correctly

the complete function is as follows

function my_ws_resource_create($field_nome = '', $field_cognome = '', $field_codice_fiscale = '', $field_data_di_nascita = '', $field_ente= '')
{

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

    $node = new stdClass();
    $node->type = $nodeType;
    $node->uid = $user->uid;
    $node->status = 1;
    $node->revision = 1;
    $node->promote = 0;
    $node->comment = 0;

    node_object_prepare($node);

    $node->field_cognome['und'][0]['value'] = $field_cognome;
    $node->field_nome['und'][0]['value'] = $field_nome;
    $node->field_codice_fiscale['und'][0]['cck_codicefiscale'] = $field_codice_fiscale;
    $node->field_data_di_nascita['und'][0]['value'] = $field_data_di_nascita;
    $node->field_categoria_contatto['und'][0]['tid'] = '66';



    // $node->field_ente = array('und' => array(array('nid'=> $field_ente )));
    // this field causes the error
    $node->field_ente = array('und' => array(array('nid'=> '6310')));



    $node->field_simplenews_term['it'][0]['tid'] = '13660';

    $form_state = array();      
    $form_state['values']['type'] = $nodeType;    
    $form_state['values']['name'] = $user->name;    
    $form_state['values']['status'] = 1;
    $form_state['values']['promote'] = 1;
    $form_state['values']['sticky'] = 0;

    $form_state['values']['op'] = t('Save');
    drupal_form_submit($nodeType . '_node_form', $form_state, (object) $node);

    if ($errors = form_get_errors()) {
        return services_error(implode(" ", $errors), 406, array('form_errors' => $errors));
    }
    return 'Creation successful';
}
1

There are 1 best solutions below

0
On

I've had success with the following (remove $form_state and replace drupal_form_submit):

if ($node = node_submit($node)) {
  node_save($node);
  // Success!
}
else {
  // Fail :(
}