public static function OptionsWidgetBase::validateElement

Form validation handler for widget elements.

Parameters

array $element: The form element.

array $form_state: The form state.

File

drupal/core/modules/options/lib/Drupal/options/Plugin/field/widget/OptionsWidgetBase.php, line 75
Contains \Drupal\options\Plugin\field\widget\OptionsWidgetBase.

Class

OptionsWidgetBase
Base class for the 'options_*' widgets.

Namespace

Drupal\options\Plugin\field\widget

Code

public static function validateElement(array $element, array &$form_state) {
  if ($element['#required'] && $element['#value'] == '_none') {
    form_error($element, t('!name field is required.', array(
      '!name' => $element['#title'],
    )));
  }

  // Massage submitted form values.
  // Drupal\field\Plugin\Type\Widget\WidgetBase::submit() expects values as
  // an array of values keyed by delta first, then by column, while our
  // widgets return the opposite.
  if (is_array($element['#value'])) {
    $values = array_values($element['#value']);
  }
  else {
    $values = array(
      $element['#value'],
    );
  }

  // Filter out the 'none' option. Use a strict comparison, because
  // 0 == 'any string'.
  $index = array_search('_none', $values, TRUE);
  if ($index !== FALSE) {
    unset($values[$index]);
  }

  // Transpose selections from field => delta to delta => field.
  $items = array();
  foreach ($values as $value) {
    $items[] = array(
      $element['#key_column'] => $value,
    );
  }
  form_set_value($element, $items, $form_state);
}