You are here

public static function FileWidget::process in PlUPload File Widget 8

Override to replace the upload/file HTML control with the PLUPLOAD form element.

Overrides FileWidget::process

File

src/Plugin/Field/FieldWidget/FileWidget.php, line 45

Class

FileWidget
Plugin annotation @FieldWidget( id = "plupload_file_widget", label = @Translation("PLupload widget"), field_types = { "file" } )

Namespace

Drupal\plupload_widget\Plugin\Field\FieldWidget

Code

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

  // If the form element does not have
  // an uplad control, skip this.
  if (!isset($element['upload'])) {
    return $element;
  }

  /** @var UploadConfiguration */
  $configuration = unserialize($form[$element['#parents'][0]]['#upload_configuration']);

  // Change the element description because
  // the PLUPLOAD widget MUST have the
  // extension filters as descripiton.
  // @see \Drupal\plupload\Element\PlUploadFile::preRenderPlUploadFile()
  // @see \Drupal\file\Plugin\Field\FieldWidget\FileWidget::formElement()
  $file_upload_help = array(
    '#theme' => 'file_upload_help',
    '#description' => '',
    '#upload_validators' => '',
    '#cardinality' => $configuration->cardinality,
  );
  $element['#description'] = \Drupal::service('renderer')
    ->renderPlain($file_upload_help);

  // Replace the upload HTML element with PLUPLOAD
  // for a single file.
  $element['upload'] = [
    '#type' => 'plupload',
    '#title' => t('Upload files'),
    //'#description' => t('This multi-upload widget uses Plupload library.'),
    '#autoupload' => TRUE,
    '#autosubmit' => TRUE,
    '#submit_element' => "[name={$element['upload_button']['#name']}]",
    '#upload_validators' => [
      'file_validate_extensions' => $configuration->validators['file_validate_extensions'],
    ],
    '#plupload_settings' => [
      'runtimes' => 'html5,flash,silverlight,html4',
      'chunk_size' => $configuration->chunk_size . 'b',
      'max_file_size' => $configuration->max_size . 'b',
      'max_file_count' => 1,
    ],
    '#event_callbacks' => [
      'FilesAdded' => 'Drupal.plupload_widget.filesAddedCallback',
      'UploadComplete' => 'Drupal.plupload_widget.uploadCompleteCallback',
    ],
    '#attached' => [
      // We need to specify the plupload attachment because it is a default
      // and will be overriden by our value.
      'library' => [
        'plupload_widget/plupload_widget',
        'plupload/plupload',
      ],
    ],
  ];
  return $element;
}