I am writing a Drupal custom module in which I create a node based on custom values. This is the code which creates node in proper manner.
global $user;
$node = new stdClass();
$node->type = 'my_node_type';
//$node->title = $nodeInfo->title;
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
$node->field_node_refrence_field['und'][0]['nid'] = $nid-of-reference-field;
$node = node_submit($node);
node_save($node);
I have the Node Autotitle module enabled for this content type. Due to that, the title is displayed as blank. I have checked the module, and I found that auto_nodetitle_set_title($node) sets the title. When I use this function in my code nothing happens.
Can anyone give me an idea on how to save the node with node_autotitle settings?
The code executed from
auto_nodetile_set_title()is the following one. (The comments identifying parts of the code are mine.)The first control statement is executed if there is a settings for the title of that content type. If there isn’t, and you are updating a module, then the second control statement is executed, otherwise it is executed the third one.
The title should never be empty, since the module always set it. The only time it could be empty is when Drupal doesn’t have information about the content type used for the node; in that case
$types[$node->type]would be NULL, but$types[$node->type]->namewould raise the error "trying to access the property of something that is not an object."I would use the following code, to save the node.
Since you are saving a new node, calling
auto_nodetitle_set_title()beforenode_save()would not allow the function to execute the code marked with (2), and use the node ID for the title. Onceauto_nodetitle_set_title()is called, you need to callnode_save()to save the new title.