Ergebnis 1 bis 11 von 11

Thema: Klasse in Contao 3 überschreiben

  1. #1
    Contao-Nutzer
    Registriert seit
    29.01.2013.
    Beiträge
    157

    Standard Klasse in Contao 3 überschreiben

    Hallo,

    ich verzweifle gerade etwas am Namespace-System von Contao 3.1. Und zwar möchte ich die FormSubmit.php aus dem core überschreiben. Da dachte ich mir, ich nutze mal die neue Namespace-Funktionalität und habe eine Erweiterung angelegt mit folgenden Dateien:

    config/autoload.ini
    config/autoload.php
    FormSubmit.php

    In autload.php gebe ich Contao einfach die andere Klasse mit:
    Code:
    <?php
    
    ClassLoader::addClasses(array
    (
    	// Forms
    	'Contao\FormSubmit'                => 'system/modules/xmeinmodul/FormSubmit.php',
    ));
    Und FormSubmit.php sieht so aus:
    Code:
    <?php
    
    namespace MyNamespace;
    use \Contao\FormSubmit as FormSubmitOld;
    
    class FormSubmit extends FormSubmitOld {
    
    	public function generate()
    	{
    		if ($this->imageSubmit)
    		{
    			// Check for version 3 format
    			if ($this->singleSRC != '' && !is_numeric($this->singleSRC))
    			{
    				return '<p class="error">'.$GLOBALS['TL_LANG']['ERR']['version2format'].'</p>';
    			}
    
    			$objModel = \FilesModel::findByPk($this->singleSRC);
    
    			if ($objModel !== null && is_file(TL_ROOT . '/' . $objModel->path))
    			{
    				return sprintf('<input type="image" src="%s" id="ctrl_%s" class="submit%s" title="%s" alt="%s"%s%s',
    								$objModel->path,
    								$this->strId,
    								(($this->strClass != '') ? ' ' . $this->strClass : ''),
    								specialchars($this->slabel),
    								specialchars($this->slabel),
    								$this->getAttributes(),
    								$this->strTagEnding);
    			}
    		}
    
    		// Return the regular button
    		return sprintf('<button type="submit" id="ctrl_%s" class="submit%s"%s>%s</button>',
    						$this->strId,
    						(($this->strClass != '') ? ' ' . $this->strClass : ''),
    						$this->getAttributes(),
    						specialchars($this->slabel));
    	}
    }
    Wenn ich ausführe, bekomme ich einen Fatal error:
    Code:
    Fatal error: Class 'Contao\FormSubmit' not found in <...>\system\modules\xmeinmodul\FormSubmit.php on line 15
    Bin etwas ratlos und hoffe, es kann jemand Licht ins Dunkel bringen

    Ciao The_Unknown

  2. #2
    Wandelndes Contao-Lexikon Avatar von BugBuster
    Registriert seit
    15.06.2009.
    Ort
    Berlin
    Beiträge
    10.512
    User beschenken
    Wunschliste

    Standard

    Lass dir die autoload.php generieren übers Backend, dann wirst du den Fehler sehen.
    Grüße, BugBuster
    "view source" is your guide.
    Danke an alle Amazon Wunschlisten Erfüller

  3. #3
    Contao-Nutzer
    Registriert seit
    29.01.2013.
    Beiträge
    157

    Standard

    OK, er hat mir jetzt das hier generiert und es funktioniert tatsächlich.
    Code:
    <?php
    
    /**
     * Contao Open Source CMS
     *
     * Copyright (c) 2005-2013 Leo Feyer
     *
     * @package Xmrl
     * @link    https://contao.org
     * @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
     */
    
    
    /**
     * Register the namespaces
     */
    ClassLoader::addNamespaces(array
    (
    	'MyNamespace',
    ));
    
    
    /**
     * Register the classes
     */
    ClassLoader::addClasses(array
    (
    	'MyNamespace\FormSubmit' => 'system/modules/meinmodul/FormSubmit.php',
    ));
    Jetzt habe ich eine Klasse in einem eigenen Namespace registriert, ok. Aber woher weiß Contao nun, dass es diese anstatt der eigenen Klasse nehmen soll? Die Klassen liegen doch in völlig verschiedenen Namespaces?

  4. #4
    Wandelndes Contao-Lexikon Avatar von BugBuster
    Registriert seit
    15.06.2009.
    Ort
    Berlin
    Beiträge
    10.512
    User beschenken
    Wunschliste

    Standard

    Nun, das ist so in Contao, grob gesagt, er nimmt den ersten Teil weg und registriert alles was danach kommt im globalen Namespace.
    Und, er registriert erst die Core Module und dann die externen Erweiterungen.

    Würde bei dir nun heißen:
    • es greift jemand zu über \FormSubmit : es würde deine Klasse genommen werden, da deine die letzte ist die unter diesen Namen registriert ist.
    • es greift jemand zu über \Contao\FormSubmit : es würde die originale Klasse von Contao verwendet werden.
    Grüße, BugBuster
    "view source" is your guide.
    Danke an alle Amazon Wunschlisten Erfüller

  5. #5
    Contao-Nutzer
    Registriert seit
    29.01.2013.
    Beiträge
    157

    Standard

    Aah, ok. Sehr gut erklärt, vielen Dank!

  6. #6
    Contao-Nutzer
    Registriert seit
    03.07.2009.
    Beiträge
    46

    Standard

    Is this still the way to override classes in contao4?

  7. #7
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    34.088
    Partner-ID
    10107

    Standard

    Overriding classes is deprecated. It still works in Contao 4 for some cases but not all. What exactly do you want to do?

  8. #8
    Contao-Nutzer
    Registriert seit
    03.07.2009.
    Beiträge
    46

    Standard

    Hi Spooky, thank you for the info. I was wondering to what extend it is supported in Contao4 because in Contao3, if I remember it correctly, it was seen as a feature to able to override core classes, back then.
    I have an old custom module which overrides contao Search class (Search.php), to eliminate some special chars, words, and char length < 3 filling up DB. I know there is a HOOK `indexPageHook` which due to its position didn't gave me a full advantage.
    Anyway now I'm using this hook to achieve the same result in my Contao 4 module, but I've to repeat some part of logic from core class inside my hook.

  9. #9
    Contao-Nutzer
    Registriert seit
    19.02.2017.
    Beiträge
    10

    Standard

    Zitat Zitat von Spooky Beitrag anzeigen
    Overriding classes is deprecated. It still works in Contao 4 for some cases but not all.
    Do you mean this way is deprecated?

    PHP-Code:
    $GLOBALS['TL_MODEL']['tl_form_field'] = \App\Model\MyCustomModel::class; 

  10. #10
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    34.088
    Partner-ID
    10107

    Standard

    That's still supported (though there may be some caveats). What's not supported is overriding classes via the root namespace.

  11. #11
    Contao-Nutzer
    Registriert seit
    19.02.2017.
    Beiträge
    10

    Standard

    Zitat Zitat von Spooky Beitrag anzeigen
    That's still supported (though there may be some caveats). What's not supported is overriding classes via the root namespace.
    In that case, my example would have no point, because instantiating FormFieldModel is hardcoded in \Contao\Form. I guess that's the caveat... Or is it a bug?

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
  •