2

I'm designing a website for a group of academics. I've created a content type, called "Preprints," where users enter information about a paper they have recently submitted to an outside journal for publication. When created, these nodes are then listed on a Views page, also called "Preprints." The users, however, would like some way of tracking the order in which they submit their Preprint. Thus, I'd like to figure out some way of automatically assigning each node a number or a code. (For example, the first paper of the year would be assigned the code LPS13001, the second would be LPS13002, and so on.) My first thought was to do this through a custom field, but I haven't been able to figure this out. Any help would be greatly appreciated!

2
  • Since you are dealing with publications, you might also be interested in the biblio module: drupal.org/project/biblio. You can add fields to the content type, like the one from the serial module.
    – Ursula
    Dec 21, 2013 at 7:10
  • @Ursula Thanks! Using the Serial module to give this content type an extra field might be even better than my original Preprint content type!
    – user24549
    Dec 21, 2013 at 16:32

2 Answers 2

1

The Serial module allows you to do exactly that. You can simply have an integer number that is incremented per content type and if you wanted a prefix you could do that in the display formatter.

Unlike Drupal's built-in auto-increment node ID, which is global and shared by nodes belonging to all content types, serial fields are managed per content type (D6) / entity (D7). For example, the serial field of an Invoice instance will generate a unique sequential number (starting at 1, then 2, etc.) exclusively for Invoice instances.

2
  • Thanks, is was a really easy fix! The only thing I'm stuck on is the display formatter. I've looked around, but can't see how to do that.
    – user24549
    Dec 21, 2013 at 16:30
  • Try: Simple Field Formatter or just preprocess the field.
    – benjy
    Dec 24, 2013 at 0:40
0

I would create a custom field and then do a hook_form_alter. For example:

//This function we alter the form to add a custom submit handler
function module_name_hook_form_name_alter(&$form, &$form_state)
{
//Add a custom submit handler

 $form['#submit'][] = 'module_name_custom_submit';

}

//This is the custom submit handler, this will assign your custom field value
module_name_custom_submit(&$form, &$form_state)
{
  $form_state['values']['field_your_custom_field']['und'][0]['value'] = module_name_preprint_increment();

}

//This handles incrementing by one
function module_name_preprint_increment()
{
$type = "preprints";
  $nodes = node_load_multiple(array(), array('type' => $type));

  if (!empty($nodes)) {
    $pre_print_numer = "LPS";
    $pre_print_numer .= count($nodes) + 1;
  }
  else {
    $pre_print_numer = "LPS1"; //First preprint ever created
  }

  return $pre_print_number;
}

In short you need to hook your form, add a custom submit handler, then have a function that will do the incrementing for you. Refer to:

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_FORM_ID_alter/7

Your Answer

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