You are here

public static function Upload::validateExtensions in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/Widget/Upload.php \Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload::validateExtensions()

Validates a list of file extensions.

@See \Drupal\file\Plugin\Field\FieldType\FileItem::validateExtensions

File

src/Plugin/EntityBrowser/Widget/Upload.php, line 205

Class

Upload
Adds an upload field browser's widget.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\Widget

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);
    }
  }
}