I'm a newbie to drupal 7 for a year by now. I'm trying to make a unique id field that would increment per node of content type I create.

The computed fields module + serial module is the likeliest method accomplishing my goal, yet I'd like to restart the serial number starting each year, I'm totally lost, please help.

This is the code I've used.
The output: 20150001, 20150002 and so on.
Is there a method to restart the serial num. back to 0001 again when it's a new year?

  • Computed Code (PHP)

    $entity_field[0]['value'] = $entity->field_serial[LANGUAGE_NONE][0]['value'];
  • Display Code (PHP)

    $display_output = date('Y') . sprintf('%04s', substr(str_pad($entity_field_item['value'], 4, '0', STR_PAD_LEFT), -4, 4));

This is another method I've found, it works on the D6 website, but turns into a blank on my D7 published contents. It's from a field on D6 using php filter module.

$case_id_query=db_result(db_query("select  max(field_case_id_value) as id  from {content_field_case_id} "));

$case_id = date("Y") . sprintf ( '%04s', substr ( $case_id_query, 4, strlen ( $case_id_query ) - 4 ) + 1 );
return array(
  0 => array('value' =>$case_id),

can I transform the method above to D7?


I recommend in the future to peruse the Module issue queue. :)

There are a number of patches in the following issue thread, one of which I've used to reset yearly (and actually modified to reset monthly): https://www.drupal.org/node/2168071

  • Christian, thank you for your advise! I'll give it try and let you know the outcome. Sep 16 '15 at 5:52
  • It worked!! I've used the developing module version and updated the patch 29 into the revelvent files before installing. Now all I'll need to change is the digits. you've saved my day~ XD Sep 16 '15 at 8:20
  • Fantastic! I'm glad you're making progress! :) As far as the digits go, how do you intend to make them look vs. how they're currently printed?
    – Christian
    Sep 16 '15 at 15:50
  • well, I've intended to cut down the digits of the "0"s after the year, which originally looks like "2015000001", I've cut down the numbers within the serial.inc file of the patch, resolving a "20150001" as result. Sep 17 '15 at 6:47

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.