You are here

public function Webform::getSubmissionForm in Webform 8.5

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

Get webform submission webform.

Parameters

array $values: (optional) An array of values to set, keyed by property name.

string $operation: (optional) The operation identifying the webform submission form variation to be returned. Defaults to 'add'. This is typically used in routing.

Return value

array A render array representing a webform submission webform.

Overrides WebformInterface::getSubmissionForm

File

src/Entity/Webform.php, line 1199

Class

Webform
Defines the webform entity.

Namespace

Drupal\webform\Entity

Code

public function getSubmissionForm(array $values = [], $operation = 'add') {

  // Test a single webform variant which is set via
  // ?_webform_handler[ELEMENT_KEY]={variant_id}.
  $webform_variant = \Drupal::request()->query
    ->get('_webform_variant') ?: [];
  if ($webform_variant) {
    $is_add_operation = $operation === 'add' && $this
      ->access('update');
    $is_test_operation = $operation === 'test' && $this
      ->access('test');
    $is_share_operation = strpos(\Drupal::routeMatch()
      ->getRouteName(), 'entity.webform.share_page') === 0;
    if ($is_add_operation || $is_test_operation || $is_share_operation) {
      $values += [
        'data' => [],
      ];
      $values['data'] = $webform_variant + $values['data'];
    }
  }

  // Set this webform's id which can be used by preCreate hooks.
  $values['webform_id'] = $this
    ->id();

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $this
    ->entityTypeManager()
    ->getStorage('webform_submission')
    ->create($values);

  // Pass this webform to the webform submission as a direct entity reference.
  // This guarantees overridden properties and settings are maintained.
  // Not sure why the overridden webform is not being correctly passed to the
  // webform submission.
  // @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::setValue
  if ($this
    ->isOverridden()) {
    $webform_submission->webform_id->entity = $this;
  }
  return \Drupal::service('entity.form_builder')
    ->getForm($webform_submission, $operation);
}