I am wrtitting one drupal custom module in which i have to create node based on custom values. Here is 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);

BUT, i have auto_nodetitle module enabled for this content type. Due to that, Title is displaying as blank. I have checked module file and found that there is function of "auto_nodetitle_set_title($node)" to set title, but when i used this function in my code nothing happens.

Can anyone give me idea how to save node with auto_nodetitle settings in custom module?

Thank you
Tejas Mehta

Comments

atw2525’s picture

Issue summary: View changes

This question was asked years ago but in case anyone else runs into this issue like I did, what worked for me was to call directly auto_nodetitle_set_title($node) and then call node_save($node) to apply the changes. This assumes that any fields the node title is based on have been set by the time auto_nodetitle_set_title($node) is called.

msankhala’s picture

I think the better way could be that auto_nodetitle provide a hook which other modules can implement something like hook_auto_nodetitle_alter :

mymodule_auto_nodetitle_alter(&$node) {
  // modify node title here.
}

This can be easily implemented.

msankhala’s picture

Here is an implementation of that.

msankhala’s picture

Status: Active » Needs review
gaurav.kapoor’s picture

I applied your patch and also added the hook to another custom module. I was able to modify node title with help of that hook. I feel it is a very useful addition to the module. Pushing this for the time being. Please report if you find any issues.

gaurav.kapoor’s picture

Status: Needs review » Fixed
msankhala’s picture

Sweet. Added a child issue to update README.txt #2961030: Update README.txt to mention about hook_auto_nodetitle_alter

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.