Update title field in hook_ENTITY_TYPE_insert() I use hook_ENTITY_TYPE_presave() and hook_ENTITY_TYPE_insert() in order to update the node with a title using its ID.

function &lt;module_name&gt;_node_presave($node) {
  // ...
  if ($node-&gt;isNew() &amp;&amp; $node-&gt;getType() == '&lt;custom_content_type&gt;') {
    $node-&gt;title = 'New entity ' . $node-&gt;id()-&gt;value;
  }
}

function &lt;module_name&gt;_node_insert($node) {
  if ($node-&gt;getType() == '&lt;custom_content_type&gt;') {
    $node-&gt;title = 'Submission #' . $node-&gt;id();
    // more code...
    $node-&gt;save();

  }
}


The first setting of title in hook_ENTITY_TYPE_presave() is only done because no entity can be saved without title. However, it seems like the setting of the title in hook_ENTITY_TYPE_insert() does not change anything. 

I have already tried calling node-&gt;save(); in hook_ENTITY_TYPE_insert() but it gives me an exception stating that I try to store an entity using an already existing primary key. The exception message is 


  The website encountered an unexpected error. Please try again later.
  
  Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity
  constraint violation: 1062 Duplicate entry '48' for key 'PRIMARY':
  INSERT INTO {node_revision} (nid, vid, langcode, revision_timestamp,
  revision_uid, revision_log) VALUES (:db_insert_placeholder_0,
  :db_insert_placeholder_1, :db_insert_placeholder_2,
  :db_insert_placeholder_3, :db_insert_placeholder_4,
  :db_insert_placeholder_5); Array ( [:db_insert_placeholder_0] => 45
  [:db_insert_placeholder_1] => 48 [:db_insert_placeholder_2] => en
  [:db_insert_placeholder_3] => 1485011236 [:db_insert_placeholder_4] =>
  11 [:db_insert_placeholder_5] => ) in
  Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 770 of
  core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).


Can I change the title of the new entity (using the id)? Should I use another hook?
Since nodes have no id on insert, you should just save the node twice.

node_save() initiates a database transaction, in which the changes are not yet written, but the id is already reserved (explaining your issues with not being able to save). You will have to await the database transaction before attempting to save the node again. Unfortunately there is no hook for this.

However, there is a workaround: drupal_register_shutdown_function(). This can be used to call an additional function at the end of the request.

You'll end up with a code like this:

function your_module_node_insert($node){
  if($node-&gt;type == 'your_node'){
    drupal_register_shutdown_function('_your_module_post_insert',$node);
  }
}

function _your_module_post_insert($node) {
  if ($node) {
      $node-&gt;save();
  }
}


Code comes originally from here: https://drupal.stackexchange.com/a/102185/9921