Thursday, 15 September 2011

drupal 7 - How to save node with node_save() and node_autotitle module in custom module? -



drupal 7 - How to save node with node_save() and node_autotitle module in custom module? -

i writing drupal custom module in create node based on custom values. code 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 node autotitle module enabled content type. due that, title displayed blank. have checked module, , found auto_nodetitle_set_title($node) sets title. when utilize function in code nil happens.

can give me thought on how save node node_autotitle settings?

the code executed auto_nodetile_set_title() next one. (the comments identifying parts of code mine.)

$types = node_type_get_types(); $pattern = variable_get('ant_pattern_' . $node->type, ''); // (1) if (trim($pattern)) { $node->changed = request_time; $node->title = _auto_nodetitle_patternprocessor($pattern, $node); } // (2) elseif ($node->nid) { $node->title = t('@type @node-id', array('@type' => $types[$node->type]->name, '@node-id' => $node->nid)); } // (3) else { $node->title = t('@type', array('@type' => $types[$node->type]->name)); } // ensure generated title isn't long. $node->title = substr($node->title, 0, 255); // flag ensure don't apply title 2 times same // node. see auto_nodetitle_is_needed(). $node->auto_nodetitle_applied = true;

the first command statement executed if there settings title of content type. if there isn't, , updating module, sec command statement executed, otherwise executed 3rd one.

the title should never empty, since module set it. time empty when drupal doesn't have info content type used node; in case $types[$node->type] null, $types[$node->type]->name raise error "trying access property of not object."

i utilize next code, save node.

global $user; $node = new stdclass(); $node->type = 'my_node_type'; node_object_prepare($node); $node->uid = $user->uid; $node->language = language_none; $node->field_node_refrence_field[$node->language][0]['nid'] = $nid-of-reference-field; $node = node_submit($node); node_save($node); auto_nodetitle_set_title($node); node_save($node);

since saving new node, calling auto_nodetitle_set_title() before node_save() not allow function execute code marked (2), , utilize node id title. 1 time auto_nodetitle_set_title() called, need phone call node_save() save new title.

drupal-7 nodes

No comments:

Post a Comment