1

I want to set a unique 7-digit code to a custom content type and display it in a disabled textfield when a node is created. I created a CCK textfield and set it to disabled in hook_form_alter, great.

I think that a good way to make this unique ID is just to use the node id and fill it out to the required length (eg. 0000012). It doesn't look like $nid is available at the node/add form though. How can I set this ID field to default to the $nid? Can I access the hook_form_alter() as the content is created (on form submission)?

3 Answers 3

1

Implement a field in PHP.

It can keep its own serial counter in a variable, and auto-fill itself with that. You can create formatters for all the different use cases yourself.

Field example code here: http://drupal.org/project/examples

Also I made a UUID field you could hack on: http://drupal.org/sandbox/Mile23/1326218

1

You're right, the nid is not available at this point, it's assigned only after the the node is saved.

The easiest solution is most probably to use hook_node_insert and resave the node after having added the value to the field you have created.

0

Looking at the code of node_save(), which the function used to save a node, the hooks that are invoked after drupal_write_record(), which is the function used to set the node ID, are the following ones (when the node is created):

// Call the node specific callback (if any). This can be
// node_invoke($node, 'insert') or
// node_invoke($node, 'update').
node_invoke($node, $op);

// Save fields.
$function = "field_attach_$op";
$function('node', $node);

module_invoke_all('node_' . $op, $node);
module_invoke_all('entity_' . $op, $node, 'node');

 

Can I access the hook_form_alter() as the content is created (on form submission)?

hook_form_alter() is invoked for a node edit form whenever the node is being created, or the node is being updated. You can add a custom submission handler from hook_form_alter(), which then is able to alter the node being saved.
The hooks I have reported should be sufficient for what you want to do, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.