You are here

public static function FileUrlWidget::value in File URL 2.0.x

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

Form API callback. Retrieves the value for the file_generic field element.

This method is assigned as a #value_callback in formElement() method.

Overrides FileWidget::value

File

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

Class

FileUrlWidget
Plugin implementation of the 'file_url_generic' widget.

Namespace

Drupal\file_url\Plugin\Field\FieldWidget

Code

public static function value($element, $input, FormStateInterface $form_state) {
  $file_url_remote = $file_url_type = NULL;
  if (!empty($element['#default_value']['fids'])) {
    $url = $element['#default_value']['fids'][0];
    $file = FileUrlHandler::urlToFile($url);
    if (!FileUrlHandler::isRemote($file)) {

      // Normalise managed file IDs from URLs to numeric IDs before calling
      // the parent method.
      $element['#default_value']['fids'] = [
        $file
          ->id(),
      ];
      $file_url_type = 'upload';
    }
    else {
      $file_url_remote = $url;
      $file_url_type = 'remote';
    }
  }
  $return = parent::value($element, $input, $form_state);
  if ($input !== FALSE) {
    if (isset($input['file_url_type']) && $input['file_url_type'] === static::TYPE_REMOTE) {
      if (!empty($input['file_url_remote']) && !$input['fids']) {
        $file_url_remote = $input['file_url_remote'];
      }
    }
  }
  if (!empty($file_url_remote)) {

    // The parent widget is doing this but only for managed files.
    $return['fids'] = [
      $file_url_remote,
    ];
  }

  // Provide file URL parameters as part of the value.
  if (empty($return['file_url_remote'])) {
    $return['file_url_remote'] = $file_url_remote;
  }
  if (empty($return['file_url_type'])) {
    $return['file_url_type'] = $file_url_type;
  }
  return $return;
}