protected function RemotePostWebformHandler::remotePost in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformHandler/RemotePostWebformHandler.php \Drupal\webform\Plugin\WebformHandler\RemotePostWebformHandler::remotePost()
Execute a remote post.
Parameters
string $state: The state of the webform submission. Either STATE_NEW, STATE_DRAFT_CREATED, STATE_DRAFT_UPDATED, STATE_COMPLETED, STATE_UPDATED, or STATE_CONVERTED depending on the last save operation performed.
\Drupal\webform\WebformSubmissionInterface $webform_submission: The webform submission to be posted.
2 calls to RemotePostWebformHandler::remotePost()
- RemotePostWebformHandler::postDelete in src/
Plugin/ WebformHandler/ RemotePostWebformHandler.php - Acts on deleted a webform submission before the delete hook is invoked.
- RemotePostWebformHandler::postSave in src/
Plugin/ WebformHandler/ RemotePostWebformHandler.php - Acts on a saved webform submission before the insert or update hook is invoked.
File
- src/
Plugin/ WebformHandler/ RemotePostWebformHandler.php, line 486
Class
- RemotePostWebformHandler
- Webform submission remote post handler.
Namespace
Drupal\webform\Plugin\WebformHandlerCode
protected function remotePost($state, WebformSubmissionInterface $webform_submission) {
$state_url = $state . '_url';
if (empty($this->configuration[$state_url])) {
return;
}
$this->messageManager
->setWebformSubmission($webform_submission);
$request_url = $this->configuration[$state_url];
$request_url = $this
->replaceTokens($request_url, $webform_submission);
$request_method = !empty($this->configuration['method']) ? $this->configuration['method'] : 'POST';
$request_type = $request_method !== 'GET' ? $this->configuration['type'] : NULL;
// Get request options with tokens replaced.
$request_options = !empty($this->configuration['custom_options']) ? Yaml::decode($this->configuration['custom_options']) : [];
$request_options = $this
->replaceTokens($request_options, $webform_submission);
try {
if ($request_method === 'GET') {
// Append data as query string to the request URL.
$query = $this
->getRequestData($state, $webform_submission);
$request_url = Url::fromUri($request_url, [
'query' => $query,
])
->toString();
$response = $this->httpClient
->get($request_url, $request_options);
}
else {
$method = strtolower($request_method);
$request_options[$request_type === 'json' ? 'json' : 'form_params'] = $this
->getRequestData($state, $webform_submission);
$response = $this->httpClient
->{$method}($request_url, $request_options);
}
} catch (RequestException $request_exception) {
$response = $request_exception
->getResponse();
// Encode HTML entities to prevent broken markup from breaking the page.
$message = $request_exception
->getMessage();
$message = nl2br(htmlentities($message));
$this
->handleError($state, $message, $request_url, $request_method, $request_type, $request_options, $response);
return;
}
// Display submission exception if response code is not 2xx.
if ($this
->responseHasError($response)) {
$message = $this
->t('Remote post request return @status_code status code.', [
'@status_code' => $response
->getStatusCode(),
]);
$this
->handleError($state, $message, $request_url, $request_method, $request_type, $request_options, $response);
return;
}
else {
$this
->displayCustomResponseMessage($response, FALSE);
}
// If debugging is enabled, display the request and response.
$this
->debug($this
->t('Remote post successful!'), $state, $request_url, $request_method, $request_type, $request_options, $response, 'warning');
// Replace [webform:handler] tokens in submission data.
// Data structured for [webform:handler:remote_post:completed:key] tokens.
$submission_data = $webform_submission
->getData();
$submission_has_token = strpos(print_r($submission_data, TRUE), '[webform:handler:' . $this
->getHandlerId() . ':') !== FALSE ? TRUE : FALSE;
if ($submission_has_token) {
$response_data = $this
->getResponseData($response);
$token_data = [
'webform_handler' => [
$this
->getHandlerId() => [
$state => $response_data,
],
],
];
$submission_data = $this
->replaceTokens($submission_data, $webform_submission, $token_data);
$webform_submission
->setData($submission_data);
// Resave changes to the submission data without invoking any hooks
// or handlers.
if ($this
->isResultsEnabled()) {
$webform_submission
->resave();
}
}
}