When a node is saved, I want to append the NID to the title of the node.

$node->title = "XYZ_" . $node->nid;

If I put this code inside hook_node_presave(), it won't work because NID($node->nid) is not available.

If I put this code inside hook_node_insert(), NID ($node->nid) is available but still won't work because we can't alter $node values inside hook_node_insert() as the transaction is already under way.

Is there a solution to this problem?

  • There's Automatic Entity Label which lets you use tokens to build a title. Maybe it can do what you want.
    – leymannx
    Sep 8, 2019 at 8:17
  • drupal_register_shutdown_function() solved the issue. Thanks. Sep 8, 2019 at 11:13


Browse other questions tagged or ask your own question.