You are here

protected function WebformSubmissionExportImportImporter::importPrepareRecord in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_submission_export_import/src/WebformSubmissionExportImportImporter.php \Drupal\webform_submission_export_import\WebformSubmissionExportImportImporter::importPrepareRecord()

Prepare import submission record.

Parameters

array $record: The import submission record.

\Drupal\webform\WebformSubmissionInterface $webform_submission: The existing webform submission.

Return value

array An array of error messages.

1 call to WebformSubmissionExportImportImporter::importPrepareRecord()
WebformSubmissionExportImportImporter::import in modules/webform_submission_export_import/src/WebformSubmissionExportImportImporter.php
Import records from CSV import file.

File

modules/webform_submission_export_import/src/WebformSubmissionExportImportImporter.php, line 549

Class

WebformSubmissionExportImportImporter
Webform submission export importer.

Namespace

Drupal\webform_submission_export_import

Code

protected function importPrepareRecord(array &$record, WebformSubmissionInterface $webform_submission = NULL) {

  // Track errors.
  $errors = [];
  if (isset($record['uid'])) {

    // Convert user id to internal IDs.
    $record['uid'] = $this
      ->getEntityImportId('user', $record['uid']);

    // Convert empty uid to anonymous user (UID: 0).
    if (empty($record['uid'])) {
      $record['uid'] = 0;
    }
  }

  // Remove empty uuid.
  if (empty($record['uuid'])) {
    unset($record['uuid']);
  }
  $webform = $this
    ->getWebform();
  $source_entity = $this
    ->getSourceEntity();

  // Set webform id.
  $record['webform_id'] = $webform
    ->id();

  // Set source entity.
  // Load or convert the source entity id to an internal ID.
  if ($source_entity) {
    $record['entity_type'] = $source_entity
      ->getEntityTypeId();
    $record['entity_id'] = $source_entity
      ->id();
  }
  elseif (!empty($record['entity_type']) && isset($record['entity_id'])) {
    $record['entity_id'] = $this
      ->getEntityImportId($record['entity_type'], $record['entity_id']);

    // If source entity_id can't be found, log error, and
    // remove the source  entity_type.
    if ($record['entity_id'] === NULL) {
      $t_args = [
        '@entity_type' => $record['entity_type'],
        '@entity_id' => $record['entity_id'],
      ];
      $errors[] = $this
        ->t('Unable to locate source entity (@entity_type:@entity_id)', $t_args);
      $record['entity_type'] = NULL;
    }
  }

  // Convert record to submission element data.
  $elements = $this
    ->getElements();
  foreach ($record as $name => $value) {

    // Set record value form an element.
    if (isset($elements[$name])) {
      $element = $elements[$name];
      $record[$name] = $this
        ->importElement($element, $value, $webform_submission, $errors);
      continue;
    }

    // Check if record name is a composite element which is
    // delimited using '__'.
    if (strpos($name, '__') === FALSE) {
      continue;
    }

    // Get element and composite key and confirm that the element exists.
    list($element_key, $composite_key) = explode('__', $name);
    if (!isset($elements[$element_key])) {
      continue;
    }

    // Make sure the composite element is not storing multiple values which
    // must use YAML.
    // @see \Drupal\webform_submission_export_import\WebformSubmissionExportImportImporter::importCompositeElement
    $element = $elements[$element_key];
    $element_plugin = $this->elementManager
      ->getElementInstance($element);
    if ($element_plugin
      ->hasMultipleValues($element)) {
      continue;
    }
    if ($element_plugin instanceof WebformLikert) {

      // Make sure the Likert question exists.
      if (!isset($element['#questions']) || !isset($element['#questions'][$composite_key])) {
        continue;
      }
      $record[$element_key][$composite_key] = $value;
    }
    elseif ($element_plugin instanceof WebformCompositeBase) {

      // Get the composite element and make sure it exists.
      $composite_elements = $element_plugin
        ->getCompositeElements();
      if (!isset($composite_elements[$composite_key])) {
        continue;
      }
      $composite_element = $composite_elements[$composite_key];
      $record[$element_key][$composite_key] = $this
        ->importElement($composite_element, $value, $webform_submission, $errors);
    }
  }
  return $errors;
}