You are here

protected function YamlFormSubmissionStorage::saveData in YAML Form 8

Save form submission data to the 'yamlform_submission_data' table.

Parameters

\Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission: A form submission.

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

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

File

src/YamlFormSubmissionStorage.php, line 698

Class

YamlFormSubmissionStorage
Defines the form submission storage.

Namespace

Drupal\yamlform

Code

protected function saveData(YamlFormSubmissionInterface $yamlform_submission, $delete_first = TRUE) {

  // Get submission data rows.
  $data = $yamlform_submission
    ->getData();
  $yamlform_id = $yamlform_submission
    ->getYamlForm()
    ->id();
  $sid = $yamlform_submission
    ->id();
  $elements = $yamlform_submission
    ->getYamlForm()
    ->getElementsFlattenedAndHasValue();
  $rows = [];
  foreach ($data as $name => $item) {
    $element = isset($elements[$name]) ? $elements[$name] : [
      '#yamlform_multiple' => FALSE,
      '#yamlform_composite' => FALSE,
    ];
    if ($element['#yamlform_multiple']) {
      if (is_array($item)) {
        foreach ($item as $delta => $value) {
          $rows[] = [
            'yamlform_id' => $yamlform_id,
            'sid' => $sid,
            'name' => $name,
            'property' => '',
            'delta' => $delta,
            'value' => (string) $value,
          ];
        }
      }
    }
    elseif ($element['#yamlform_composite']) {
      if (is_array($item)) {
        foreach ($item as $property => $value) {
          $rows[] = [
            'yamlform_id' => $yamlform_id,
            'sid' => $sid,
            'name' => $name,
            'property' => $property,
            'delta' => 0,
            'value' => (string) $value,
          ];
        }
      }
    }
    else {
      $rows[] = [
        'yamlform_id' => $yamlform_id,
        'sid' => $sid,
        'name' => $name,
        'property' => '',
        'delta' => 0,
        'value' => (string) $item,
      ];
    }
  }
  if ($delete_first) {

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

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