You are here

public static function MiconLinkWidget::validateElement in Micon 8

Same name and namespace in other branches
  1. 2.x micon_link/src/Plugin/Field/FieldWidget/MiconLinkWidget.php \Drupal\micon_link\Plugin\Field\FieldWidget\MiconLinkWidget::validateElement()

Recursively clean up options array if no data-icon is set.

File

micon_link/src/Plugin/Field/FieldWidget/MiconLinkWidget.php, line 136

Class

MiconLinkWidget
Plugin implementation of the 'link' widget.

Namespace

Drupal\micon_link\Plugin\Field\FieldWidget

Code

public static function validateElement($element, FormStateInterface $form_state, $form) {
  $values = $form_state
    ->getValue($element['#parents']);
  if (!empty($values)) {
    foreach ($values['options']['attributes'] as $attribute => $value) {
      if (!empty($value)) {
        $values['options']['attributes'][$attribute] = $value;
        $values['attributes'][$attribute] = $value;
      }
      else {
        unset($values['options']['attributes'][$attribute]);
        unset($values['attributes'][$attribute]);
      }
    }
  }
  $form_state
    ->setValueForElement($element, $values);
}