You are here

public static function YamlFormElementStates::isDefaultValueCustomizedFormApiStates in YAML Form 8

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

Parameters

array $element: The form element.

Return value

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

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

File

src/Element/YamlFormElementStates.php, line 659

Class

YamlFormElementStates
Provides a form element to edit an element's #states.

Namespace

Drupal\yamlform\Element

Code

public 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.');
  }
  $states = $element['#default_value'];
  foreach ($states as $state => $conditions) {
    if (!isset($element['#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 (YamlFormArrayHelper::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)) {

        // Make sure only an 'and/or' operator is being used. XOR is not
        // support in UI because it is confusing to none technicl users.
        if (!in_array($condition, [
          'and',
          'or',
        ])) {
          return t('Conditional logic (Form API #states) is using the %operator operator.', [
            '%operator' => Unicode::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' => Unicode::strtoupper($condition),
          ]);
        }

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