You are here

public function WebformComputedBase::postSave in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformComputedBase.php \Drupal\webform\Plugin\WebformElement\WebformComputedBase::postSave()

Acts on a saved webform submission element before the insert or update hook is invoked.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides WebformElementBase::postSave

File

src/Plugin/WebformElement/WebformComputedBase.php, line 263

Class

WebformComputedBase
Provides a base class for 'webform_computed' elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function postSave(array &$element, WebformSubmissionInterface $webform_submission, $update = TRUE) {
  if ($update || empty($element['#store']) || $webform_submission
    ->getWebform()
    ->getSetting('results_disabled')) {
    return;
  }

  // Recalculate the stored computed value to account new a submission's
  // generated sid and serial.
  $key = $element['#webform_key'];
  $value = (string) $this
    ->computeValue($element, $webform_submission);

  // Update the submission's value.
  $webform_submission
    ->setElementData($key, $value);

  // The below database update is a one-off solution because there is
  // currently no other instances where a single element's value
  // needs to be updated.
  // @see \Drupal\webform\WebformSubmissionStorage::saveData
  $fields = [
    'webform_id' => $webform_submission
      ->getWebform()
      ->id(),
    'sid' => $webform_submission
      ->id(),
    'name' => $key,
    'property' => '',
    'delta' => 0,
    'value' => $value,
  ];
  $this->database
    ->update('webform_submission_data')
    ->fields($fields)
    ->condition('webform_id', $fields['webform_id'])
    ->condition('sid', $fields['sid'])
    ->condition('name', $fields['name'])
    ->execute();
}