class PluploadTestForm in Plupload integration 8
Same name and namespace in other branches
- 2.0.x plupload_test/src/PluploadTestForm.php \Drupal\plupload_test\PluploadTestForm
Plupload test form class.
Hierarchy
- class \Drupal\plupload_test\PluploadTestForm implements FormInterface uses StringTranslationTrait
Expanded class hierarchy of PluploadTestForm
1 string reference to 'PluploadTestForm'
- plupload_test.routing.yml in plupload_test/
plupload_test.routing.yml - plupload_test/plupload_test.routing.yml
File
- plupload_test/
src/ PluploadTestForm.php, line 13
Namespace
Drupal\plupload_testView source
class PluploadTestForm implements FormInterface {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getFormId() {
return '_plupload_test_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['plupload'] = [
'#type' => 'plupload',
'#title' => 'Plupload',
'#upload_validators' => [
'file_validate_extensions' => [
'zip',
],
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state
->getValue('plupload') as $uploaded_file) {
if ($uploaded_file['status'] != 'done') {
$form_state
->setErrorByName('plupload', $this
->t("Upload of %filename failed.", [
'%filename' => $uploaded_file['name'],
]));
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Create target directory if necessary.
$destination = \Drupal::config('system.file')
->get('default_scheme') . '://plupload-test';
\Drupal::service('file_system')
->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$saved_files = [];
foreach ($form_state
->getValue('plupload') as $uploaded_file) {
$file_uri = $this
->loadStreamWrapper()
->normalizeUri($destination . '/' . $uploaded_file['name']);
// Move file without creating a new 'file' entity.
\Drupal::service('file_system')
->move($uploaded_file['tmppath'], $file_uri);
// @todo: When https://www.drupal.org/node/2245927 is resolved,
// use a helper to save file to file_managed table
$saved_files[] = $file_uri;
}
if (!empty($saved_files)) {
\Drupal::messenger()
->addStatus('Files uploaded correctly: ' . implode(', ', $saved_files) . '.');
}
}
/**
* Returns the Drupal stream wrapper manager service.
*/
private function loadStreamWrapper() {
return \Drupal::service('stream_wrapper_manager');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PluploadTestForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
PluploadTestForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
PluploadTestForm:: |
private | function | Returns the Drupal stream wrapper manager service. | |
PluploadTestForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
PluploadTestForm:: |
public | function |
Form validation handler. Overrides FormInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
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. |