You are here

public function FileUpload::getForm in Lightning Media 8.2

Same name and namespace in other branches
  1. 8.4 src/Plugin/EntityBrowser/Widget/FileUpload.php \Drupal\lightning_media\Plugin\EntityBrowser\Widget\FileUpload::getForm()
  2. 8 src/Plugin/EntityBrowser/Widget/FileUpload.php \Drupal\lightning_media\Plugin\EntityBrowser\Widget\FileUpload::getForm()
  3. 8.3 src/Plugin/EntityBrowser/Widget/FileUpload.php \Drupal\lightning_media\Plugin\EntityBrowser\Widget\FileUpload::getForm()

Overrides EntityFormProxy::getForm

File

src/Plugin/EntityBrowser/Widget/FileUpload.php, line 51

Class

FileUpload
An Entity Browser widget for creating media entities from uploaded files.

Namespace

Drupal\lightning_media\Plugin\EntityBrowser\Widget

Code

public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
  $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
  $form['input'] = [
    '#type' => 'ajax_upload',
    '#title' => $this
      ->t('File'),
    '#process' => [
      [
        $this,
        'processUploadElement',
      ],
    ],
  ];
  $validators = $form_state
    ->get([
    'entity_browser',
    'widget_context',
    'upload_validators',
  ]) ?: [];

  // If the widget context didn't specify any file extension validation, add
  // it as the first validator, allowing it to accept only file extensions
  // associated with existing media bundles.
  if (empty($validators['file_validate_extensions'])) {
    $allowed_bundles = $this
      ->getAllowedBundles($form_state);
    $validators = array_merge([
      'file_validate_extensions' => [
        implode(' ', $this->helper
          ->getFileExtensions(TRUE, $allowed_bundles)),
      ],
      // This must be a function because file_validate() still thinks that
      // function_exists() is a good way to ensure callability.
      'lightning_media_validate_upload' => [
        $allowed_bundles,
      ],
    ], $validators);
  }
  $form['input']['#upload_validators'] = $validators;
  return $form;
}