Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:29:07+00:00 2026-06-19T03:29:07+00:00

I am writing a Drupal custom module in which I create a node based

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:29:09+00:00Added an answer on June 19, 2026 at 3:29 am

    The code executed from auto_nodetile_set_title() is the following one. (The comments identifying parts of the code are 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 the generated title isn't too long.
      $node->title = substr($node->title, 0, 255);
      // With that flag we ensure we don't apply the title two times to the same
      // node. See auto_nodetitle_is_needed().
      $node->auto_nodetitle_applied = TRUE;
    

    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]->name would 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.

    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 you are saving a new node, calling auto_nodetitle_set_title() before node_save() would not allow the function to execute the code marked with (2), and use the node ID for the title. Once auto_nodetitle_set_title() is called, you need to call node_save() to save the new title.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing Drupal sites sometimes. This development involves writing custom module and of
I'm writing a Drupal module to integrate with a custom Java-based REST API for
I'm writing a custom php code in my Drupal website. I need to load
Preface: Yes, I've read: http://drupal.org/node/547518 I am writing 'foo' module for Drupal6, where I
I writing custom module for Drupal 7 and got the following warning: Warning: Invalid
I writing a module for Drupal 7. It must get current $node variable in
I am writing a module for Drupal 7, that has 3 submodules. All submodules
I am writing module which allows to quick add users only by providing First
I am writing a drupal module that involves a form with many checkboxes. E.g.
I'm writing a Drupal module that deals with creating new nodes from CSV files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.