class FileUploadForm in Media Directories 3.x
Same name and namespace in other branches
- 8 modules/media_directories_ui/src/Form/FileUploadForm.php \Drupal\media_directories_ui\Form\FileUploadForm
- 2.x modules/media_directories_ui/src/Form/FileUploadForm.php \Drupal\media_directories_ui\Form\FileUploadForm
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\media_library\Form\AddFormBase implements BaseFormIdInterface, TrustedCallbackInterface
- class \Drupal\media_directories_ui\Form\AddMediaFormBase
- class \Drupal\media_directories_ui\Form\FileUploadForm
- class \Drupal\media_directories_ui\Form\AddMediaFormBase
- class \Drupal\media_library\Form\AddFormBase implements BaseFormIdInterface, TrustedCallbackInterface
Expanded class hierarchy of FileUploadForm
1 file declares its use of FileUploadForm
- MediaDirectoriesLibraryUiBuilder.php in modules/
media_directories_ui/ src/ MediaDirectoriesLibraryUiBuilder.php
File
- modules/
media_directories_ui/ src/ Form/ FileUploadForm.php, line 11
Namespace
Drupal\media_directories_ui\FormView source
class FileUploadForm extends AddMediaFormBase {
protected function buildInputElement(array $form, FormStateInterface $form_state) {
$max_filesize = \Drupal\Component\Utility\Environment::getUploadMaxSize();
$media_type = $this
->getMediaType($form_state);
$process = (array) \Drupal::service('element_info')
->getInfoProperty('managed_file', '#process', []);
$source_field = $media_type
->getSource()
->getConfiguration()['source_field'];
$field_config = $this->entityTypeManager
->getStorage('field_config')
->load('media.' . $media_type
->id() . '.' . $source_field);
$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' => [
'file_validate_extensions' => [
$field_config
->getSetting('file_extensions'),
],
],
'#multiple' => TRUE,
'#upload_location' => 'public://media-directories/',
'#process' => array_merge([
'::validateUploadElement',
], $process, [
'::processUploadElement',
]),
];
return $form;
}
/**
* Validates the upload element.
*
* @param array $element
* The upload element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return array
* The processed upload element.
*/
public function validateUploadElement(array $element, FormStateInterface $form_state) {
/* if ($form_state::hasAnyErrors()) {
// When an error occurs during uploading files, remove all files so the
// user can re-upload the files.
$element['#value'] = [];
}
$values = $form_state->getValue('upload', []);
if (count($values['fids']) > $element['#cardinality'] && $element['#cardinality'] !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
$form_state->setError($element, $this->t('A maximum of @count files can be uploaded.', [
'@count' => $element['#cardinality'],
]));
$form_state->setValue('upload', []);
$element['#value'] = [];
}*/
return $element;
}
/**
* Processes an upload (managed_file) element.
*
* @param array $element
* The upload element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return array
* The processed upload element.
*/
public function processUploadElement(array $element, FormStateInterface $form_state) {
$element['upload_button']['#submit'] = [
'::uploadButtonSubmit',
];
// Limit the validation errors to make sure
// FormValidator::handleErrorsWithLimitedValidation doesn't remove the
// current selection from the form state.
// @see Drupal\Core\Form\FormValidator::handleErrorsWithLimitedValidation()
$element['upload_button']['#limit_validation_errors'] = [
[
'upload',
],
[
'current_selection',
],
];
$element['upload_button']['#ajax'] = [
'callback' => '::updateFormCallback',
'wrapper' => 'media-library-wrapper',
];
return $element;
}
/**
* Submit handler for the upload button, inside the managed_file element.
*
* @param array $form
* The form render array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function uploadButtonSubmit(array $form, FormStateInterface $form_state) {
$files = $this->entityTypeManager
->getStorage('file')
->loadMultiple($form_state
->getValue('upload', []));
$this
->processInputValues($files, $form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function prepareMediaEntityForSave(MediaInterface $media) {
/** @var \Drupal\file\FileInterface $file */
$file = $media
->get($this
->getSourceFieldName($media->bundle->entity))->entity;
$file
->setPermanent();
$file
->save();
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddFormBase:: |
protected | property | The entity type manager. | |
AddFormBase:: |
protected | property | The media library UI builder. | |
AddFormBase:: |
protected | property | The type of media items being created by this form. | |
AddFormBase:: |
protected | property | The opener resolver. | |
AddFormBase:: |
protected | property | The media view builder. | |
AddFormBase:: |
protected | function | Returns an array of supported actions for the form. | |
AddFormBase:: |
protected | function | Returns a render array containing the current selection. | |
AddFormBase:: |
protected | function | Builds the sub-form for setting required fields on a new media item. | 1 |
AddFormBase:: |
protected | function | Build the render array of the media library UI. | |
AddFormBase:: |
protected | function | Returns a render array for a single pre-selected media item. | |
AddFormBase:: |
protected | function | Get all added media items from the form state. | |
AddFormBase:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
|
AddFormBase:: |
protected | function | Get all pre-selected and added media items from the form state. | |
AddFormBase:: |
protected | function | Get the media type from the form state. | 2 |
AddFormBase:: |
protected | function | Get all pre-selected media items from the form state. | |
AddFormBase:: |
protected | function | Returns the name of the source field for a media type. | |
AddFormBase:: |
protected | function | Determines if the "advanced UI" of the Media Library is enabled. | |
AddFormBase:: |
public | function | Converts the set of newly added media into an item list for rendering. | |
AddFormBase:: |
public | function | Submit handler for the remove button. | 1 |
AddFormBase:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
AddFormBase:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
|
AddFormBase:: |
public | function | AJAX callback to send the new media item(s) to the media library. | |
AddFormBase:: |
public | function | AJAX callback to send the new media item(s) to the calling code. | |
AddFormBase:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
AddFormBase:: |
protected | function | Validate a created media item. | |
AddFormBase:: |
public | function | Constructs an AddFormBase object. | 2 |
AddMediaFormBase:: |
public | function | Go back to the library view. | |
AddMediaFormBase:: |
public | function |
Form constructor. Overrides AddFormBase:: |
|
AddMediaFormBase:: |
protected | function | ||
AddMediaFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides AddFormBase:: |
1 |
AddMediaFormBase:: |
protected | function |
Creates a new, unsaved media item from a source field value. Overrides AddFormBase:: |
|
AddMediaFormBase:: |
protected | function | ||
AddMediaFormBase:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
AddMediaFormBase:: |
protected | function |
Get the media library state from the form state. Overrides AddFormBase:: |
|
AddMediaFormBase:: |
protected | function |
Creates media items from source field input values. Overrides AddFormBase:: |
|
AddMediaFormBase:: |
public | function |
AJAX callback to update the entire form based on source field input. Overrides AddFormBase:: |
|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FileUploadForm:: |
protected | function |
Builds the element for submitting source field value(s). Overrides AddFormBase:: |
|
FileUploadForm:: |
protected | function |
Prepares a created media item to be permanently saved. Overrides AddFormBase:: |
|
FileUploadForm:: |
public | function | Processes an upload (managed_file) element. | |
FileUploadForm:: |
public | function | Submit handler for the upload button, inside the managed_file element. | |
FileUploadForm:: |
public | function | Validates the upload element. | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. |