You are here

protected function RemotePostYamlFormHandler::remotePost in YAML Form 8

Execute a remote post.

Parameters

string $operation: The type of form submission operation to be posted. Can be 'insert', 'update', or 'delete'.

\Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission: The form submission to be posted.

2 calls to RemotePostYamlFormHandler::remotePost()
RemotePostYamlFormHandler::postDelete in src/Plugin/YamlFormHandler/RemotePostYamlFormHandler.php
Acts on deleted a form submission before the delete hook is invoked.
RemotePostYamlFormHandler::postSave in src/Plugin/YamlFormHandler/RemotePostYamlFormHandler.php
Acts on a saved form submission before the insert or update hook is invoked.

File

src/Plugin/YamlFormHandler/RemotePostYamlFormHandler.php, line 268

Class

RemotePostYamlFormHandler
Form submission remote post handler.

Namespace

Drupal\yamlform\Plugin\YamlFormHandler

Code

protected function remotePost($operation, YamlFormSubmissionInterface $yamlform_submission) {
  $request_url = $this->configuration[$operation . '_url'];
  if (empty($request_url)) {
    return;
  }
  $request_type = $this->configuration['type'];
  $request_post_data = $this
    ->getPostData($operation, $yamlform_submission);
  try {
    switch ($request_type) {
      case 'json':
        $response = $this->httpClient
          ->post($request_url, [
          'json' => $request_post_data,
        ]);
        break;
      case 'x-www-form-urlencoded':
      default:
        $response = $this->httpClient
          ->post($request_url, [
          'form_params' => $request_post_data,
        ]);
        break;
    }
  } catch (RequestException $request_exception) {
    $message = $request_exception
      ->getMessage();
    $response = $request_exception
      ->getResponse();

    // If debugging is enabled, display the error message on screen.
    $this
      ->debug($message, $operation, $request_url, $request_type, $request_post_data, $response, 'error');

    // Log error message.
    $context = [
      '@form' => $this
        ->getYamlForm()
        ->label(),
      '@operation' => $operation,
      '@type' => $request_type,
      '@url' => $request_url,
      '@message' => $message,
      'link' => $this
        ->getYamlForm()
        ->toLink(t('Edit'), 'handlers-form')
        ->toString(),
    ];
    $this->logger
      ->error('@form form remote @type post (@operation) to @url failed. @message', $context);
    return;
  }

  // If debugging is enabled, display the request and response.
  $this
    ->debug(t('Remote post successful!'), $operation, $request_url, $request_type, $request_post_data, $response, 'warning');
}