Hello,

I am trying a few days to achieve this but it's impossible... I am altering the comment form of the articles. Articles have the field:field_parent and Comments have the field: field_child that is a reference to the parent field. So I am trying to give to the child field, the same value that the field_parent has. Here is the code I use inside my alter function but does not work:

$node = \Drupal::routeMatch()->getParameter('node');
if (is_object($node) && $node != null && $node->getType() == 'article') {
   $node->id();
   $field= $node->field_parent->value;	}	
		
$form['field_child']['widget'][0]['target_id']['#default_value'] = $field;

If I remove the part ->field_parent->value the code returns: Article's Title(Node id). So why doesn't it work when I ask for the field_parent value?

Important: The above returns only the node title and the node id when the comment form is in the same page with the article. But what I need is to have the comment form in a different page and the child field been dynamically filled with the value of parent field. Does anybody know what class should I call in this case inside my function? Any help would be much appreciated!

Thank you