You are here

public function WebformSubmissionStorage::saveData in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformSubmissionStorage.php \Drupal\webform\WebformSubmissionStorage::saveData()

Save webform submission data to the 'webform_submission_data' table.

This method is public the allow webform handler (i.e. remote posts) to update [webform:handler] tokens stored in the submission data.

Parameters

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

bool $delete_first: TRUE to delete any data first. For new submissions this is not needed.

Overrides WebformSubmissionStorageInterface::saveData

See also

\Drupal\webform\Plugin\WebformHandler\RemotePostWebformHandler::remotePost

1 call to WebformSubmissionStorage::saveData()
WebformSubmissionStorage::doSave in src/WebformSubmissionStorage.php
Performs storage-specific saving of the entity.

File

src/WebformSubmissionStorage.php, line 1289

Class

WebformSubmissionStorage
Defines the webform submission storage.

Namespace

Drupal\webform

Code

public function saveData(WebformSubmissionInterface $webform_submission, $delete_first = TRUE) {

  // Get submission data rows.
  $data = $webform_submission
    ->getData();
  $webform_id = $webform_submission
    ->getWebform()
    ->id();
  $sid = $webform_submission
    ->id();
  $elements = $webform_submission
    ->getWebform()
    ->getElementsInitializedFlattenedAndHasValue();
  $computed_elements = $webform_submission
    ->getWebform()
    ->getElementsComputed();
  $rows = [];
  foreach ($data as $name => $item) {
    $element = isset($elements[$name]) ? $elements[$name] : [
      '#webform_multiple' => FALSE,
      '#webform_composite' => FALSE,
    ];

    // Check if this is a computed element which is not
    // stored in the database.
    $is_computed_element = isset($computed_elements[$name]) ? TRUE : FALSE;
    if ($is_computed_element && empty($element['#store'])) {
      continue;
    }
    if ($element['#webform_composite']) {
      if (is_array($item)) {
        $composite_items = empty($element['#webform_multiple']) ? [
          $item,
        ] : $item;
        foreach ($composite_items as $delta => $composite_item) {
          foreach ($composite_item as $property => $value) {
            $rows[] = [
              'webform_id' => $webform_id,
              'sid' => $sid,
              'name' => $name,
              'property' => $property,
              'delta' => $delta,
              'value' => (string) $value,
            ];
          }
        }
      }
    }
    elseif ($element['#webform_multiple']) {
      if (is_array($item)) {
        foreach ($item as $delta => $value) {
          $rows[] = [
            'webform_id' => $webform_id,
            'sid' => $sid,
            'name' => $name,
            'property' => '',
            'delta' => $delta,
            'value' => (string) $value,
          ];
        }
      }
    }
    else {
      $rows[] = [
        'webform_id' => $webform_id,
        'sid' => $sid,
        'name' => $name,
        'property' => '',
        'delta' => 0,
        'value' => (string) $item,
      ];
    }
  }
  if ($delete_first) {

    // Delete existing submission data rows.
    $this->database
      ->delete('webform_submission_data')
      ->condition('sid', $sid)
      ->execute();
  }

  // Insert new submission data rows.
  $query = $this->database
    ->insert('webform_submission_data')
    ->fields([
    'webform_id',
    'sid',
    'name',
    'property',
    'delta',
    'value',
  ]);
  foreach ($rows as $row) {
    $query
      ->values($row);
  }
  $query
    ->execute();
}