You are here

public static function VideoUploadWidget::validateExtensions in Video 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/VideoUploadWidget.php \Drupal\video\Plugin\Field\FieldWidget\VideoUploadWidget::validateExtensions()

Form API callback.

This function is assigned as an #element_validate callback in settingsForm().

This doubles as a convenience clean-up function and a validation routine. Commas are allowed by the end-user, but ultimately the value will be stored as a space-separated list for compatibility with file_validate_extensions().

File

src/Plugin/Field/FieldWidget/VideoUploadWidget.php, line 119

Class

VideoUploadWidget
Plugin implementation of the 'video_upload' widget.

Namespace

Drupal\video\Plugin\Field\FieldWidget

Code

public static function validateExtensions($element, FormStateInterface $form_state) {
  if (!empty($element['#value'])) {
    $extensions = preg_replace('/([, ]+\\.?)/', ' ', trim(strtolower($element['#value'])));
    $extensions = array_filter(explode(' ', $extensions));
    $extensions = implode(' ', array_unique($extensions));
    if (!preg_match('/^([a-z0-9]+([.][a-z0-9])* ?)+$/', $extensions)) {
      $form_state
        ->setError($element, t('The list of allowed extensions is not valid, be sure to exclude leading dots and to separate extensions with a comma or space.'));
    }
    else {
      $form_state
        ->setValueForElement($element, $extensions);
    }
  }
}