I try to use group module in d8.

1. I created a new group type named test enter image description here

2. I added 2 group as demo with admin is default group_membership.

3. in group 1 (name okok) I edited and added new user to group. enter image description here

4. When I click the add member, the site redirect to another add form: enter image description here

  • I enter the user as normal.
  • then click save button.

5. The result is User was added into that group.

So, What I wonder is how to know an user:

  • Was added to a group.
  • Was removed from a group.

Because I want to call another service every time that action/event was executed. For example, I want to log in to the watchdog like:

  • "User A has been added into group X"
  • "User B has been remove from group Y"

Is there any way to trigger that action/event in code.

Thanks for all your helps.

  • Are those two separate examples, or are they connected? – Jaypan Jun 3 at 17:04
  • I think they are separate, but my direction is dispatch event for out-group event also. – mr.Thi Jun 3 at 17:07
  • I'm sorry, I can't understand what you are asking. Can you please try to add more information to the original question? Thank you. – Jaypan Jun 3 at 17:38
  • @yaypan I updated the discription already, please check. Thanks - Thi – mr.Thi Jun 3 at 17:48
  • When you say 'event', is that in Drupal terms - do you want an Event Handler, or is it in generic terms, like 'when the user leaves a group, I want to perform this action'? – Jaypan Jun 3 at 17:54

as @Jaypan suggested to me, I used hook_ENTITY_TYPE_insert() so my code is:

function hook_group_content_insert(GroupContentInterface $group_content) {
  if ($group_content->hasField('entity_id')
    && !$group_content->get('entity_id')->isEmpty()) {
    $users = $group_content->get('entity_id')->referencedEntities();
    $user = reset($users);
    if ($user instanceof UserInterface) {
      // Do your code here.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.