ProcessWire - Konfigurierbare Module
Aus Wikizone
Version vom 4. November 2020, 20:57 Uhr von 84.155.186.35 (Diskussion) (Die Seite wurde neu angelegt: „Ab ProcessWire 2.5.5. kann man Module mit Konfigurationsmöglichkeiten einfacher erstellen. Die Konfigurationsoptionen lagert man in eine extra Datei aus. h…“)
Ab ProcessWire 2.5.5. kann man Module mit Konfigurationsmöglichkeiten einfacher erstellen. Die Konfigurationsoptionen lagert man in eine extra Datei aus.
https://processwire.com/blog/posts/new-module-configuration-options/
Beispiel Modul "Test" (gibt eine Meldung im Admin bei jeder Seitenladen aus)
- Datei mit dem Namen und Klasse [Modulname]Config erstellen
- Klasse extends ModulConfig
- Default Werte über getDefaults() Funktion definieren
- Inputfelder über getInputfields() Funktion definieren
Test.php
class Test extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Module Test',
'version' => 1,
'summary' => 'Module for testing/demo purposes.',
'autoload' => 'template=admin',
);
}
public function ready() {
if($this->fullname && !count($this->input->post)) {
$msg = "Hi $this->fullname! ";
$msg .= "Your age: $this->age. ";
$msg .= "Favorite color: $this->color.";
$this->message($msg);
}
}
}
TestConfig.php
class TestConfig extends ModuleConfig {
public function getDefaults() {
return array(
'fullname' => '',
'color' => 'blue',
'age' => 40,
);
}
public function getInputfields() {
$inputfields = parent::getInputfields();
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'fullname');
$f->label = 'Full Name';
$f->required = true;
$inputfields->add($f);
$f = $this->modules->get('InputfieldSelect');
$f->attr('name', 'color');
$f->label = 'Favorite Color';
$f->options = array(
'red' => 'Red',
'green' => 'Green',
'blue' => 'Blue'
);
$inputfields->add($f);
$f = $this->modules->get('InputfieldInteger');
$f->attr('name', 'age');
$f->label = 'Your Age';
$inputfields->add($f);
return $inputfields;
}
}