You are here

public function WebformSubmissionConditionsValidator::replaceCrossPageTargets in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformSubmissionConditionsValidator.php \Drupal\webform\WebformSubmissionConditionsValidator::replaceCrossPageTargets()

Replace hidden cross page targets with hidden inputs.

Parameters

array $conditions: An element's conditions.

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

array $targets: An array of hidden target selectors.

array $form: A form.

Return value

array The conditions with cross page targets replaced with hidden inputs.

1 call to WebformSubmissionConditionsValidator::replaceCrossPageTargets()
WebformSubmissionConditionsValidator::buildForm in src/WebformSubmissionConditionsValidator.php
Apply form #states to visible elements.

File

src/WebformSubmissionConditionsValidator.php, line 229

Class

WebformSubmissionConditionsValidator
Webform submission conditions (#states) validator.

Namespace

Drupal\webform

Code

public function replaceCrossPageTargets(array $conditions, WebformSubmissionInterface $webform_submission, array $targets, array &$form) {

  // Cache random cross page values.
  static $cross_page_values = [];
  $cross_page_conditions = [];
  foreach ($conditions as $index => $value) {
    if (is_int($index) && is_array($value) && WebformArrayHelper::isSequential($value)) {
      $cross_page_conditions[$index] = $this
        ->replaceCrossPageTargets($value, $webform_submission, $targets, $form);
    }
    else {
      $cross_page_conditions[$index] = $value;
      if (is_string($value) && in_array($value, [
        'and',
        'or',
        'xor',
      ])) {
        continue;
      }
      elseif (is_int($index)) {
        $selector = key($value);
        $condition = $value[$selector];
      }
      else {
        $selector = $index;
        $condition = $value;
      }
      if (!isset($targets[$selector])) {
        continue;
      }
      $condition_result = $this
        ->validateCondition($selector, $condition, $webform_submission);
      if ($condition_result === NULL) {
        continue;
      }
      $target_trigger = $condition_result ? 'value' : '!value';
      $target_name = 'webform_states_' . Crypt::hashBase64($selector);
      $target_selector = ':input[name="' . $target_name . '"]';

      // IMPORTANT:
      // Using a random value to make sure users can't determine a hidden
      // or computed element's value/result.
      if (!isset($cross_page_values[$target_name])) {
        $cross_page_values[$target_name] = rand();
      }
      $target_value = $cross_page_values[$target_name];
      if (is_int($index)) {
        unset($cross_page_conditions[$index][$selector]);
        $cross_page_conditions[$index][$target_selector] = [
          $target_trigger => $target_value,
        ];
      }
      else {
        unset($cross_page_conditions[$selector]);
        $cross_page_conditions[$target_selector] = [
          $target_trigger => $target_value,
        ];
      }

      // Append cross page element's result as a hidden input.
      $form[$target_name] = [
        '#type' => 'hidden',
        '#value' => $target_value,
      ];
    }
  }
  return $cross_page_conditions;
}