You are here

protected static function WebformElementStates::isDefaultValueCustomizedFormApiStates in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformElementStates.php \Drupal\webform\Element\WebformElementStates::isDefaultValueCustomizedFormApiStates()

Determine if an element's #states array is customized.

Parameters

array $element: The element.

Return value

bool|string FALSE if #states array is not customized or a warning message.

1 call to WebformElementStates::isDefaultValueCustomizedFormApiStates()
WebformElementStates::processWebformStates in src/Element/WebformElementStates.php
Expand an email confirm field into two HTML5 email elements.

File

src/Element/WebformElementStates.php, line 998

Class

WebformElementStates
Provides a webform element to edit an element's #states.

Namespace

Drupal\webform\Element

Code

protected static function isDefaultValueCustomizedFormApiStates(array $element) {

  // Empty default values are not customized.
  if (empty($element['#default_value'])) {
    return FALSE;
  }

  // #states must always be an array.
  if (!is_array($element['#default_value'])) {
    return t('Conditional logic (Form API #states) is not an array.');
  }
  $state_options = OptGroup::flattenOptions($element['#state_options']);
  $states = $element['#default_value'];
  foreach ($states as $state => $conditions) {
    if (!isset($state_options[$state])) {
      return t('Conditional logic (Form API #states) is using a custom %state state.', [
        '%state' => $state,
      ]);
    }

    // If associative array we can assume that it not customized.
    if (WebformArrayHelper::isAssociative($conditions)) {
      $trigger = reset($conditions);
      if (count($trigger) > 1) {
        return t('Conditional logic (Form API #states) is using multiple triggers.');
      }
      continue;
    }
    $operator = NULL;
    foreach ($conditions as $condition) {

      // Make sure only one condition is being specified.
      if (is_array($condition) && count($condition) > 1) {
        return t('Conditional logic (Form API #states) is using multiple nested conditions.');
      }
      elseif (is_string($condition)) {
        if (!in_array($condition, [
          'and',
          'or',
          'xor',
        ])) {
          return t('Conditional logic (Form API #states) is using the %operator operator.', [
            '%operator' => mb_strtoupper($condition),
          ]);
        }

        // Make sure the same operator is being used between the conditions.
        if ($operator && $operator !== $condition) {
          return t('Conditional logic (Form API #states) has multiple operators.', [
            '%operator' => mb_strtoupper($condition),
          ]);
        }

        // Set the operator.
        $operator = $condition;
      }
    }
  }
  return FALSE;
}