View source
<?php
namespace Drupal\media_directories_ui\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Utility\Token;
use Drupal\media\Entity\MediaType;
use Drupal\media_directories_ui\MediaDirectoriesUiHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaCombinedUploadForm extends FileUploadForm {
protected $fileSystem;
protected $mediaDirectoriesUiHelper;
public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, Token $token, ThemeManagerInterface $theme_manager, ElementInfoManagerInterface $element_info, FileSystemInterface $file_system, MediaDirectoriesUiHelper $media_directories_ui_helper) {
parent::__construct($entity_type_manager, $current_user, $token, $theme_manager, $element_info);
$this->fileSystem = $file_system;
$this->mediaDirectoriesUiHelper = $media_directories_ui_helper;
}
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'), $container
->get('file_system'), $container
->get('media_directories_ui.helper'));
}
public function getFormId() {
return 'media_directories_combined_upload_form';
}
public function buildInputElement(array $form, FormStateInterface $form_state) {
$target_types = $this
->getTargetBundles($form_state);
$validators_by_media_type = [];
foreach ($target_types as $type) {
$validators_by_media_type[$type] = $this
->getUploadValidators(MediaType::load($type));
}
$pre_render = (array) $this->elementInfo
->getInfoProperty('managed_file', '#pre_render', []);
$form['container']['upload'] = [
'#type' => 'managed_file',
'#title' => $this
->t('Select files'),
'#description' => $this
->t('Allowed file extensions: @extensions', [
'@extensions' => $this->mediaDirectoriesUiHelper
->getValidExtensions($target_types),
]),
'#multiple' => TRUE,
'#upload_location' => 'temporary://',
'#upload_validators' => [
'media_directories_ui_file_validator' => [
$validators_by_media_type,
],
],
'#process' => [
[
'Drupal\\file\\Element\\ManagedFile',
'processManagedFile',
],
'::processUploadElement',
],
'#pre_render' => array_merge($pre_render, [
[
static::class,
'preRenderUploadElement',
],
]),
];
return $form;
}
protected function processInputValues(array $source_field_values, array $form, FormStateInterface $form_state) {
$media = [];
foreach ($source_field_values as $source_field_value) {
$media_type = $this->mediaDirectoriesUiHelper
->getMediaType($source_field_value);
$media_storage = $this->entityTypeManager
->getStorage('media');
$source_field_name = $this
->getSourceFieldName($media_type);
$field_config = $this->entityTypeManager
->getStorage('field_config')
->load('media.' . $media_type
->id() . '.' . $source_field_name);
$destination = $this
->getUploadLocation($field_config
->getSettings());
if ($this->fileSystem
->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY)) {
$source_field_value = file_move($source_field_value, $destination);
}
$media[] = $this
->createMediaFromValue($media_type, $media_storage, $source_field_name, $source_field_value, $form_state);
}
$form_state
->set('media', array_values($media));
$form_state
->setRebuild();
}
public function updateFormCallback(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
$wrapper_id = $triggering_element['#ajax']['wrapper'];
$added_media = $form_state
->get('media');
$response = new AjaxResponse();
if ($form_state::hasAnyErrors()) {
$response
->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $form));
return $response;
}
if (end($triggering_element['#parents']) === 'remove_button') {
if (empty($added_media)) {
$build = [
'#theme' => 'media_directories_add',
'#selected_type' => 'combined_upload',
'#active_directory' => -1,
'#target_bundles' => $this
->getTargetBundles($form_state),
'#media_library_form_rebuild' => TRUE,
];
$form_state
->setRebuild();
$response
->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $build));
}
else {
$response
->addCommand(new ReplaceCommand("#{$wrapper_id}", $form));
}
}
else {
$response
->addCommand(new ReplaceCommand("#{$wrapper_id}", $form));
}
return $response;
}
}