Utils::wf_civicrm_api() requires CiviCRM to initialize in order to use civicrm_api3()

Created on 8 April 2023, over 1 year ago
Updated 17 January 2024, 10 months ago

1.0 Problem/Motivation

I've got a really simple test webform with just three fields: existing contact, first and last names. Maybe there's something else going on, but I get this fatal error:

Error: Call to undefined function Drupal\webform_civicrm\civicrm_api3() in Drupal\webform_civicrm\Utils->wf_civicrm_api() (line 686 of modules/contrib/webform_civicrm/src/Utils.php).
Drupal\webform_civicrm\Utils->wf_civicrm_api('contact', 'get', Array) (Line: 812)
Drupal\webform_civicrm\Utils->wf_crm_apivalues('contact', 'get', Array, 'display_name') (Line: 255)
_fillCiviCRMData(Array, Object) (Line: 177)
webform_civicrm_webform_submission_load(Array) (Line: 434)

It seems like it currently assumes that the calling function is initializing CiviCRM, like in Utils::wf_crm_display_name().

2.0 Detailed steps to reproduce (embed screenshots)

It's happening when calling _fillCiviCRMData line 255:

$contacts = $utils->wf_crm_apivalues('contact', 'get', ['id' => ['IN' => array_keys($contacts)]], 'display_name') + $contacts;

3.0 Proposed resolution

Either in Utils::wf_civicrm_api() or in _fillCiviCRMData on line 255 add

\Drupal::service('civicrm')->initialize();
🐛 Bug report
Status

Closed: cannot reproduce

Version

6.2

Component

CiviCRM Data Handling

Created by

🇨🇦Canada nubeli

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @nubeli
  • 🇮🇳India jitendrapurohit

    I'm not able to replicate this on a clean install @nubeli. I think something is wrong with your setup. CiviCRM should be initialized using the CiviCRM Webform Handler plugin. See https://github.com/colemanw/webform_civicrm/blob/6.x/src/Plugin/WebformH...

    Can you please confirm if webform_civicrm handler is present (screenshot attached) when you visit - /admin/structure/webform/manage//handlers in the URL?

    If not - then the setup seems to be wrong. Is this webform migrated? If yes, the migration code need to create this handler in D9 to function correctly.

  • 🇨🇦Canada nubeli

    @jitendrapurohit that's not the issue - the CiviCRM handler is on the webform. So I'm not sure where things are wrong. But I figured that it's probably better to ensure that CiviCRM is initialized at that step anyway. Similar patterns are used elsewhere in the module, though inconsistently. For instance in webform_civicrm.module, on Line 141, 876, ... in Utils.php line 566

  • 🇨🇦Canada karing 🇨🇦

    I can't replicate this either and we have numerous tests that pass on D9/D10. This must be specific to your instance.

  • Status changed to Closed: cannot reproduce over 1 year ago
  • 🇨🇦Canada nubeli

    I was able to determine the issue on this particular site. This is not required.

    Though aside, I'm wondering why webform_civicrm has \Drupal::service('civicrm')->initialize(); for some things but not others when the handler itself deals with initializing CiviCRM. Maybe it's not required in those cases either.

Production build 0.71.5 2024