drupaldrupal-7drupal-fieldsdrupal-content-types

Drupal: Auto number field for content-type with starting value


I need a field which autonumbers my content-type, but starts at some value like 2000. I tried Serial Field and Computed Field. Couldn`t figure out how to do it. The number (using Serial Field) does automatically increment, but I cannot define a starting number.

Is there anyway to do this?

Many thanks!


Solution

  • You can do this by using Computed Field. Add a computed field to your content type and set the data type to be integer. In the Computed Field (PHP) textarea this code will work for you:

    if (empty($entity_field[0]['value'])) {
      $count = db_select('node')->fields('node')->condition('type', $entity->type)->execute()->rowCount();
      $entity_field[0]['value'] = $count + 2000;
    }
    

    This checks if a value for this field is already set for this node and if not - it gets the total number of nodes of this content type, adds a constant (2000 in this example) and sets the result as the value for your field.