D8 Populate og group & reference filed through form alter

use DrupalnodeEntityNode;
use DrupalCoreFormFormStateInterface;

/**
* Implements hook_form_alter()
*/
function YOURMODULENAME_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == ‘your_form_id’) {
$nid = Drupal::request()->query->get(‘nid’);
$node = is_numeric($nid) ? Node::load($nid) : NULL;
if ($node->getType() == ‘ABC’) {
$form[‘og_audience’][‘widget’][0][‘target_id’][‘#default_value’] = $node;
}
elseif ($node->getType() == ‘XYZ’) {
$form[‘your_filed_name’][‘widget’][0][‘target_id’][‘#default_value’] = $node;
}
}
}

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/2/feed