I am developing a simple module that hooks into Membership Create, and then uses the information of the related contact to perform some other functions, like initiating a NextCloud account with the same email address and display name. There is a Contribution page in place, using a profile for members, and after the form is submitted the rest roles...

All is working well. Membership is created. Contact is created. The hook gets triggered after the Membership create. I can also get the associated contact ID with:

$contactId = $objectRef->contact_id;

But then when I use the API4 to retrieve contact details nothing is returned. Here is the code I received from Support>Developer>API4 Explorer:

    $contacts = \Civi\Api4\Contact::get(TRUE)
  ->addSelect('display_name', 'email_primary.email')
  ->addWhere('id', '=', $contactId)
foreach ($contacts as $contact) {
  // do something

The returned $contacts array is empty, even though it is present in the backend and $contactId has a value.

I made sure the membership group can be listed by public, but I dont think that should be an issue. Where else shall I look into? Any help is appreciated.

1 Answer 1


Try changing \Civi\Api4\Contact::get(TRUE) to \Civi\Api4\Contact::get(FALSE)

The TRUE/FALSE parameter is a short-hand for checkPermissions. The default in Api4 is that permissions are checked but you can disable that. Obviously if you're going to disable permissions you need to consider who is running the code, what info it returns and how it is used.

If your membership sign-up is used without logging in (anonymous users) then they won't have access to use the API, but you still want this to run so bypassing permissions checking is appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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