سؤال

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!

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

We use custom auto-numbers on several custom record types in our Drupal 6 system (the following should also work for Drupal 7). To make it work, we implemented a rule using the Rules module that fires upon saving the record.

The rule runs ON event After saving content and should verify that you're saving the content type you're interested in. (For example, if you're auto numbering Widgets, add an IF for Content type is Widget.

For your actions, put an *Populate created content's 'custom_number' field* action on the rule (where 'custom_number' is your auto-number field), and follow this example:

$number = db_result(db_query(
    "SELECT MAX(field_custom_number_value) FROM {content_type_widget}"));

if(empty($number) || $number < 200) { // Replace 200 with your starting value
    $number = 200;
} else {
    $number++;
}

return array( 0 => array('value' => $number) );

Every time you save a new widget, the system runs a query to find the max custom_number already assigned. If none are assigned, or a number less than 200 is assigned, the system will assign 200; otherwise, it will assign 1 more than the current max number assigned.

If you wanted to, you could also add a line of debug code to add a watchdog line or other debug event in the event that a number less than your desired minimum is discovered.

The Serial Module generates a table whose name is serial_bundlename_fieldname i.e. If you have added a field name 'field_job_id' for bundle 'Jobs' then a table 'serial_jobs_field_job_id' will be created.

The table contains 2 fields 'sid' and 'nid' 'sid' is auto increment field.

You can use a tool like phpMyAdmin and execute the following query

ALTER TABLE serial_jobs_field_job_id AUTO_INCREMENT =2000;

This should possibly set the starting value for autoincrement field.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top