You are here

public static function FileUrlWidget::validate in File URL 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldWidget/FileUrlWidget.php \Drupal\file_url\Plugin\Field\FieldWidget\FileUrlWidget::validate()

Replaces the ManagedFile validator.

Parameters

array $element: The element render array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

array $complete_form: The full form render array.

File

src/Plugin/Field/FieldWidget/FileUrlWidget.php, line 197

Class

FileUrlWidget
Plugin implementation of the 'file_url_generic' widget.

Namespace

Drupal\file_url\Plugin\Field\FieldWidget

Code

public static function validate(&$element, FormStateInterface $form_state, &$complete_form) {
  $file_url_type = NestedArray::getValue($form_state
    ->getValues(), array_merge($element['#parents'], [
    'file_url_type',
  ]));
  $remote_url = NestedArray::getValue($form_state
    ->getValues(), array_merge($element['#parents'], [
    'file_url_remote',
  ]));
  $fids = NestedArray::getValue($form_state
    ->getValues(), array_merge($element['#parents'], [
    'fids',
  ]));
  if (($remote_url || $fids) && !in_array($file_url_type, [
    static::TYPE_UPLOAD,
    static::TYPE_REMOTE,
  ], TRUE)) {

    // @todo Find a way to guess the type from values. Temporary disable this
    //   validation.
    // $form_state->setError($element, t("A type should be selected. Either 'Upload file' or 'Remote file URL'."));
  }
  if ($file_url_type === static::TYPE_UPLOAD) {

    // If it's a file upload, pass it to the 'managed_file' validation.
    ManagedFile::validateManagedFile($element, $form_state, $complete_form);
  }
}