Drupal 7中替代\Drupal::service('salesforce.client')调用Salesforce客户端的方法
Hey there! The \Drupal::service('salesforce.client') syntax you're using is exclusive to Drupal 8 and later, since those versions use a Symfony-powered service container—something Drupal 7 doesn't have built-in. No worries though, the Salesforce module for Drupal 7 has a straightforward replacement.
The Correct Drupal 7 Approach
Use the salesforce_get_client() function provided by the Salesforce module. This function handles all the heavy lifting of initializing the authenticated REST API client, just like the Drupal 8 service does.
Here's the code you'll need:
// Get the Salesforce REST API client $client = salesforce_get_client();
Optional: Validate the Client
It's a good idea to check if the client initialized successfully (in case there's an authentication issue or misconfiguration):
$client = salesforce_get_client(); if (!$client) { // Handle initialization failure drupal_set_message(t('Failed to connect to Salesforce. Please verify your module settings.'), 'error'); // You could also redirect to the Salesforce config page here if needed }
Why Your Original Code Broke
Drupal 7 doesn't have the global Drupal class that Drupal 8 introduced for service access. That's exactly why you're seeing the "Class 'Drupal' not found" error—Drupal 7 simply doesn't recognize that syntax.
内容的提问来源于stack exchange,提问作者Mantas Jurkūnas

