Problem/Motivation

Send an email notification to the user when their membership request has been approved.

Proposed resolution

Use State Machine to handle the request status as this will provide all the events to trigger sending an email.

Remaining tasks

  • Prpose resolution
  • Code
  • Tests

User interface changes

Should be none

API changes

Data model changes

Comments

dwkitchen created an issue. See original summary.

marcelovani’s picture

Hey David, have you got any progress on this?

lobsterr’s picture

I wonder do we really need it?
1) I wanted to keep module as simple as possible
2) I don't want to introduce an unnecessary dependency
3) We don't have super complicated workflow there. It can go only one direction and we don't have specific restriction between the states. So, if a user has rights manage users, he can do whatever he/she wants.
4) It is always possible to send emails using another ways

Your thoughts? cons/pros?

umit’s picture

In my opinion, the user whose membership has been approved should know that it has been approved. I think that e-mail or message notification must be sent.

lobsterr’s picture

@Umit I didn't say that we don't need notifications, but it is possible to implement it without integration state machine module.
And even switching to this field will require the same implementation.
It is exactly, what I mentioned in comment above

lobsterr’s picture

Status: Active » Closed (won't fix)
dwkitchen’s picture

Title: Email Notifications - State Machine » Email Notifications
Version: 1.0.0-alpha2 » 1.0.x-dev
Issue summary: View changes
Status: Closed (won't fix) » Active

I am re-opening as the proposed resolution has not been accepted the requirement is still true.

lobsterr’s picture

1) On our side we use message stack + SwiftMailer module, but this solution is not generic enough, but we can think how to make it generic
2) Another option is custom code
3) Rules module integration. I have done first steps in this ticket https://www.drupal.org/project/grequest/issues/3174612

In addition we need custom settings to on / off notifications, but it should be done on a group level and not on a group type level. Unfortunately, it is not supported by the group module.

We use entity_data module developed by us to solve the issue with custom settings for the groups (similar to user_data service). I'm in the process to get maintainer rights to publish it.

Anyway I see it as submodule of the current one.
Let's discuss it and find the best solution.

lobsterr’s picture

Issue summary: View changes
Related issues: +#3193333: Add support for state machine

After another round of thinking about the issue I have decided to switch to state machine #3193333: Add support for state machine

lobsterr’s picture

I have implemented state machine for the status field. Now we have events:

For creation

group_membership_request.create.pre_transition

group_membership_request.create.post_transition

For creation

group_membership_request.create.pre_transition

group_membership_request.create.post_transition

For rejection

group_membership_request.reject.pre_transition

group_membership_request.reject.post_transition

Now we need to think, which solution to use to send emails. Any proposals?
Also I wonder how do you see configuration pages?
Or from this point give users chance to use any implementations they want?

umit’s picture

I follow the developments with interest. I am sure this module will become very useful in the future. It can also be via drupal standard mail to send emails.

mr.andrey’s picture

I'm trying to set up email notifications. Following the README, I added the following code to my custom module as the first step. Unfortunately, none of the test messages show up in the log.

What am I doing wrong?

Is there a different way to do it? I don't see group request events in rules.

Thanks.

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MyEventSubscriber implements EventSubscriberInterface {
  public static function getSubscribedEvents() {
    return [
      'group_membership_request.create.pre_transition' => 'preCreate',
      'group_membership_request.create.post_transition' => 'postCreate',
      'group_membership_request.approve.pre_transition' => 'preApprove',
      'group_membership_request.approve.post_transition' => 'postApprove',
      'group_membership_request.reject.pre_transition' => 'preReject',
      'group_membership_request.reject.post_transition' => 'postReject',
    ];
  }

  public function preCreate(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test preCreate');
    \Drupal::logger('helper123')->notice(var_export($event,true));
  }

  public function postCreate(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test postCreate');
    \Drupal::logger('helper123')->notice(var_export($event,true));
  }

  public function preApprove(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test preApprove');
    \Drupal::logger('helper123')->notice(var_export($event,true));
  }

  public function postApprove(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test postApprove');
    \Drupal::logger('helper123')->notice(var_export($event,true));
  }

  public function preReject(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test preReject');
    \Drupal::logger('helper123')->notice(var_export($event,true));
  }

  public function postReject(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test postReject');
    \Drupal::logger('helper123')->notice(var_export($event,true));
  }

  protected function setMessage(WorkflowTransitionEvent $event, $phase) {
    $str = '@entity_label (@field_name)';
    $str .= '- @state_label at @phase (workflow: @workflow).';
    \Drupal::messenger()->addMessage(new TranslatableMarkup($str, [
      '@entity_label' => $event->getEntity()->label(),
      '@field_name' => $event->getFieldName(),
      '@state_label' => $event->getTransition()->getToState()->getLabel(),
      '@workflow' => $event->getWorkflow()->getId(),
      '@phase' => $phase,
    ]));
  }

}

UPDATE:
Nevermind, got it figured out.

For reference (helper123 is my custom module):

1. In helper123.services.yml, add:

services:
      
  helper123.group_membership_event_subscriber:
    class: Drupal\helper123\EventSubscriber\GroupMembershipEventSubscriber
    tags:
      - { name: event_subscriber }

2. In src/EventSubscriber/GroupMembershipEventSubscriber.php, add:

<?php
namespace Drupal\helper123\EventSubscriber;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class GroupMembershipEventSubscriber implements EventSubscriberInterface {
  public static function getSubscribedEvents() {
    return [
      'group_membership_request.create.pre_transition' => 'preCreate',
      'group_membership_request.create.post_transition' => 'postCreate',
      'group_membership_request.approve.pre_transition' => 'preApprove',
      'group_membership_request.approve.post_transition' => 'postApprove',
      'group_membership_request.reject.pre_transition' => 'preReject',
      'group_membership_request.reject.post_transition' => 'postReject',
    ];
  }

  public function preCreate(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test preCreate');
  }

  public function postCreate(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test postCreate');
  }

  public function preApprove(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test preApprove');
  }

  public function postApprove(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test postApprove');
  }

  public function preReject(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test preReject');
  }

  public function postReject(WorkflowTransitionEvent $event) {
    //$this->setMessage($event, __FUNCTION__);
    \Drupal::logger('helper123')->notice('test postReject');
  }

}
kecsot’s picture

I've tested all events.
It works as LOBsTerr said in #9 and #10.

Works as designed. (For example: #12)

kecsot’s picture

Now we need to think, which solution to use to send emails. Any proposals?

I think its enough for developers. We can write custom module to trigger actions or send message by another way.

This module extends group module so I suggest to follow group module's dependecies and keep grequest simple.

leonardo.montenegro’s picture

About 2 years ago, back when we were still using Groups v1 with older versions of all modules, we used the same example from @mr.andrey. It has been working fine throughout the time, but suddenly it's presenting issues after moving to Groups V3 and grequest v3.1.

In my case, I'm using the three events below:

  • group_membership_request.create.post_transition
  • group_membership_request.approve.post_transition
  • group_membership_request.reject.post_transition

From our tests, the ones from approval/rejection are being triggered just fine, but the create one (called after a Group Membership Request is created) isn't being called. As a result, our custom notification mechanism stopped working for new requests.

My guess is that it stopped working for the newer versions. At least as a temporary workaround, probably a "hook_group_relationship_insert" would work, for detecting any Group Membership Requests creation attempts.