3

Whenever an user saves a node I want to prefix it with table autoincrement-id so that the id is unique.

So if the title of node is "xyz" then it should be saved as unique id "299999-xyz". Ideally this id should be the node-id which is unique but it may not be possible to know the actual id until the node is saved in the table.

What is the best way to accomplish it?

I'm thinking of changing the title in the form validate function. This id can be numeric time() value.

*PS:*I'll have tons of such nodes so when linking them from other nodes (node-reference) this id will uniquely identify such nodes in "autocomplete textbox" in node forms.

4

I suspect you can do this via a hook_nodeapi when $op == "presave".

function mymodule_nodeapi (&$node, $op, $a3 = NULL, $a4 = NULL)
{
    if ($op == "presave") {
      $unique = mymodule_get_unique_number();
      $title = preg_replace("/^\d+\-/", "", $node->title);
      $node->title = $unique . "-" . $title;
    }
}

The trick is to tweak the PCRE pattern to extract the unique number from the $title. Putting in better delimiyters would help. You could always save off the original title in a CCK field that only uid==1 can edit, and build the node title from that.

EDIT: Just re-read the question. This will change the title every time you save. Not sure if you want that or not. You can edit the logic to only do this once, based on the preg results.

  • I do not see hook_nodeapi on the api.Drupal listings – Ashlar Nov 23 '11 at 15:16
  • 1
    The function is linked in my answer. It is a D4.7-6 hook. D7 split out the ops into individual hooks via the node API. Question is tagged for Drupal 6. – mpdonadio Nov 23 '11 at 15:38
  • During presave will $node->nid be available? – AgA Nov 23 '11 at 18:12
  • No, hence using a made-up function called mymodule_get_unique_number(). – mpdonadio Nov 23 '11 at 18:27
7

Use the module "Automatic Nodetitles". This allows you to use tokens on the title field. Once enabled you can set any pattern for the title. Set your pattern on /admin/content/node-type/YOURCONTENTTYPE to "[nid] - [title]" for example. Already existing nodes can be updated automatically.

  • In case of newly saved nodes what'd nid be replaced with? – AgA Nov 23 '11 at 16:10
  • The nid of the node when its saved. – Lance Nov 23 '11 at 16:19
  • But I'm not able to set title. This module gives only two options so if I enter a title then it'd do nothing. So it'll work only if a node is reedited and not created a new. If I use another cck field as title2 then it can work but then the default title field would be wasted. – AgA Nov 23 '11 at 18:09
  • Also looks like I can't use [nid] for new nodes it seems to be empty – AgA Nov 23 '11 at 18:25
  • You're right, the [nid] is only set on update. Sorry about that. Check this thread: drupal.org/node/194197 – Lance Nov 24 '11 at 6:54
0

This should also be possible using php by invoking hook_insert. This hook is called after the node is saved and the nid is available, so you can then make changes and resave the node.

  • Does this work with all content types, or just when the module instantiating the hook also creates a type via hook_node_info() ? – mpdonadio Nov 23 '11 at 15:23
  • It works with any content type! It's a really neat hook :p – Chapabu Nov 23 '11 at 15:30
  • Curious. The note on the API doc page says "To take action when nodes of any type are inserted (not just nodes of the type(s) defined by this module), use hook_nodeapi() instead." – mpdonadio Nov 23 '11 at 15:49
  • Bah..my bad..hook_insert is the standard for D7. For D6 you still should use hook_nodeapi with the $op as insert :p – Chapabu Nov 23 '11 at 16:54
  • This second level updation I don't want to do. Say during server crash or site errors the node will not be in correct state then – AgA Nov 24 '11 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.