You are here

public static function AjaxUpload::process in Lightning Media 8

Same name and namespace in other branches
  1. 8.4 src/Element/AjaxUpload.php \Drupal\lightning_media\Element\AjaxUpload::process()
  2. 8.2 src/Element/AjaxUpload.php \Drupal\lightning_media\Element\AjaxUpload::process()
  3. 8.3 src/Element/AjaxUpload.php \Drupal\lightning_media\Element\AjaxUpload::process()

Processes the element.

Parameters

array $element: The unprocessed element.

FormStateInterface $form_state: The current form state.

Return value

array The processed element.

Overrides InteractiveUpload::process

1 call to AjaxUpload::process()
FileUpload::processUploadElement in src/Plugin/EntityBrowser/Widget/FileUpload.php
Processes the upload element.

File

src/Element/AjaxUpload.php, line 18

Class

AjaxUpload
An interactive, AJAX-ey file upload form element.

Namespace

Drupal\lightning_media\Element

Code

public static function process(array $element, FormStateInterface $form_state) {
  $element = parent::process($element, $form_state);

  // Generate a CSS ID for the wrapping DIV.
  $wrapper_id = implode('-', $element['#parents']);
  $wrapper_id = Html::cleanCssIdentifier($wrapper_id);

  // The element being processed is just a wrapper, and does not accept input
  // or support AJAX directly. Still, store the wrapping DIV ID in a spot
  // where other elements can access it if they need to refer to it.
  $element['#ajax']['wrapper'] = $wrapper_id;

  // Bring in the File module's slick auto-uploading stuff.
  $element['#attached']['library'][] = 'file/drupal.file';

  // The js-form-managed-file class is needed for the File module's
  // auto-upload JavaScript to target this element.
  $element['#prefix'] = '<div class="js-form-managed-file" id="' . $wrapper_id . '">';
  $element['#suffix'] = '</div>';

  // Hide the upload button. It will be triggered by the auto-upload JS.
  $element['upload']['#attributes']['class'][] = 'js-hide';

  // As far as AJAX is concerned, the Upload and Remove buttons do the same
  // thing (return their parent element). The differences lie in their
  // respective submit functions.
  $element['upload']['#ajax'] = $element['remove']['#ajax'] = [
    'callback' => [
      static::class,
      'el',
    ],
    'wrapper' => $wrapper_id,
  ];
  return $element;
}