You are here

public function WebformSubmissionConditionsValidator::validateConditions in Webform 6.x

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

Validate #state conditions.

Parameters

array $conditions: An associative array containing conditions.

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

Return value

bool|null TRUE if the conditions validate. NULL if the conditions can't be processed. NULL is returned when there is an invalid selector or a missing element in the conditions.

Overrides WebformSubmissionConditionsValidatorInterface::validateConditions

See also

\Drupal\Core\Form\FormHelper::processStates

5 calls to WebformSubmissionConditionsValidator::validateConditions()
WebformSubmissionConditionsValidator::buildForm in src/WebformSubmissionConditionsValidator.php
Apply form #states to visible elements.
WebformSubmissionConditionsValidator::isElementEnabled in src/WebformSubmissionConditionsValidator.php
Determine if an element is enabled.
WebformSubmissionConditionsValidator::isElementVisible in src/WebformSubmissionConditionsValidator.php
Determine if an element is visible.
WebformSubmissionConditionsValidator::validateFormElement in src/WebformSubmissionConditionsValidator.php
Validate a form element.
WebformSubmissionConditionsValidator::validateState in src/WebformSubmissionConditionsValidator.php
Validate state with conditions.

File

src/WebformSubmissionConditionsValidator.php, line 592

Class

WebformSubmissionConditionsValidator
Webform submission conditions (#states) validator.

Namespace

Drupal\webform

Code

public function validateConditions(array $conditions, WebformSubmissionInterface $webform_submission) {
  if (empty($conditions)) {
    return TRUE;
  }

  // Determine condition logic.
  // @see Drupal.states.Dependent.verifyConstraints
  if (WebformArrayHelper::isSequential($conditions)) {
    $condition_logic = in_array('xor', $conditions) ? 'xor' : 'or';
  }
  else {
    $condition_logic = 'and';
  }
  $condition_results = [];
  foreach ($conditions as $index => $value) {

    // Skip and, or, and xor.
    if (is_string($value) && in_array($value, [
      'and',
      'or',
      'xor',
    ])) {
      continue;
    }
    if (is_int($index) && is_array($value)) {

      // Validate nested conditions.
      // NOTE: Nested conditions is not supported via the UI.
      $nested_result = $this
        ->validateConditions($value, $webform_submission);
      if ($nested_result === NULL) {
        return NULL;
      }
      $condition_results[] = $nested_result;
    }
    else {
      if (is_int($index)) {
        $selector = key($value);
        $condition = $value[$selector];
      }
      else {
        $selector = $index;
        $condition = $value;
      }
      $condition_result = $this
        ->validateCondition($selector, $condition, $webform_submission);
      if ($condition_result === NULL) {
        return NULL;
      }
      $condition_results[] = $this
        ->validateCondition($selector, $condition, $webform_submission);
    }
  }

  // Process condition logic. (XOR, AND, or OR)
  $conditions_sum = array_sum($condition_results);
  $conditions_total = count($condition_results);
  switch ($condition_logic) {
    case 'xor':
      return $conditions_sum === 1;
    case 'or':
      return (bool) $conditions_sum;
    case 'and':
      return $conditions_sum === $conditions_total;
    default:

      // Never called.
      return NULL;
  }
}