You are here

protected function WebformSubmissionConditionsValidator::checkCondition in Webform 6.x

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

Check a condition.

Parameters

array $element: An element.

string $selector: The element's selector.

array $condition: The condition.

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

Return value

bool|null TRUE if condition is validated. NULL if the condition can't be evaluated.

1 call to WebformSubmissionConditionsValidator::checkCondition()
WebformSubmissionConditionsValidator::validateCondition in src/WebformSubmissionConditionsValidator.php
Validate #state condition.

File

src/WebformSubmissionConditionsValidator.php, line 729

Class

WebformSubmissionConditionsValidator
Webform submission conditions (#states) validator.

Namespace

Drupal\webform

Code

protected function checkCondition(array $element, $selector, array $condition, WebformSubmissionInterface $webform_submission) {
  $trigger = key($condition);
  $trigger_value = $condition[$trigger];
  $element_plugin = $this->elementManager
    ->getElementInstance($element);

  // Ignored conditions for generic webform elements.
  if ($element_plugin instanceof WebformElement) {
    return TRUE;
  }
  $element_value = $element_plugin
    ->getElementSelectorInputValue($selector, $trigger, $element, $webform_submission);

  // Process trigger sub state used for custom #states API validation.
  // @see Drupal.behaviors.webformStatesComparisons
  // @see http://drupalsun.com/julia-evans/2012/03/09/extending-form-api-states-regular-expressions
  if ($trigger === 'value' && is_array($trigger_value)) {
    $trigger_substate = key($trigger_value);
    if (in_array($trigger_substate, [
      'pattern',
      '!pattern',
      'less',
      'less_equal',
      'greater',
      'greater_equal',
      'between',
      '!between',
    ])) {
      $trigger = $trigger_substate;
      $trigger_value = reset($trigger_value);
    }
  }

  // Process trigger state/negate.
  list($trigger, $trigger_negate) = $this
    ->processState($trigger);

  // Process triggers (aka remote conditions).
  // @see \Drupal\webform\Element\WebformElementStates::processWebformStates
  if ($element_plugin
    ->hasMultipleValues($element) && $trigger !== 'empty') {
    $result = FALSE;
    $element_values = (array) $element_value;
    foreach ($element_values as $element_value) {
      $trigger_result = $this
        ->checkConditionTrigger($trigger, $trigger_value, $element_value);
      if ($trigger_result !== FALSE) {
        $result = $trigger_result;
      }
    }
  }
  else {
    $result = $this
      ->checkConditionTrigger($trigger, $trigger_value, $element_value);
  }
  if ($result === NULL) {
    return FALSE;
  }
  return $trigger_negate ? !$result : $result;
}