8 – The content of the group is not saved

I altered the node creation form to allow me to select a group to which to assign the node with this code:

/ *
* Implements hook_ENTITY_TYPE_insert ()
* /
MYMODULE_node_insert function (node ​​$ node) {
if ($ node-> bundle () == & # 39; article_private & # 39;) {
$ groups =  Drupal :: request () -> get (& # 39; groups & # 39;);
preg_match_all ("/  (( d +) ) /", $ groups, $ group_ids);
$ plugin_id = "group_node: article_private";
foreach ($ group_ids[1] like $ gid) {
$ group = Group :: load ($ gid);
$ group-> addContent ($ node, $ plugin_id);
}
}
}

I did something similar to edit an existing node with a new "relationship" group
This works as expected, the nodes are created and also the content of the group that relates the group to the node ($ group-> addContent ($ node, $ plugin_id);)

Then I did a batch process to assign several nodes to their relative groups
but group content is not created and no error is reported.
This is the main code:

BatchCreateRelations class {

static public function batchCreateRelations ($ batchrelations, & $ context) {
$ message = & # 39; Creating relationships & # 39 ;;
$ results = matrix ();
$ plugin_id = "group_node: article_private";
foreach ($ batchrelations as $ relation) {
$ node = Node :: load ($ relation[0]);
$ group = Group :: load ($ relation[1]);
$ existing = array ();
$ existing = $ group-> getContentByEntityId ($ plugin_id, $ node-> id ());
yes (account (existing $) == 0) {
$ results[] = $ group-> addContent ($ node, $ plugin_id);
}
//$x.= "Creating relationship between node {$ relation[0]} and group {$ relation[1]}north";
}

$ context['message'] = $ message;
$ context['results'] = $ results;
}