Automatic Node Titles using the NID

This problem falls into one of Drupal's "Which came first, the chicken or the egg?" scenarios. Firstly, the NID is the Node ID, which is the unique number assigned to a piece of content. For example, when you next create a Page, blog entry, etc in Drupal, go to yoursite.com/admin/content/node and hover over the "Edit" link for your new node. You'll notice that the URL is something like yoursite.com/node/10/edit?destination=admin%2Fcontent%2Fnode and in this case, the NID for that piece of content is 10.
 
Now, if you want to automatically populate the title of a particular content type with the NID, we run into a problem where the node hasn't been created yet, so there currently isn't an NID associated to that piece of content and we can't assign an NID without a title...
 
There's currently a few threads on this topic and the one that gave me the solution is absolutely massive: http://drupal.org/node/194197#comment-2149246 so to save you the hassle of reading through the entire thing and trying multiple things, here is a rundown of Tom Freudenberg's fantastic solution.

  1. I've only used this solution in Drupal 7 and it requires the Automatic Node Title module (which you should probably have if you've come to this post), the Rules module and its dependencies, plus the PHP Filter module which comes with Drupal.
  2. When editing the content type you want to affect, select "Automatically generate the title and hide the title field" and leave the pattern field blank.
  3. Create a new rule by going to yoursite.com/admin/config/workflow/rules and clicking + "Add New Rule"
  4. Give your rule a name like "Assigning [nid] to title" and select "After saving new content", click "Save".
  5. Click "+ Add Condition" and select "Node content is of type" then click the content type you'd like to affect and click save.
  6. Click "+ Add Action" and then "Execute Custom PHP code" and paste this into the Value field: auto_nodetitle_operations_update(array($node->nid)); then click Save!

Add a new content type and check to make sure it's working :)

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.