Commit a96b17bc authored by john_a's avatar john_a Committed by John Avery
Browse files

Issue #2974051 by john_a, mahtab_alam: drupal_set_message() will be deprecated soon

parent 0e9b1ec6
......@@ -5,6 +5,7 @@ namespace Drupal\marketing_cloud_example\Plugin\WebformHandler;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\Core\Messenger\MessengerTrait;
/**
* Form submission handler.
......@@ -20,6 +21,7 @@ use Drupal\webform\Plugin\WebformHandlerBase;
* )
*/
class MarketingCloudExample extends WebformHandlerBase {
use MessengerTrait;
/**
* {@inheritdoc}
......@@ -73,23 +75,32 @@ class MarketingCloudExample extends WebformHandlerBase {
$response = \Drupal::service('marketing_cloud_sms.service')
->postMessageToNumber($messageId, $data);
if (!$response) {
// The service returned FALSE, this is ba a gracefully handled error
// (e.g. invalid JSON, invalid login details, etc), and details are
// logged in Watchdog.
$message = t('SMS send failed. Please check the log.');
drupal_set_message($message, 'info');
$this->messenger()->addError($message);
}
elseif (isset($response['errors'])) {
$message = t('Message not sent - errors: %errors', ['%errors' => $response['errors']]);
drupal_set_message($message, 'error');
\Drupal::logger(__METHOD__)->error($message);
// Salesforce has returned 200 Success with an error object, so we need
// to handle this case. This is not automatically treated as an error by
// the module, because it may be an expected response.
$message = t('Salesforce returned an error - errors: %errors', ['%errors' => $response['errors']]);
$this->messenger()->addError($message);
$this->getLogger()->error($message);
}
elseif (!isset($response['result'])) {
$message = t('Message not sent - errors: %errors', ['%errors' => print_r($response, TRUE)]);
drupal_set_message($message, 'error');
\Drupal::logger(__METHOD__)->error($message);
// An error was returned by Salesforce, so to maintain consistency with
// the 200 with errors object, we validate against an expected response.
$message = t('Salesforce returned an error - errors: %errors', ['%errors' => print_r($response, TRUE)]);
$this->messenger()->addError($message);
$this->getLogger()->error($message);
}
else {
$message = t('Response from SalesForce: "%result"', ['%result' => $response['result']]);
drupal_set_message($message, 'info');
\Drupal::logger(__METHOD__)->info($message);
// Fall through to a successful response.
$message = t('Success, response from SalesForce: "%result"', ['%result' => $response['result']]);
$this->messenger()->addMessage($message);
$this->getLogger()->info($message);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment