You are here

public static function FileUrlWidget::process 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::process()

Form API callback: Processes a file_generic field element.

Expands the file_generic type to include the description and display fields.

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

Overrides FileWidget::process

File

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

Class

FileUrlWidget
Plugin implementation of the 'file_url_generic' widget.

Namespace

Drupal\file_url\Plugin\Field\FieldWidget

Code

public static function process($element, FormStateInterface $form_state, $form) {
  $file_url_type = isset($element['#value']['file_url_type']) ? $element['#value']['file_url_type'] : NULL;
  $file_url_remote = $element['#value']['file_url_remote'];
  $file_url_remote_is_valid = UrlHelper::isValid($file_url_remote, TRUE);
  if ($file_url_remote_is_valid && $file_url_type) {

    // The parent widget only populates '#files' with managed files, we add
    // the remote files too, to get them listed as items in the widget.
    $remote_file = RemoteFile::load($file_url_remote);
    $element['#files'] = [
      $file_url_remote => $remote_file,
    ];
    $file_link = [
      '#type' => 'link',
      '#title' => $remote_file
        ->getFileUri(),
      '#url' => Url::fromUri($remote_file
        ->getFileUri()),
    ];
    if ($element['#multiple']) {
      $element["file_{$file_url_remote}"]['selected'] = [
        '#type' => 'checkbox',
        '#title' => \Drupal::service('renderer')
          ->renderPlain($file_link),
      ];
    }
    else {
      $element["file_{$file_url_remote}"]['filename'] = $file_link + [
        '#weight' => -10,
      ];
    }
  }
  $access_file_url_elements = empty($element['#files']) && !$file_url_remote_is_valid || !$file_url_type;

  // Build the file URL additional sub-elements.
  $element['file_url_type'] = [
    '#type' => 'radios',
    '#options' => [
      static::TYPE_UPLOAD => t('Upload file'),
      static::TYPE_REMOTE => t('Remote file URL'),
    ],
    '#default_value' => $file_url_type,
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
    '#access' => $access_file_url_elements,
    '#weight' => 5,
  ];
  $field_name = $element['#field_name'];
  $delta = $element['#delta'];
  $selector = ':input[name="' . $field_name . '[' . $delta . '][file_url_type]"]';
  $remote_visible = [
    $selector => [
      'value' => static::TYPE_REMOTE,
    ],
  ];
  $element['file_url_remote'] = [
    '#type' => 'url',
    '#title' => t('Remote URL'),
    '#title_display' => 'invisible',
    '#description' => t('This must be an external URL such as <em>http://example.com</em>.'),
    '#default_value' => $file_url_remote,
    // Only show this field when the 'remote' radio is selected.
    '#states' => [
      'visible' => $remote_visible,
    ],
    '#attached' => [
      // Load the JS functionality that triggers automatically the 'Upload'
      // button when a remote URL is entered.
      'library' => [
        'file_url/remote_url',
      ],
    ],
    '#attributes' => [
      // Used by 'file_url/remote_url' library identify the text field.
      'data-drupal-file-url-remote' => TRUE,
    ],
    '#access' => $access_file_url_elements,
    '#weight' => 15,
  ];

  // Only show this field when the 'upload' radio is selected. Add also a
  // wrapper around file upload, so states knows what field to target.
  $upload_visible = [
    $selector => [
      'value' => static::TYPE_UPLOAD,
    ],
  ];
  $element['upload']['#states']['visible'] = $upload_visible;
  $element['upload']['#theme_wrappers'][] = 'form_element';

  // The upload instructions are added directly to the file upload element.
  $element['upload']['#description'] = [
    '#theme' => 'file_upload_help',
    '#description' => '',
    '#upload_validators' => $element['#upload_validators'],
    '#cardinality' => $element['#cardinality'],
  ];
  $element['upload']['#weight'] = 10;

  // Make sure the upload button is the last in form element.
  $element['upload_button']['#weight'] = 20;
  return parent::process($element, $form_state, $form);
}