class FileTestForm
Same name and namespace in other branches
- 9 core/modules/file/tests/file_test/src/Form/FileTestForm.php \Drupal\file_test\Form\FileTestForm
- 8.9.x core/modules/file/tests/file_test/src/Form/FileTestForm.php \Drupal\file_test\Form\FileTestForm
- 10 core/modules/file/tests/file_test/src/Form/FileTestForm.php \Drupal\file_test\Form\FileTestForm
File test form class.
Hierarchy
- class \Drupal\Core\Form\FormBase extends \Drupal\Core\Form\FormInterface, \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\file_test\Form\FileTestForm uses \Drupal\file_test\Form\FileTestFormTrait, \Drupal\Core\StringTranslation\StringTranslationTrait implements \Drupal\Core\Form\FormBase
Expanded class hierarchy of FileTestForm
1 string reference to 'FileTestForm'
- file_test.routing.yml in core/
modules/ file/ tests/ file_test/ file_test.routing.yml - core/modules/file/tests/file_test/file_test.routing.yml
File
-
core/
modules/ file/ tests/ file_test/ src/ Form/ FileTestForm.php, line 16
Namespace
Drupal\file_test\FormView source
class FileTestForm extends FormBase {
use FileTestFormTrait;
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getFormId() : string {
return '_file_test_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) : array {
$form = $this->baseForm($form, $form_state);
$form['file_test_upload'] = [
'#type' => 'file',
'#title' => $this->t('Upload a file'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) : void {
// Process the upload and perform validation. Note: we're using the
// form value for the $replace parameter.
if (!$form_state->isValueEmpty('file_subdir')) {
$destination = 'temporary://' . $form_state->getValue('file_subdir');
\Drupal::service('file_system')->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY);
}
else {
$destination = FALSE;
}
// Setup validators.
$validators = [];
if ($form_state->getValue('is_image_file')) {
$validators['FileIsImage'] = [];
}
$allow = $form_state->getValue('allow_all_extensions');
if ($allow === 'empty_array') {
$validators['FileExtension'] = [];
}
elseif ($allow === 'empty_string') {
$validators['FileExtension'] = [
'extensions' => '',
];
}
elseif (!$form_state->isValueEmpty('extensions')) {
$validators['FileExtension'] = [
'extensions' => $form_state->getValue('extensions'),
];
}
// The test for \Drupal::service('file_system')->moveUploadedFile()
// triggering a warning is unavoidable. We're interested in what happens
// afterwards in file_save_upload().
if (\Drupal::state()->get('file_test.disable_error_collection')) {
define('SIMPLETEST_COLLECT_ERRORS', FALSE);
}
$file = file_save_upload('file_test_upload', $validators, $destination, 0, static::fileExistsFromName($form_state->getValue('file_test_replace')));
if ($file) {
$form_state->setValue('file_test_upload', $file);
\Drupal::messenger()->addStatus($this->t('File @filepath was uploaded.', [
'@filepath' => $file->getFileUri(),
]));
\Drupal::messenger()->addStatus($this->t('File name is @filename.', [
'@filename' => $file->getFilename(),
]));
\Drupal::messenger()->addStatus($this->t('File MIME type is @mimetype.', [
'@mimetype' => $file->getMimeType(),
]));
\Drupal::messenger()->addStatus($this->t('You WIN!'));
}
elseif ($file === FALSE) {
\Drupal::messenger()->addError($this->t('Epic upload FAIL!'));
}
}
/**
* Get a FileExists enum from its name.
*/
protected static function fileExistsFromName(string $name) : FileExists {
return match ($name) { FileExists::Replace->name => FileExists::Replace,
FileExists::Error->name => FileExists::Error,
default => FileExists::Rename,
};
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
DependencySerializationTrait::$_entityStorages | protected | property | An array of entity type IDs keyed by the property name of their storages. | ||
DependencySerializationTrait::$_serviceIds | protected | property | An array of service IDs keyed by property name used for serialization. | ||
DependencySerializationTrait::__sleep | public | function | 3 | ||
DependencySerializationTrait::__wakeup | public | function | 3 | ||
FileTestForm::buildForm | public | function | Form constructor. | Overrides FormInterface::buildForm | 1 |
FileTestForm::fileExistsFromName | protected static | function | Get a FileExists enum from its name. | ||
FileTestForm::getFormId | public | function | Returns a unique string identifying the form. | Overrides FormInterface::getFormId | 1 |
FileTestForm::submitForm | public | function | Form submission handler. | Overrides FormInterface::submitForm | |
FileTestFormTrait::baseForm | public | function | Adds common form elements to the form. | ||
FormBase::$configFactory | protected | property | The config factory. | 2 | |
FormBase::$elementInfoManager | protected | property | The element info manager. | ||
FormBase::$requestStack | protected | property | The request stack. | 1 | |
FormBase::$routeMatch | protected | property | The route match. | ||
FormBase::config | protected | function | Retrieves a configuration object. | ||
FormBase::configFactory | protected | function | Gets the config factory for this form. | 2 | |
FormBase::container | private | function | Returns the service container. | ||
FormBase::create | public static | function | Instantiates a new instance of this class. | Overrides ContainerInjectionInterface::create | 111 |
FormBase::currentUser | protected | function | Gets the current user. | 2 | |
FormBase::elementInfoManager | protected | function | The element info manager. | ||
FormBase::getRequest | protected | function | Gets the request object. | ||
FormBase::getRouteMatch | protected | function | Gets the route match. | ||
FormBase::logger | protected | function | Gets the logger for a specific channel. | ||
FormBase::redirect | protected | function | Returns a redirect response object for the specified route. | ||
FormBase::resetConfigFactory | public | function | Resets the configuration factory. | ||
FormBase::setConfigFactory | public | function | Sets the config factory for this form. | ||
FormBase::setElementInfoManager | public | function | Sets the element info manager for this form. | ||
FormBase::setRequestStack | public | function | Sets the request stack object to use. | ||
FormBase::validateForm | public | function | Form validation handler. | Overrides FormInterface::validateForm | 60 |
LoggerChannelTrait::$loggerFactory | protected | property | The logger channel factory service. | ||
LoggerChannelTrait::getLogger | protected | function | Gets the logger for a specific channel. | ||
LoggerChannelTrait::setLoggerFactory | public | function | Injects the logger channel factory. | ||
MessengerTrait::$messenger | protected | property | The messenger. | 25 | |
MessengerTrait::messenger | public | function | Gets the messenger. | 25 | |
MessengerTrait::setMessenger | public | function | Sets the messenger. | ||
RedirectDestinationTrait::$redirectDestination | protected | property | The redirect destination service. | 2 | |
RedirectDestinationTrait::getDestinationArray | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | ||
RedirectDestinationTrait::getRedirectDestination | protected | function | Returns the redirect destination service. | ||
RedirectDestinationTrait::setRedirectDestination | public | function | Sets the redirect destination service. | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | 1 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.