Score:-3

How can I programmatically add users?

ls flag

How can I programmatically add users to an organic group?

apaderno avatar
us flag
Welcome to Drupal Answers! Since the question is about code, we expect to see the code you wrote so far. If you are asking a question about code before attempting to write code, you are asking the question too early.
ankur avatar
ls flag
i have tried this but not working ..$membership = Og::createMembership($gid, $account);$membership->save();
Score:1
us flag

Something like the following should work; replace $userId & $groupId with approriate values

// Get a User entity via ID
$user = \Drupal\user\Entity\User::load($userId);

// Get a Group entity via ID
$group = \Drupal\group\Entity\Group::load($groupId);

// Add member and save
$group->addMember($user);
$group->save();

You can use the following to get the currently logged in userID

$userId = \Drupal::currentUser()->id()
ankur avatar
ls flag
Getting error like "Error: Class 'Drupal\group\Entity\Group' not found "i think this above solution for Group module but want to add in Organic Group module (og_group) not in the Group module.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.