Drupal 8 Plupload Datei hochladen
Eingetragen von congomonster (24)
am 12.08.2016 - 20:00 Uhr in
am 12.08.2016 - 20:00 Uhr in
Hallo,
ich habe mir ein eigenes Modul erstellt bzw. einen Block. Weiterhin habe ich mir eine Form erstellt und Plupload eingebunden.
Der "normale" Dateiupload über managed File klappt wunderbar. Nur über Plupload nicht. Ich hab jetzt schon viel gesucht.
Auf den englischsprachigen Webseiten wird gesagt, plupload legt die Dateien in den tmp Ordner. Wo soll denn der sein?
Ich habe sowas gar nicht. Habe mal einen tmp Ordner neben dem Core Ordner esrtellt, aber da tut sich nix.
Hier mal meine Form:
<?php
/**
* @file
* Contains \Drupal\example\Form\ExampleForm.
*/
namespace Drupal\service_bildumwandlung_s_w\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements an example form.
*/
class service_bildumwandlung_s_wForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'service_bildumwandlung_s_w_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// email adress
$form['customer_email'] = array(
'#type' => 'email',
'#title' => $this->t('Ihre E-Mail Adresse:'),
'#required' => true,
);
//file upload
$form['customer_files'] = array(
//'#type' => 'managed_file',
'#type' => 'plupload',
'#required' => true,
'#name' => 'customer_files',
'#title' => t('Ihre Datei(en):'),
'#description' => t("Hier können Sie Ihre Daten hochladen!"),
// plupload settings
'#upload_validators' => array(
'file_validate_extensions' => array(
'jpg jpeg gif png tif tiff'),
'my_custom_file_validator' => array('some validation criteria'),
),
'#plupload_settings' => array(
'runtimes' => 'html5',
'chunk_size' => '1mb',
),
'#upload_location' => 'public://service/service1_bildumwandlung_sw'
);
// submit button
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save'),
'#button_type' => 'primary',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (strlen($form_state->getValue('customer_email')) == 3) {
$form_state->setErrorByName('customer_email', $this->t('The phone number is too short. Please enter a full phone number.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Upload files.
foreach ($element['customer_files']['#value'] as $uploaded_file) {
$source = $uploaded_file['tmppath'];
$filename = filefield_sources_clean_filename($uploaded_file['name'], array());
// Move the file to a temporary destination using final base file name.
$temp_destination = file_stream_wrapper_uri_normalize('temporary://' . $filename);
$temp_filepath = file_unmanaged_move($source, $temp_destination, FILE_EXISTS_RENAME);
// Save the files to their final destination.
if ($file = filefield_sources_save_file($temp_filepath, array(), 'public://service/service1_bildumwandlung_sw/' . $form['nid']['#value'] . '/')) {
$file->oid = $element['oid']['#value'];
// Set status to 1, meaning this is a permanent file and should not be
// cleaned up upon cron run. IMPORTANT!
$file->status = 1;
file_save($file);
}
else {
drupal_set_message(t('An error occurred during upload. Please try again.'));
}
}
drupal_set_message($this->t('Ihre Daten werden innerhalb von 15 Minuten an @email zurück gesendet.', array('@email' => $form_state->getValue('customer_email'))));
//$form_state->setRedirect('<front>');
}
// end class
}
?>
Ist auch mein erstes Modul. Ich habe bei Drupal direkt geschaut und bin den Beispielen gefolgt.
- Anmelden oder Registrieren um Kommentare zu schreiben
Upload geschafft
am 15.08.2016 - 16:00 Uhr
Im Modul selbst gibt es eine Beispiel Form :) Damit klappt der Upload dann auch.
<?php
namespace Drupal\plupload_test;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Plupload test form class.
*/
class PluploadTestForm implements FormInterface {
/**
* {@inheritdoc}
*/
public function getFormId() {
return '_plupload_test_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['plupload'] = array(
'#type' => 'plupload',
'#title' => 'Plupload',
'#upload_validators' => array(
'file_validate_extensions' => array('zip'),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 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', t("Upload of %filename failed.", array('%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';
file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$saved_files = array();
foreach ($form_state->getValue('plupload') as $uploaded_file) {
$file_uri = file_stream_wrapper_uri_normalize($destination . '/' . $uploaded_file['name']);
// Move file without creating a new 'file' entity.
file_unmanaged_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_set_message('Files uploaded correctly: ' . implode(', ', $saved_files) . '.', 'status');
}
}
}
?>