Timeline for How can I programmatically add members to a group?
Current License: CC BY-SA 4.0
15 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 3, 2021 at 0:00 | history | tweeted | twitter.com/StackDrupal/status/1400240847447285760 | ||
Jun 2, 2021 at 15:34 | history | edited | leymannx | CC BY-SA 4.0 |
Remove Drupal and version tag from title
|
Dec 6, 2017 at 5:30 | answer | added | Егор Анастасьев | timeline score: 4 | |
Jun 22, 2017 at 16:54 | answer | added | ekta | timeline score: 5 | |
Jun 22, 2017 at 16:50 | comment | added | Neograph734 | Saving the group crossed my mind, but I didn't think it would help... Can you add the answer yourself? | |
Jun 22, 2017 at 16:49 | comment | added | ekta | Yes great! So, I am able to add members to a group now. I modified the code to this: $account = \Drupal\user\Entity\User::load(7); $group = \Drupal\group\Entity\Group::load(1); $group->addMember($account); $group->save(); | |
Jun 22, 2017 at 15:48 | comment | added | Neograph734 | Because you already loaded group 1 and are calling the function for that group ;) See line 102: cgit.drupalcode.org/group/tree/src/Entity/GroupInterface.php | |
Jun 22, 2017 at 15:47 | comment | added | ekta | Sure, I am setting gid because otherwise then how should I make sure to add a user to group 1 and not group 2. Thank you very much for looking into this. | |
Jun 22, 2017 at 15:44 | comment | added | Neograph734 | I am sorry, I don't see anything else... What if you remove the gid from $values? You should not have to set that anyway. Maybe someone else has a good insight. | |
Jun 22, 2017 at 15:33 | comment | added | ekta | Yes that is right actually. I am now loading an existing user and passing that as an argument in addMember function. However still it does not add the user to the group. | |
Jun 22, 2017 at 15:12 | comment | added | Neograph734 |
If users already exist, why do you create a user? ($account = $this->createUser(); ) Can't you load an existing user instead: $account = \Drupal\group\Entity\User::load(1); ?
|
|
Jun 22, 2017 at 15:03 | comment | added | ekta | Hi @Neograph734, Thanks for your response. I have edited my question above. I hope I have made it a little more clear. adding ->save() after createUser() doesn't seem to solve this. Can you please suggest something more on this? Thank you very much. | |
Jun 22, 2017 at 15:01 | history | edited | ekta | CC BY-SA 3.0 |
added 440 characters in body
|
Jun 22, 2017 at 14:16 | comment | added | Neograph734 | Please explain what is not working, that helps us answer you. From the looks, you are not saving the created user. Try to add ->save(); after createUser() | |
Jun 22, 2017 at 13:45 | history | asked | ekta | CC BY-SA 3.0 |