I am in need of creating a unique number for nodes within a particular content type. Has anyone done this and can point me to any references? I thought the "Computed Field" would be my best bet, but I'm not sure? Thanks!

Comments

emdalton’s picture

I need this too. I don't know how to get a "Computed Field" to find the last node of a given content type to auto-populate the field of the new node. I was looking at how Casetracker does it. Here's some sample code:

From casetracker.module:

from function casetracker_nodeapi

 case 'insert':
      // cases: generate a case ID and send it along.
      if (in_array($node->type, variable_get('casetracker_case_node_types', array('casetracker_basic_case')), TRUE)) {
        $node->case_number = _casetracker_next_case_number($node->pid); // $node->case_number is used in casetracker_mail_send().
        db_query("INSERT INTO {casetracker_case} (nid, vid, pid, case_priority_id, case_type_id, case_status_id, assign_to, case_number) VALUES (%d, %d, %d, %d, %d, %d, %d, %d)", $node->nid, $node->vid, $node->pid, $node->case_priority_id, $node->case_type_id, $node->case_status_id, casetracker_get_uid($node->assign_to), $node->case_number);
      }


/**
 * Returns the next case number for use in a project. Case numbers are
 * unique to a project, so there will be multiple case number 100s, etc.
 * See also _casetracker_next_project_number().
 *
 * @param $project_id
 *   The node ID of the project this case is assigned to.
 */
function _casetracker_next_case_number($project_id) {
  $project_case_numbers = variable_get('casetracker_current_case_numbers', array());
  $case_number = ++$project_case_numbers[$project_id]; // cases increment by one per project.
  variable_set('casetracker_current_case_numbers', $project_case_numbers);
  return $case_number;
}

I looked at the instructions for how to make a new module to implement a CCK field type, but I'm a bit overwhelmed. Any guidance would be extremely appreciated.

In my specific case I need the sequence to be alphanumeric (I only have a 3-character wide field to work with, due to a legacy business process), but I can convert to that from an ordinary decimal sequential field if I can figure out how to get one working.

brst t’s picture

I used the same bit of casetracker code to model my solutions using the computed field type in CCK. I set the Data Type to varchar() and the Length to 10

I started with the simple next_project_number function to set up a simple counter:

  $my_counter = variable_get('current_my_counter', 0) + 1;
  variable_set('current_my_counter', $my_counter);

  $node_field[0]['value'] = $my_counter;

And then as a step toward unique numbers per issue per organic group I set it to grab the current group node nid to prefix the counter:

  $currentGroup = og_get_group_context();
  $gid02 = $currentGroup->nid;


  $my_counter = variable_get('current_my_counter', 0) + 1;
  variable_set('current_my_counter', $my_counter);

  $node_field[0]['value'] = ((int)$gid02 . '-' . $my_counter);

But since that didn't provide any more utility than simply drawing upon $node->nid, I still wanted the counter to tick on a per group, per content type basis. (My organic 'group' pages are cck defined project pages.)

So I replaced the simple 'add 1' next_project_counter function with an adapted version of the next_case_number function from the casetracker module, which solves this by storing the info as an array. And where casetracker was set to increment by one per casetracker's $projectid, I set it to increment by one per organic group nid.

  $currentGroup = og_get_group_context();
  $gid02 = $currentGroup->nid;


  $project_issue_numbers = variable_get('issuetracker_current_numbers', array());
  $issue_number = ++$project_issue_numbers[$gid02]; // numbers increment by one per organic group id.
  variable_set('issuetracker_current_numbers', $project_issue_numbers);

  $node_field[0]['value']  = ((int)$gid02 . '-' . $issue_number);
jbrown’s picture

I have just written a module that implements this: http://drupal.org/project/type_local_nids

brst t’s picture

That [lnid] token for pathauto is a far better solution than having an extra counter field and very handy for structuring the urls of organic groups.

I'm using it to suffix friendly URL names for node types with /[og-id]/[lnid]

eg. content/[og-id]/[lnid] and book//[og-id]/[lnid]

(but the pathauto dependency didn't show up in the module listing >> should go in the .info file)

Much better than the extra counter computed field I posted above, especially since it was also incrementing on comment submission, too.

Thanks.

jbrown’s picture

Assigned: Unassigned » jbrown
Status: Active » Fixed

type_local_nids doesn't have any dependencies. If token is installed, it will expose the lnid via token. Pathauto has support for token, but it is optional.

brst t’s picture

No formal dependencies, sure - but I don't understand its use if token isn't installed? I suppose the project description satisfies that.

Not so much a cck issue, but I am glad I found a pointer to this module. Thanks again.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

rajmataj’s picture

This module sounds like it will do what you want, and works for both D6 and D7:

http://drupal.org/project/serial