MembershipService
The MembershipService provides the main API for membership-related logic: checking whether a contact is a member of a target contact, and loading memberships for a contact or for a target.
Service ID: crm_membership.service
Class: Drupal\crm_membership\Service\MembershipService
Getting the service
Inject the service in your code, or use the static helper:
$membership_service = \Drupal::service('crm_membership.service');
Inject via constructor (recommended):
public function __construct(
protected readonly MembershipService $membershipService,
) {}
Then register your class in services.yml with autowire: true or explicitly inject crm_membership.service.
Methods
isMember
public function isMember(Contact $contact, Contact $target): bool
Checks whether $contact is considered a member of $target.
- Direct memberships: Loads active memberships that reference both the contact and the target, then for each membership uses its Membership Term plugin to see if the membership is active for that contact (including grace periods).
- Indirect memberships: Loads all active memberships for the target (e.g. household memberships) and checks whether the contact is active in any of their current periods (e.g. as an applicable contact on a period).
- Event: If still not a member, dispatches
MembershipEvents::IS_MEMBER; subscribers can callMembershipIsMemberEvent::markAsMember()to mark the contact as a member.
Returns true if any of the above determines the contact is a member.
getMemberships
public function getMemberships(Contact $contact): array
Returns all active memberships that have the given contact in their contacts field. Return value is an array of Membership entities (keyed by ID). On storage/plugin errors, logs and returns an empty array.
getMembershipsForTarget
public function getMembershipsForTarget(Contact $target): array
Returns all active memberships whose target_contact is the given contact. Return value is an array of Membership entities (keyed by ID). Used internally by isMember() for indirect membership checks and can be used by custom code to list memberships for an organization (or other target).
Dependencies
The service depends on:
- Term plugins: Status and period logic are delegated to the membership’s term plugin (via
Membership::isActiveFor()). - Event dispatcher: The
IS_MEMBERevent is dispatched so other modules can extend or override “is member” logic.
See Events for subscribing to IS_MEMBER.