ProcessWire - Konfigurierbare Module: Unterschied zwischen den Versionen
| Zeile 75: | Zeile 75: | ||
== Tipps für das Konfigurieren von Modulen == | == Tipps für das Konfigurieren von Modulen == | ||
Es gibt verschiedene Möglichkeiten Module zu konfigurieren. Außerdem ältere Funktionen die manchmal eingesetzt werden. | Es gibt verschiedene Möglichkeiten Module zu konfigurieren. Außerdem ältere Funktionen die manchmal eingesetzt werden. | ||
| + | |||
| + | === Old School Storing === | ||
| + | In alten Versionen von ProcessWire hat man es so gemacht: | ||
| + | <syntaxhighlight lang="php"> | ||
| + | class MyFooBar implements Module, ConfigurableModule { | ||
| + | |||
| + | public static function getModuleInfo() { /* you know what's here */ } | ||
| + | |||
| + | public function __construct() { | ||
| + | // set your default values for config variables | ||
| + | $this->set('foo', ''); | ||
| + | $this->set('bar', 0); | ||
| + | } | ||
| + | |||
| + | public function init() { | ||
| + | // note that PW populates the actual values for your config vars, 'foo' and 'bar' | ||
| + | // before this init() function is called, but after __construct is called. That's why | ||
| + | // we set the defaults in __construct. Once you get to this init(), your config is | ||
| + | // now populated, and likewise available for this non-static function below: | ||
| + | } | ||
| + | |||
| + | public function getConfig() { | ||
| + | // you can name this function whatever you want, since it's not part of any interface | ||
| + | // this is the stuff you would usually have in your static getModuleConfigInputfields | ||
| + | // see how we can pull the values from $this->foo rather than $data['foo']; | ||
| + | $inputfields = new InputfieldWrapper(); | ||
| + | |||
| + | $f = $this->modules->get('InputfieldText'); | ||
| + | $f->attr('name', 'foo'); | ||
| + | $f->attr('value', $this->foo); | ||
| + | $f->label = 'Foo'; | ||
| + | $inputfields->add($f); | ||
| + | |||
| + | $f = $this->modules->get('InputfieldInteger'); | ||
| + | $f->attr('name', 'bar'); | ||
| + | $f->attr('value', $this->bar); | ||
| + | $f->label = 'Bar'; | ||
| + | $inputfields->add($f); | ||
| + | |||
| + | return $inputfields; | ||
| + | } | ||
| + | |||
| + | public static function getModuleConfigInputfields(array $data) { | ||
| + | // note we don't actually need $data in this pattern | ||
| + | $module = wire('modules')->get('MyFooBar'); | ||
| + | return $module->getConfig(); | ||
| + | } | ||
| + | } | ||
| + | </php> | ||
=== Die Funktion getModuleConfigInputfields() === | === Die Funktion getModuleConfigInputfields() === | ||
Version vom 24. Juni 2024, 10:28 Uhr
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/ https://processwire.com/api/ref/modules/save-config/
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;
}
}
Tipps für das Konfigurieren von Modulen
Es gibt verschiedene Möglichkeiten Module zu konfigurieren. Außerdem ältere Funktionen die manchmal eingesetzt werden.
Old School Storing
In alten Versionen von ProcessWire hat man es so gemacht: <syntaxhighlight lang="php"> class MyFooBar implements Module, ConfigurableModule {
public static function getModuleInfo() { /* you know what's here */ }
public function __construct() {
// set your default values for config variables
$this->set('foo', );
$this->set('bar', 0);
}
public function init() {
// note that PW populates the actual values for your config vars, 'foo' and 'bar'
// before this init() function is called, but after __construct is called. That's why
// we set the defaults in __construct. Once you get to this init(), your config is
// now populated, and likewise available for this non-static function below:
}
public function getConfig() {
// you can name this function whatever you want, since it's not part of any interface
// this is the stuff you would usually have in your static getModuleConfigInputfields
// see how we can pull the values from $this->foo rather than $data['foo'];
$inputfields = new InputfieldWrapper();
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'foo');
$f->attr('value', $this->foo);
$f->label = 'Foo';
$inputfields->add($f);
$f = $this->modules->get('InputfieldInteger');
$f->attr('name', 'bar');
$f->attr('value', $this->bar);
$f->label = 'Bar';
$inputfields->add($f);
return $inputfields; }
public static function getModuleConfigInputfields(array $data) {
// note we don't actually need $data in this pattern
$module = wire('modules')->get('MyFooBar');
return $module->getConfig();
}
} </php>
Die Funktion getModuleConfigInputfields()
https://processwire.com/talk/topic/29098-how-should-getmoduleconfiginputfields-be-declared/
Should getModuleConfigInputfields() be declared in a module as static or not? Also, I am confused that som modules use an array as the parameter whereas others use an InputfieldWrapper.
For example, WireMailSmtp has
static public function getModuleConfigInputfields(array $data)
whereas WireMailRouter has
public function getModuleConfigInputfields(InputfieldWrapper $inputfields)
I am trying to build a module that allows certain configs to be set outside the modules themselves (to allow non-superuser access), so I need to call the getModuleConfigInputfields() method, but without knowing what parameter to supply. If it is an array, I can use getConfig(), but otherwise what. And why do different modules do it differently anyway?
Ryans Antwort:
The Modules class supports a lot of different options for this, and it figures out which you are using automatically according to how you've defined it. The one that I prefer to use is your second example, that is not static and has the InputfieldWrapper $inputfields as the argument.
The static method is the old/original way from when ProcessWire first was released (13 years ago), and it was used primarily so that one could configure a module without booting up an instance of the module. Later on (like ~8 years ago?), the Modules class was upgraded so that it could instantiate the module without initializing it, which made the static option obsolete. The only reason ProcessWire still recognizes it is for backwards compatibility, or if it happens to be preferred by the module developer for one reason or another.