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->getCommentedEntity();
if ($node && $node instanceof \Drupal\node\NodeInterface) {
if ($node->hasField('field_example')) {
$comment->subject->value = $node->field_example->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?