You are here

protected function WebformSubmissionStorage::loadData in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/WebformSubmissionStorage.php \Drupal\webform\WebformSubmissionStorage::loadData()

Save webform submission data from the 'webform_submission_data' table.

Parameters

array $webform_submissions: An array of webform submissions.

1 call to WebformSubmissionStorage::loadData()
WebformSubmissionStorage::doLoadMultiple in src/WebformSubmissionStorage.php
Performs storage-specific loading of entities.

File

src/WebformSubmissionStorage.php, line 1384

Class

WebformSubmissionStorage
Defines the webform submission storage.

Namespace

Drupal\webform

Code

protected function loadData(array &$webform_submissions) {

  // Load webform submission data.
  if ($sids = array_keys($webform_submissions)) {

    /** @var \Drupal\Core\Database\StatementInterface $result */
    $result = $this->database
      ->select('webform_submission_data', 'sd')
      ->fields('sd', [
      'webform_id',
      'sid',
      'name',
      'property',
      'delta',
      'value',
    ])
      ->condition('sd.sid', $sids, 'IN')
      ->orderBy('sd.sid', 'ASC')
      ->orderBy('sd.name', 'ASC')
      ->orderBy('sd.property', 'ASC')
      ->orderBy('sd.delta', 'ASC')
      ->execute();
    $submissions_data = [];
    while ($record = $result
      ->fetchAssoc()) {
      $sid = $record['sid'];
      $name = $record['name'];

      /** @var \Drupal\webform\WebformInterface $webform */
      $webform = $webform_submissions[$sid]
        ->getWebform();
      $elements = $webform ? $webform
        ->getElementsInitializedFlattenedAndHasValue() : [];
      $element = isset($elements[$name]) ? $elements[$name] : [
        '#webform_multiple' => FALSE,
        '#webform_composite' => FALSE,
      ];
      if ($element['#webform_composite']) {
        if ($element['#webform_multiple']) {
          $submissions_data[$sid][$name][$record['delta']][$record['property']] = $record['value'];
        }
        else {
          $submissions_data[$sid][$name][$record['property']] = $record['value'];
        }
      }
      elseif ($element['#webform_multiple']) {
        $submissions_data[$sid][$name][$record['delta']] = $record['value'];
      }
      else {
        $submissions_data[$sid][$name] = $record['value'];
      }
    }

    // Set webform submission data via setData().
    foreach ($submissions_data as $sid => $submission_data) {
      $webform_submission = $webform_submissions[$sid];
      $webform_submission
        ->setData($submission_data);
      $webform_submission
        ->setOriginalData($webform_submission
        ->getData());
    }
  }
}