Which function should I use to update a node through the comment form? I use this function to synchronize a comment field with the value from a field that exists in the node the comment belongs to.

function mymodule_comment_create(\Drupal\comment\CommentInterface $comment) {
  $node = $comment-&gt;getCommentedEntity();
  if ($node &amp;&amp; $node instanceof \Drupal\node\NodeInterface) {
    if ($node-&gt;hasField('field_example')) {
      $comment-&gt;subject-&gt;value = $node-&gt;field_example-&gt;value;
    }
  }
}


How could I achieve the opposite? When a comment is submitted with the field field_rating, the field_average_rating field of the node should be updated.

How could I achieve that?
You could implement the same hook hook_comment_create() but just inverse the assignment you do on the comment as in your code, for example:

function mymodule_comment_create(\Drupal\comment\CommentInterface $comment) {
  $node = $comment-&gt;getCommentedEntity();
  if ($node &amp;&amp; $node instanceof \Drupal\node\NodeInterface) {
    if ($node-&gt;hasField('node_field') &amp;&amp; $comment-&gt;hasField('comment_field') &amp;&amp; !empty($comment-&gt;comment_field-&gt;value)) {
      $node-&gt;node_field-&gt;value = $comment-&gt;comment_field-&gt;value;
    }
  }
}