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 - 20: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 - 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');
    }
  }

}

?>

  • Anmelden oder Registrieren um Kommentare zu schreiben

Benutzeranmeldung

  • Registrieren
  • Neues Passwort anfordern

Aktive Forenthemen

  • für drupal11 ein Slider Modul
  • [gelöst] W3CSS Paragraphs Views
  • Drupal 11 neu aufsetzen und Bereiche aus 10 importieren
  • Wie erlaubt man neuen Benutzern auf die Resetseite zugreifen zu dürfen.
  • [gelöst] Anzeigeformat Text mit Bild in einem Artikel, Drupal 11
  • Social Media Buttons um Insteragram erweitern
  • Nach Installation der neuesten D10-Version kein Zugriff auf Website
  • Composer nach Umzug
  • [gelöst] Taxonomie Begriffe zeigt nicht alle Nodes an
  • Drupal 11 + Experience Builder (Canvas) + Layout Builder
  • Welche KI verwendet ihr?
  • Update Manger läst sich nicht Installieren
Weiter

Neue Kommentare

  • melde mich mal wieder, da ich
    vor 1 Woche 5 Tagen
  • Hey danke
    vor 1 Woche 6 Tagen
  • Update: jetzt gibt's ein
    vor 2 Wochen 4 Stunden
  • Hallo, im Prinzip habe ich
    vor 2 Wochen 4 Tagen
  • Da scheint die Terminologie
    vor 2 Wochen 4 Tagen
  • Kannst doch auch alles direkt
    vor 3 Wochen 1 Tag
  • In der entsprechenden View
    vor 3 Wochen 1 Tag
  • Dazu müsstest Du vermutlich
    vor 3 Wochen 1 Tag
  • gelöst
    vor 5 Wochen 5 Tagen
  • Ja natürlich. Dass ist etwas,
    vor 5 Wochen 6 Tagen

Statistik

Beiträge im Forum: 250233
Registrierte User: 20451

Neue User:

  • ByteScrapers
  • Mroppoofpaync
  • 4aficiona2

» Alle User anzeigen

User nach Punkten sortiert:
wla9461
stBorchert6003
quiptime4972
Tobias Bähr4019
bv3924
ronald3857
md3717
Thoor3678
Alexander Langer3416
Exterior2903
» User nach Punkten
Zur Zeit sind 0 User und 25 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