You are here

public function WebformSubmissionForm::copyFormValuesToEntity in Webform 8.5

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

Copies top-level form values to entity properties

This should not change existing entity properties that are not being edited by this form.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity the current form should operate upon.

array $form: A nested array of form elements comprising the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ContentEntityForm::copyFormValuesToEntity

File

src/WebformSubmissionForm.php, line 586

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

public function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {

  // NOTE: We are not copying form values to the entity because
  // webform element keys can override webform submission properties.

  /* @var $webform_submission \Drupal\webform\WebformSubmissionInterface */
  $webform_submission = $entity;
  $webform = $webform_submission
    ->getWebform();

  // Get elements values from webform submission and merge existing data.
  $element_values = array_intersect_key($form_state
    ->getValues(), $webform
    ->getElementsInitializedFlattenedAndHasValue());
  $webform_submission
    ->setData($element_values + $webform_submission
    ->getData());

  // Get field values.
  // This used to support the Workflows Field module.
  // @see https://www.drupal.org/project/webform/issues/3002547
  // @see https://www.drupal.org/project/workflows_field
  $base_field_definitions = $this->entityFieldManager
    ->getBaseFieldDefinitions($entity
    ->getEntityTypeId());
  $all_fields = $webform_submission
    ->getFields(FALSE);
  $bundle_fields = array_diff_key($all_fields, $base_field_definitions);
  $field_values = array_intersect_key($form_state
    ->getValues(), $bundle_fields);
  foreach ($field_values as $name => $field_value) {
    $webform_submission
      ->set($name, $field_value);
  }

  // Set current page.
  if ($current_page = $this
    ->getCurrentPage($form, $form_state)) {
    $entity
      ->setCurrentPage($current_page);
  }

  // Set in draft.
  $in_draft = $form_state
    ->get('in_draft');
  if ($in_draft !== NULL) {
    $entity
      ->set('in_draft', $in_draft);
  }
}