Version 1.5.0 of ZedSeries Library features a ConfigWizard class that lets developers quickly implement custom config options for their Contao extensions.
Config options are managed through a section/key/value schema.
In the following example we’ll manage two configuration options for a virtual extension. We’ll choose to assign to this configuration the name ‘dummy_ext’ (it will be our “section” name). Our two configuration options will be named option_string and option_flag.
Setting up the dca
First of all we define our two field in the tl_zedseries_config dca.
Fields must be defined through the standard Contao notation as if they were standard database fields.
The only difference is the key of the “fields” array that must be prefix’ed with the section name: so the entry for the option_string field will be dummy_ext_options_string and the entry for the option_flag will be dummy_ext_option_flag
The option_string will be a TextWidget, the option_flag will be a CheckBoxWidget field.
Create the system/modules/dummy_ext/dca/tl_zedseries_config.php file and put the following lines:
please note that in the example below the labels for each fields are autoloaded from the tl_zedseries_config language file
<?php
$GLOBALS['TL_DCA']['tl_zedseries_config']['fields']['dummy_ext_option_string'] = array('label' = >$GLOBALS['TL_LANG']['tl_zedseries_config']['dummy_ext_option_string'], 'inputType' => 'text', 'eval' => array('mandatory'=>true));
$GLOBALS['TL_DCA']['tl_zedseries_config']['fields']['dummy_ext_option_flag'] = array('label' => &$GLOBALS['TL_LANG']['tl_zedseries_config']['dummy_ext_option_flag'], 'inputType' => 'checkbox');
?>
Setting up the backend menu entry
Add this code to system/modules/dummy_ext/config/config.php:
<?php
array_insert($GLOBALS['BE_MOD']['content'], 9, array( 'CustomConfiguration' =>array( 'callback' => 'DummyExtConfig' ) ) );
?>
then put in system/modules/dummy_ext/DummyExtConfig.php
<?php
class DummyExtConfig extends ZedSeriesConfigWizard {
public function getSection() {
return 'dummy_ext';
}
public function getTitle() {
return 'Configuration for dummy extension';
}
public function getLogo() {
return 'system/modules/dummy_ext/media/images/config_logo.png';
}
}
?>
Retrieve settings
Settings can be retrieved using:
- the ZedSeriesConfig::getValue($section, $k) static method, that returns the value for the specified section/key
- the ZedSeriesConfig::getValues($section) static method that returns an associative array containing the keys and values for the specified section
- the {{zcfg::_section_::_key_}} insert-tags
Conclusion
The above is only a quick example, feel free to comment below for any doubts or questions
No comments:
Post a Comment