You are here

protected function SetWebformFieldValue::doExecute in RULES WEBFORM 8

Same name and namespace in other branches
  1. 3.x src/Plugin/RulesAction/SetWebformFieldValue.php \Drupal\rules_webform\Plugin\RulesAction\SetWebformFieldValue::doExecute()

Set a webform field value.

Parameters

mixed $field: A webform field.

mixed $value: New value of a webform field.

string $event_name: An event name.

File

src/Plugin/RulesAction/SetWebformFieldValue.php, line 48

Class

SetWebformFieldValue
Action for altering of webform submission value.

Namespace

Drupal\rules_webform\Plugin\RulesAction

Code

protected function doExecute($field, $value, $event_name) {
  $submission = \Drupal::state()
    ->get('rules_webform.submission');

  // Do nothing if a submission has been removed by
  // 'delete_webform_submission' action.
  if (!isset($submission) || !$event_name) {
    return;
  }
  $data = $submission
    ->getData();
  $field_name = $this
    ->getContexts()['field']
    ->getContextData()
    ->getName();
  if (array_key_exists($field_name, $data)) {
    $data[$field_name] = $value;
    $submission
      ->setData($data);

    // The 'submission_update' event fires from
    // hook_webform_submission_presave(). Therefore we don't have to save
    // changes. Saving is needed for events like 'webform_submit' and others.
    if ($event_name != 'updating_submission') {
      $submission
        ->save();
    }
  }
}