7

The scenario goes this way, I have a node type which has lot of Taxonomy term fields in it. It does also have other fields like text, date etc but 90% of it is a taxonomy field. I am aware that a node can be created programatically in two ways,

  • node_save() - Creating a node object and saving it

    $node =  new stdClass();
    
    $node->type = 'article';
    
    $node->field_term[LANGUAGE_NONE][0]['tid'] = 3128 // term id of the term
    
    node_save($node);
    
  • Form submit - creating a form array and submitting using drupal_form_submit

    $node[field_first]['und'] => chennai;
    
    $node[field_second]['und'] => "iphone5";
    
    $node['type'] = 'article'; 
    
    $form_state['values'] = $node;
    
    $form_state['values']['op'] = t('Save');
    
    drupal_form_submit($node_type . '_node_form', $form_state, (object)$node);
    

    with $node having the POST values.

Advantage of using form submit method is Drupal will take care of creating new terms in vocabulary (I am supposed to get more new terms). Form submit can follow form validations which is needed for creating a node.

Is there any method to create nodes faster in to Drupal system which supposed to get more terms in it?

If there is no other method than the above mentioned, what is the best one?

  • Your first approach is slightly faster but if your second approach is easier to maintain use the second one. – jantimon Oct 5 '12 at 10:10
  • The Problem arises when i have more than 12 term fields and each fields having more than 10 values as terms, It is extremely slower and takes much of the resources – Dinesh Kumar Sarangapani Oct 5 '12 at 10:13
  • Then you might write 1 entity field query to find out which terms already exist, create all missing ones and in the end create the nodes like you showed in the first approach. – jantimon Oct 5 '12 at 10:54
  • Yes, as said there are several methods to create a node, all i want is what is the best with a reason – Dinesh Kumar Sarangapani Oct 5 '12 at 11:21
  • 4
    @DineshKumar 'Best' is too subjective a term, you might want to narrow that down to what you actually mean (e.g. most performant). Questions asking for the 'best' of something don't do so well on stack exchange sites. Besides that, the only real answer to this question comes from benchmarking the various methods in your current environment, which only you can do. – Clive Oct 5 '12 at 12:27
4

The safest method is to use drupal_form_submit(). If you take at look at the services module it does the same thing. Form validation should be important if you are creating nodes programmatically. Are you importing or creating multiple nodes at once? If this is the case and you are concerned about performance you can use Drupal's batch api or queue. Batch is great if you need user feedback but if you are creating nodes on cron and don't require a ui with feedback then queue is the way to go. In both cases they take care of memory management for you.

I wouldn't be to concerned about how long it takes to create one node as that is how long it would take with the ui. Unless the reason you are doing it through codes is to increase creation times.

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.