Startseite
  • » Home
  • » Handbuch & FAQ
  • » Forum
  • » Übersetzungsserver
  • » Suche
Startseite › Forum › Drupalcenter.de › Module › Modul-Entwicklung ›

Drupal 8 Plupload Datei hochladen

Eingetragen von congomonster (24)
am 12.08.2016 - 21:00 Uhr in
  • Modul-Entwicklung
  • Drupal 8.x

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.

‹ XML erstellen von hochgeladenen Dateien D8 - Xing Auth und Datenabfrage ›
  • Anmelden oder Registrieren um Kommentare zu schreiben

Upload geschafft

Eingetragen von congomonster (24)
am 15.08.2016 - 17: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');
    }
  }

}

?>

  • Anmelden oder Registrieren um Kommentare zu schreiben

Benutzeranmeldung

  • Registrieren
  • Neues Passwort anfordern

Aktive Forenthemen

  • Best venues and rates for jet ski and aqua bike rental options in Tenerife Sur
  • Drupal CMS installieren
  • [erledigt]MP3 in Drupal 10 einbinden
  • (gelöst)Drupal 11 installieren
  • Titel ausblenden
  • Ich brauche dringen Hilfe zu Updates oder ggf. wwie geht Composer?
  • Dynamische Ansicht von Seiteninhalt (als Tabelle?)
  • Vergabe von Berechtigungen für bestimmte Rollen; mir fehlt der Haken bzw. das „Veröffentlicht“
  • Medien und andere Daten mit Feeds von Drupal 7 auf Drupal 10 migrieren
  • Rolle erstellen nicht zu finden
  • für drupal11 ein Slider Modul
  • [gelöst] W3CSS Paragraphs Views
Weiter

Neue Kommentare

  • Verwende doch das Tag dafür,
    vor 5 Tagen 8 Stunden
  • Guckst du hier: step by step
    vor 4 Tagen 22 Stunden
  • Guckst du hier: step by step
    vor 4 Tagen 22 Stunden
  • Ich habe ja keine Angst vor
    vor 2 Wochen 7 Stunden
  • Ist grundsätzlichmachbar – aber nichts für „einfach mal schnell“
    vor 2 Wochen 2 Tagen
  • Vielen Dank erst einmal, aber
    vor 2 Wochen 4 Tagen
  • Du hast die "Trusted host
    vor 2 Wochen 4 Tagen
  • Bitte genauer den aktuellen Lösungs-Ansatz beschreiben
    vor 4 Wochen 3 Tagen
  • Git und rsync sind die wichtigsten Werkzeuge
    vor 5 Wochen 2 Stunden
  • Arrrrg. Nix Tabelle :-D /*
    vor 9 Wochen 2 Tagen

Statistik

Beiträge im Forum: 250285
Registrierte User: 20499

Neue User:

  • Rickienurce
  • Inga GuAph
  • Robertolix

» Alle User anzeigen

User nach Punkten sortiert:
wla9464
stBorchert6003
quiptime4972
Tobias Bähr4019
bv3924
ronald3857
md3717
Thoor3678
Alexander Langer3416
Exterior2903
» User nach Punkten
Zur Zeit sind 0 User und 12 Gäste online.

Hauptmenü

  • » Home
  • » Handbuch & FAQ
  • » Forum
  • » Übersetzungsserver
  • » Suche

Quicklinks I

  • Infos
  • Drupal Showcase
  • Installation
  • Update
  • Forum
  • Team
  • Verhaltensregeln

Quicklinks II

  • Drupal Jobs
  • FAQ
  • Drupal-Kochbuch
  • Best Practice - Drupal Sites - Guidelines
  • Drupal How To's

Quicklinks III

  • Tipps & Tricks
  • Drupal Theme System
  • Theme Handbuch
  • Leitfaden zur Entwicklung von Modulen

RSS & Twitter

  • Drupal Planet deutsch
  • RSS Feed News
  • RSS Feed Planet
  • Twitter Drupalcenter
Drupalcenter Team | Impressum & Datenschutz | Kontakt
Angetrieben von Drupal | Drupal is a registered trademark of Dries Buytaert.
Drupal Initiative - Drupal Association