Skip to content

Events

The module dispatches a single event that allows other modules to customize whether a contact is considered a member of a target contact.

IS_MEMBER

Event name: MembershipEvents::IS_MEMBER (crm_membership.is_member)
Event class: Drupal\crm_membership\Event\MembershipIsMemberEvent

Dispatched by MembershipService::isMember() when the contact was not found to be a member via direct or indirect memberships. Subscribers can apply custom logic (e.g. legacy data, external systems, roles) and mark the contact as a member.

Event object

Property / method Type Description
$contact Contact (readonly) The contact being checked.
$target Contact (readonly) The target contact (“member of”).
markAsMember(): void Mark the contact as a member. Stops propagation so later subscribers do not run.
isMember(): bool bool Whether the contact has been marked as a member.

Subscribing

Create an EventSubscriber that listens to MembershipEvents::IS_MEMBER and, when your logic says the contact is a member, call $event->markAsMember().

Example (in a custom module):

<?php

declare(strict_types=1);

namespace Drupal\my_module\EventSubscriber;

use Drupal\crm_membership\Event\MembershipEvents;
use Drupal\crm_membership\Event\MembershipIsMemberEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class MembershipIsMemberSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents(): array {
    return [
      MembershipEvents::IS_MEMBER => 'onIsMember',
    ];
  }

  public function onIsMember(MembershipIsMemberEvent $event): void {
    $contact = $event->contact;
    $target = $event->target;
    // Your logic: e.g. check another system, check a role, etc.
    if ($this->myCustomCheck($contact, $target)) {
      $event->markAsMember();
    }
  }

  private function myCustomCheck($contact, $target): bool {
    return FALSE;
  }
}

Register the subscriber in my_module.services.yml:

services:
  my_module.membership_is_member_subscriber:
    class: Drupal\my_module\EventSubscriber\MembershipIsMemberSubscriber
    tags:
      - { name: event_subscriber }

After saving the subscriber, clear caches. The next time MembershipService::isMember($contact, $target) runs and does not find a membership, your subscriber will run and can call $event->markAsMember() to make the method return true.