You are here

protected function RemotePostWebformHandler::getRequestData in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformHandler/RemotePostWebformHandler.php \Drupal\webform\Plugin\WebformHandler\RemotePostWebformHandler::getRequestData()

Get a webform submission's request data.

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.

Return value

array A webform submission converted to an associative array.

1 call to RemotePostWebformHandler::getRequestData()
RemotePostWebformHandler::remotePost in src/Plugin/WebformHandler/RemotePostWebformHandler.php
Execute a remote post.

File

src/Plugin/WebformHandler/RemotePostWebformHandler.php, line 571

Class

RemotePostWebformHandler
Webform submission remote post handler.

Namespace

Drupal\webform\Plugin\WebformHandler

Code

protected function getRequestData($state, WebformSubmissionInterface $webform_submission) {

  // Get submission and elements data.
  $data = $webform_submission
    ->toArray(TRUE);

  // Remove unsupported properties from data.
  // These are typically added by other module's like metatag.
  $unsupported_properties = array_combine($this->unsupportedProperties, $this->unsupportedProperties);
  $data = array_diff_key($data, $unsupported_properties);

  // Flatten data and prioritize the element data over the
  // webform submission data.
  $element_data = $data['data'];
  unset($data['data']);
  $data = $element_data + $data;

  // Excluded selected submission data.
  $data = array_diff_key($data, $this->configuration['excluded_data']);

  // Append uploaded file name, uri, and base64 data to data.
  $webform = $this
    ->getWebform();
  foreach ($data as $element_key => $element_value) {

    // Ignore empty and not equal to zero values.
    // @see https://stackoverflow.com/questions/732979/php-whats-an-alternative-to-empty-where-string-0-is-not-treated-as-empty
    if (empty($element_value) && $element_value !== 0 && $element_value !== '0') {
      continue;
    }
    $element = $webform
      ->getElement($element_key);
    if (!$element) {
      continue;
    }

    // Cast markup to string. This only applies to computed Twig values.
    // @see \Drupal\webform\Element\WebformComputedTwig::computeValue
    if ($element_value instanceof MarkupInterface) {
      $data[$element_key] = $element_value = (string) $element_value;
    }
    $element_plugin = $this->elementManager
      ->getElementInstance($element);
    if ($element_plugin instanceof WebformManagedFileBase) {
      if ($element_plugin
        ->hasMultipleValues($element)) {
        foreach ($element_value as $fid) {
          $data['_' . $element_key][] = $this
            ->getRequestFileData($fid);
        }
      }
      else {
        $data['_' . $element_key] = $this
          ->getRequestFileData($element_value);
      }
    }
    elseif (!empty($this->configuration['cast'])) {

      // Cast value.
      $data[$element_key] = $this
        ->castRequestValues($element, $element_plugin, $element_value);
    }
  }

  // Replace tokens.
  $data = $this
    ->replaceTokens($data, $webform_submission);

  // Append custom data.
  if (!empty($this->configuration['custom_data'])) {
    $custom_data = Yaml::decode($this->configuration['custom_data']);

    // Replace tokens.
    $custom_data = $this
      ->replaceTokens($custom_data, $webform_submission);

    // Cast custom data.
    $custom_data = $this
      ->castCustomData($custom_data);
    $data = $custom_data + $data;
  }

  // Append state custom data.
  if (!empty($this->configuration[$state . '_custom_data'])) {
    $state_custom_data = Yaml::decode($this->configuration[$state . '_custom_data']);

    // Replace tokens.
    $state_custom_data = $this
      ->replaceTokens($state_custom_data, $webform_submission);

    // Cast custom data.
    $state_custom_data = $this
      ->castCustomData($state_custom_data);
    $data = $state_custom_data + $data;
  }
  return $data;
}