I have CiviCRM 5.56.1 installed via composer on a Drupal 9 application. In a custom module I use API v4 to manipulate records. For example, get membership types.

\Civi\Api4\MembershipType::get()->addWhere('name', 'My Membership')->execute();

The error that I get is the following:

Error: Class "CRM_Core_DAO" not found in /var/www/html/vendor/civicrm/civicrm-core/Civi/Api4/Utils/CoreUtil.php on line 101 #0 /var/www/html/vendor/civicrm/civicrm-core/Civi/Api4/Generic/DAOGetAction.php(156): Civi\Api4\Utils\CoreUtil::getOperators()

In my civicrm settings file I have made sure to add the correct $civicrm_root variable. I have check the resulting include paths and the correct civicrm core path is included. CiviCRM in general works fine via the UI. The same command works when using the cv CLI.

Any ideas why this happens when calling from within Drupal? It seems namespaced classes are found. In older related questions it is suggesting to use civicrm_initialize(), but this function does not seem to exist anymore.

1 Answer 1


One thing you can try is invoking the service and initializing it from there. That will give you access to API4.

$civicrm = \Drupal::service('civicrm');

Alternatively - \Drupal::service('civicrm')->initialize();

Additionally, you can use the following to access API related classes:

use Drupal\civicrm\Civicrm;
use Civi\Api4\Contact;
use Civi\Api4\Membership;
use Civi\Api4\Address;
use Civi\Api4\Phone;
use Civi\Api4\Email;

//in function
$memberships = Membership::get(FALSE)
$contacts = Contact::get(FALSE)

  • \Drupal::service('civicrm')->initialize(); did the trick, thanks. Jan 31, 2023 at 3:54

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.