<!-- THEME DEBUG -->
<!-- THEME HOOK: 'field' -->
<!-- FILE NAME SUGGESTIONS:
   ▪️ field--node--title--article.html.twig
   ✅ field--node--title.html.twig
   ▪️ field--node--article.html.twig
   ▪️ field--title.html.twig
   ▪️ field--string.html.twig
   ▪️ field.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/contrib/classy/templates/field/field--node--title.html.twig' -->
<span class=Add user or some content to a group in Drupal ">

Add user or some content to a group in Drupal

Add a user to group

$uid = '5';
$gid = '40';
$user = \Drupal\user\Entity\User::load($uid);
$group = \Drupal::entityTypeManager()->getStorage('group')->load($gid);
$group->addMember($user);
$group->save();

Add content to a group

$content_types_manager = \Drupal::service('opigno_group_manager.content_types.manager');
$plugin_definition = $content_types_manager->getDefinition('ContentTypeModule');
$added_entity = \Drupal::entityTypeManager()
  ->getStorage($plugin_definition['entity_type'])
  ->load($entity_id());

$group->addContent($added_entity, $plugin_definition['group_content_plugin_id']);
$group->save();