You are here

protected function WebformSubmissionDevelGenerateTrait::generateSubmission in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/DevelGenerate/WebformSubmissionDevelGenerateTrait.php \Drupal\webform\Plugin\DevelGenerate\WebformSubmissionDevelGenerateTrait::generateSubmission()

Create one node. Used by both batch and non-batch code branches.

1 call to WebformSubmissionDevelGenerateTrait::generateSubmission()
WebformSubmissionDevelGenerateTrait::generateSubmissions in src/Plugin/DevelGenerate/WebformSubmissionDevelGenerateTrait.php
Generates submissions for a list of given webforms.

File

src/Plugin/DevelGenerate/WebformSubmissionDevelGenerateTrait.php, line 301

Class

WebformSubmissionDevelGenerateTrait
Provides a WebformSubmissionDevelGenerate plugin.

Namespace

Drupal\webform\Plugin\DevelGenerate

Code

protected function generateSubmission(&$results) {
  $webform_id = array_rand(array_filter($results['webform_ids']));

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $this
    ->getWebformStorage()
    ->load($webform_id);
  $users = $results['users'];
  $uid = $users[array_rand($users)];
  $entity_type = $results['entity-type'];
  $entity_id = $results['entity-id'];

  // Get submission URL from source entity or webform.
  $url = $webform
    ->toUrl();
  if ($entity_type && $entity_id) {
    $source_entity = $this
      ->getEntityStorage($entity_type)
      ->load($entity_id);
    if ($source_entity
      ->hasLinkTemplate('canonical')) {
      $url = $source_entity
        ->toUrl();
    }
  }
  $timestamp = rand($results['created_min'], $results['created_max']);
  $this
    ->getSubmissionStorage()
    ->create([
    'webform_id' => $webform_id,
    'entity_type' => $entity_type,
    'entity_id' => $entity_id,
    'uid' => $uid,
    'remote_addr' => mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255),
    'uri' => preg_replace('#^' . base_path() . '#', '/', $url
      ->toString()),
    'data' => Yaml::encode($this->webformSubmissionGenerate
      ->getData($webform)),
    'created' => $timestamp,
    'changed' => $timestamp,
  ])
    ->save();
}