You are here

public static function Duration::validateElement in Duration Field 8

Sets the value of the submitted element.

File

src/Element/Duration.php, line 173

Class

Duration
Provides a duration form element.

Namespace

Drupal\duration_field\Element

Code

public static function validateElement(&$element, FormStateInterface $form_state, $form) {
  $mappings = [
    'year' => 'Y',
    'month' => 'M',
    'day' => 'D',
    'hour' => 'H',
    'minute' => 'M',
    'second' => 'S',
  ];
  $values = [];
  foreach ($mappings as $key => $duration_key) {
    if (isset($element[$key])) {
      $values[$key] = $element[$key]['#value'];
    }
  }
  $form_state
    ->setValue($element['#parents'], DurationService::convertValue($values));
}