You are here

public static function YamlFormElementAttributes::validateYamlFormElementAttributes in YAML Form 8

Validates element attributes.

File

src/Element/YamlFormElementAttributes.php, line 154

Class

YamlFormElementAttributes
Provides a form element for element attributes.

Namespace

Drupal\yamlform\Element

Code

public static function validateYamlFormElementAttributes(&$element, FormStateInterface $form_state, &$complete_form) {
  $values = $element['#value'];
  $attributes = [];
  if ($values['class']) {
    if (isset($element['class']['select'])) {
      $class = $element['class']['select']['#value'];
      $class_other = $element['class']['other']['#value'];
      if (isset($class[YamlFormSelectOther::OTHER_OPTION])) {
        unset($class[YamlFormSelectOther::OTHER_OPTION]);
        $class[$class_other] = $class_other;
      }
      if ($class) {
        $attributes['class'] = array_values($class);
      }
    }
    else {
      $attributes['class'] = [
        $values['class'],
      ];
    }
  }
  if ($values['style']) {
    $attributes['style'] = $values['style'];
  }
  if (!empty($values['attributes'])) {
    $attributes += Yaml::decode($values['attributes']);
  }
  $form_state
    ->setValueForElement($element['class'], NULL);
  $form_state
    ->setValueForElement($element['style'], NULL);
  $form_state
    ->setValueForElement($element['attributes'], NULL);
  $form_state
    ->setValueForElement($element, $attributes);
}