Contao-Camp 2024
Ergebnis 1 bis 5 von 5

Thema: prepareformdata hook form upload rename

  1. #1
    Contao-Nutzer
    Registriert seit
    30.09.2011.
    Beiträge
    55

    Standard prepareformdata hook form upload rename

    Hallo,

    vielleicht kann mich mal jemand anstuppsen damit ich meinen Fehler finde.

    Ich möchte aus einem Formular welches 3 Upload Felder hat die Upload Daten umbennen Anhand vom Feld Vorname Nachnahme und dem UploadFeld.

    Was habe ich bis jetzt gemacht:

    Ordner erstellt
    system/modules/filerename
    system/modules/filerename/config

    Datei erstellt
    system/modules/filerename/filereame.php

    Code:
    <?php
    use Contao\DataContainer;
    use Contao\File;
    use Contao\StringUtil;
    
    namespace Contao;
    
    class FileRename extends \Backend
    {
        public function prepareFormData(DataContainer $dc)
        {
            // Get the form data
            $formData = $dc->activeRecord->row();
            
            // Get the file object for the upbewerbung field
            $file = FilesModel::findByUuid($formData['upbewerbung']);
            if ($file === null)
            {
                return;
            }
            
            // Set the new file name
            $newFileName = StringUtil::generateAlias($formData['vorname'] . '-' . $formData['nachname'] . '-bewerbung.pdf');
            $file->name = $newFileName;
            $file->save();
            
            // Get the file object for the uplebenslauf field
            $file = FilesModel::findByUuid($formData['uplebenslauf']);
            if ($file === null)
            {
                return;
            }
            
            // Set the new file name
            $newFileName = StringUtil::generateAlias($formData['vorname'] . '-' . $formData['nachname'] . '-lebenslauf.pdf');
            $file->name = $newFileName;
            $file->save();
            
            // Get the file object for the upzeugnis field
            $file = FilesModel::findByUuid($formData['upzeugnis']);
            if ($file === null)
            {
                return;
            }
            
            // Set the new file name
            $newFileName = StringUtil::generateAlias($formData['vorname'] . '-' . $formData['nachname'] . '-zeugnis.pdf');
            $file->name = $newFileName;
            $file->save();
        }
    }
    system/modules/filerename/config/config.php

    Code:
    // Register the classes
    ClassLoader::addClasses(array
    (
        'FileRename' => 'system/modules/filerename/FileRename.php'
    ));
    
    // Register the hooks
    $GLOBALS['TL_HOOKS']['prepareFormData'][] = array('FileRename', 'prepareFormData');
    Prod.Cahce erneuern
    Composer install

    Contao 4.13.x
    PHP 8.0.x


    Ich möchte mich schon einmal bedanken für die hilfe.

  2. #2
    Contao-Nutzer
    Registriert seit
    30.09.2011.
    Beiträge
    55

    Standard

    Ausgabe Composer Install:

    Code:
    $ /opt/plesk/php/8.0/bin/php -q -dmax_execution_time=0 -dmemory_limit=-1 -ddisplay_errors=0 -ddisplay_startup_errors=0 -derror_reporting=0 -dallow_url_fopen=1 -ddisable_functions= -ddate.timezone=UTC /var/www/vhosts/xxxxxxxxxxxxxx/httpdocs/web/contao-manager.phar.php composer install --no-dev --no-progress --no-ansi --no-interaction --optimize-autoloader
    
    Using config.component-dir has been deprecated. Please use extra.contao-component-dir instead.
    Installing dependencies from lock file
    Verifying lock file contents can be installed on current platform.
    Nothing to install, update or remove
    Generating optimized autoload files
    Class Contao\ManagerPlugin\PluginLoader located in ./vendor/contao/manager-plugin/src/Resources/PluginLoader.php does not comply with psr-4 autoloading standard. Skipping.
    108 packages you are using are looking for funding.
    Use the `composer fund` command to find out more!
    contao/manager-plugin: Dumping generated plugins file...
    contao/manager-plugin: ...done dumping generated plugins file
    Please edit your root composer.json and set "post-install-cmd" to "@php vendor/bin/contao-setup --no-ansi" instead of using "ScriptHandler::initializeApplication()".
    > Contao\ManagerBundle\Composer\ScriptHandler::initializeApplication
    Added the /var/www/vhosts/xxxxxxxxx//httpdocs/web/preview.php file.
    Added the /var/www/vhosts/xxxxxxxxx/httpdocs/web/index.php file.
    Added the /var/www/vhosts/xxxxxxxxx/httpdocs/web/app.php file.
    
     Trying to install assets as relative symbolic links.
    
     --- ------------------------------- ------------------ 
          Bundle                          Method / Error    
     --- ------------------------------- ------------------ 
      ?   ContaoCoreBundle                relative symlink  
      ?   RockSolidCustomElementsBundle   relative symlink  
      ?   ContaoFaqBundle                 relative symlink  
      ?   RockSolidColumnsBundle          relative symlink  
      ?   ContaoCommentsBundle            relative symlink  
      ?   ContaoNewsBundle                relative symlink  
      ?   RockSolidFrontendHelperBundle   relative symlink  
      ?   RockSolidMegaMenuBundle         relative symlink  
      ?   RockSolidThemeAssistantBundle   relative symlink  
      ?   ContaoCalendarBundle            relative symlink  
      ?   ContaoNewsletterBundle          relative symlink  
      ?   RockSolidIconPickerBundle       relative symlink  
      ?   RockSolidSliderBundle           relative symlink  
     --- ------------------------------- ------------------ 
    
     [OK] All assets were successfully installed.                                   
    
    
     --- ----------------------------------------------- ---------------------------------------------------------------- 
          Symlink                                         Target / Error                                                  
     --- ----------------------------------------------- ---------------------------------------------------------------- 
      ?   web/files/uploads                               files/uploads                                                   
      ?   web/files/Sportheatcontainer                    files/Sportheatcontainer                                        
      ?   web/files/social-feed                           files/social-feed                                               
      ?   web/files/xxxxxxx                                files/xxxxx                                                  
      ?   web/files/backend                               files/backend                                                   
      ?   web/files/oneo                                  files/oneo                                                      
      ?   web/files/favicon                               files/favicon                                                   
      ?   web/system/modules/m17StickyBEFooter/assets     system/modules/m17StickyBEFooter/assets                         
      ?   web/system/modules/haste/assets                 system/modules/haste/assets                                     
      ?   web/system/modules/dcawizard/assets             system/modules/dcawizard/assets                                 
      ?   web/system/modules/notification_center/assets   system/modules/notification_center/assets                       
      ?   system/themes/flexible                          vendor/contao/core-bundle/src/Resources/contao/themes/flexible  
      ?   web/assets                                      assets                                                          
      ?   web/system/themes                               system/themes                                                   
      ?   system/logs                                     var/logs                                                        
      ?   web/vendor/scrivo/highlight_php/styles          vendor/scrivo/highlight.php/styles                              
     --- ----------------------------------------------- ---------------------------------------------------------------- 
    
    
     // Clearing the cache for the prod environment with debug                      
     // false                                                                       
    
     [OK] Cache for the "prod" environment (debug=false) was successfully cleared.  
    
    
     // Clearing the cache for the dev environment with debug                       
     // true                                                                        
    
     [OK] Cache for the "dev" environment (debug=true) was successfully cleared.    
    
    
     // Warming up the cache for the prod environment with debug                    
     // false                                                                       
    
    // Register the classes
    ClassLoader::addClasses(array
    (
        'FileRename' => 'system/modules/filerename/FileRename.php'
    ));
    
    // Register the hooks
    $GLOBALS['TL_HOOKS']['prepareFormData'][] = array('FileRename', 'prepareFormData');
     [OK] Cache for the "prod" environment (debug=false) was successfully warmed.   
    
    Done! Please open the Contao install tool or run contao:migrate on the command line to make sure the database is up-to-date.
    
    # Process terminated with exit code 0
    # Result: OK

  3. #3
    Contao-Urgestein Avatar von zonky
    Registriert seit
    19.03.2010.
    Ort
    Berlin, Rdf
    Beiträge
    9.714
    User beschenken
    Wunschliste

    Standard

    warum machst Du das nicht so, wie im Handbuch beschrieben?

    https://docs.contao.org/dev/referenc...epareFormData/

    also folgende Datei anlegen src/EventListener/PrepareFormDataListener.php, Inhalt aus Beispiel einfügen + anpassen...

  4. #4
    Contao-Nutzer
    Registriert seit
    30.09.2011.
    Beiträge
    55

    Standard

    Vielen dank Zonky das hatte ich mir auch angesehen.

    Reicht es dann nur die Datei zu erstellen benötige ich dann diese ganze Class Loader sachen nicht nur die Datei in dem von dir genannten Verzeichnis ?

    Und vielleicht was ich in einem anderen Beitrag gefunden habe das man in der composer.json das /src eintragen soll falls er es nicht Automatisch lädt.

    Ich versuche mal mein Glück bin nur kein PHP Coder und muss mir das zusammen Basteln

  5. #5
    Contao-Urgestein Avatar von zonky
    Registriert seit
    19.03.2010.
    Ort
    Berlin, Rdf
    Beiträge
    9.714
    User beschenken
    Wunschliste

    Standard

    Zitat Zitat von Speedygonzalez Beitrag anzeigen
    Reicht es dann nur die Datei zu erstellen
    Im Normalfall, ja - anschließend Cache löschen...

    Ich mache meistens auf Console ein "composer install"

Aktive Benutzer

Aktive Benutzer

Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)

Lesezeichen

Lesezeichen

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •