View source
<?php
namespace Drupal\media_directories_ui\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Url;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\Utility\Token;
use Drupal\media\MediaInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileUploadForm extends AddMediaFormBase implements TrustedCallbackInterface {
protected $elementInfo;
public static function trustedCallbacks() {
return [
'preRenderUploadElement',
];
}
public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, Token $token, ThemeManagerInterface $theme_manager, ElementInfoManagerInterface $element_info) {
parent::__construct($entity_type_manager, $current_user, $token, $theme_manager);
$this->elementInfo = $element_info;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('current_user'), $container
->get('token'), $container
->get('theme.manager'), $container
->get('element_info'));
}
protected function buildInputElement(array $form, FormStateInterface $form_state) {
$media_type = $this
->getMediaType($form_state);
$source_field = $media_type
->getSource()
->getConfiguration()['source_field'];
$field_config = $this->entityTypeManager
->getStorage('field_config')
->load('media.' . $media_type
->id() . '.' . $source_field);
$process = (array) $this->elementInfo
->getInfoProperty('managed_file', '#process', []);
$pre_render = (array) $this->elementInfo
->getInfoProperty('managed_file', '#pre_render', []);
$form['container']['upload'] = [
'#type' => 'managed_file',
'#title' => $field_config
->label(),
'#description' => $this
->t('Allowed file extensions: @extensions', [
'@extensions' => $field_config
->getSetting('file_extensions'),
]),
'#upload_validators' => $this
->getUploadValidators($media_type),
'#multiple' => TRUE,
'#upload_location' => $this
->getUploadLocation($field_config
->getSettings()),
'#process' => array_merge([
'::validateUploadElement',
], $process, [
'::processUploadElement',
]),
'#pre_render' => array_merge($pre_render, [
[
static::class,
'preRenderUploadElement',
],
]),
];
return $form;
}
public function validateUploadElement(array $element, FormStateInterface $form_state) {
return $element;
}
public function processUploadElement(array $element, FormStateInterface $form_state) {
$element['upload_button']['#submit'] = [
'::uploadButtonSubmit',
];
$media_type = $form_state
->get('media_type');
$element['upload_button']['#limit_validation_errors'] = [
[
'upload',
],
[
'current_selection',
],
];
$element['upload_button']['#ajax'] = [
'callback' => '::updateFormCallback',
'wrapper' => 'media-library-wrapper',
'url' => Url::fromRoute('media_directories_ui.media.add'),
'options' => [
'query' => [
'media_type' => is_object($media_type) ? $media_type
->id() : $media_type,
'target_bundles' => $this
->getTargetBundles($form_state),
'active_directory' => $this
->getDirectory($form_state),
'cardinality' => $this
->getCardinality($form_state),
'selection_mode' => $this
->getSelectionMode($form_state),
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
],
];
if (isset($element['remove_button'])) {
$element['proceed_button'] = [
'#type' => 'submit',
'#value' => $this
->t('Proceed with all files from the list'),
];
if (isset($element['upload_button']['#validate'])) {
$element['proceed_button']['#validate'] = $element['upload_button']['#validate'];
}
if (isset($element['upload_button']['#submit'])) {
$element['proceed_button']['#submit'] = $element['upload_button']['#submit'];
}
if (isset($element['upload_button']['#limit_validation_errors'])) {
$element['proceed_button']['#limit_validation_errors'] = $element['upload_button']['#limit_validation_errors'];
}
if (isset($element['upload_button']['#ajax'])) {
$element['proceed_button']['#ajax'] = $element['upload_button']['#ajax'];
}
if (isset($element['upload_button']['#weight'])) {
$element['proceed_button']['#weight'] = $element['upload_button']['#weight'];
}
}
return $element;
}
public static function preRenderUploadElement($element) {
if (isset($element['proceed_button'])) {
if (isset($element['remove_button']['#access'])) {
$element['proceed_button']['#access'] = $element['remove_button']['#access'];
}
}
return $element;
}
public function uploadButtonSubmit(array $form, FormStateInterface $form_state) {
$files = $this->entityTypeManager
->getStorage('file')
->loadMultiple($form_state
->getValue('upload', []));
$this
->processInputValues($files, $form, $form_state);
}
protected function prepareMediaEntityForSave(MediaInterface $media) {
$file = $media
->get($this
->getSourceFieldName($media->bundle->entity))->entity;
$file
->setPermanent();
$file
->save();
}
}