Created
May 17, 2010 13:56
-
-
Save rayners/403783 to your computer and use it in GitHub Desktop.
Example of a dynamic plugin setting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<mtapp:setting | |
id="dynamic_setting" | |
label="Dynamic Setting"> | |
<input type="text" name="dynamic_setting" value="<mt:var name="dynamic_setting">" /> | |
</mtapp:setting> | |
<mt:loop name="custom_setting_loop"> | |
<mtapp:setting | |
id="$setting_name" | |
label="$setting_name"> | |
<input type="text" name="<mt:var name="setting_name">" value="<mt:var name="setting_value">" /> | |
</mtapp:setting> | |
</mt:loop> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Plugin Name | |
settings: | |
dynamic_setting: | |
scope: blog | |
default: 0 | |
settings_hash: | |
scope: blog | |
init: sub { my $p = shift; require PluginName::Plugin; bless $p, 'PluginName::Plugin', $p; } | |
config_template: config.tmpl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PluginName::Plugin; | |
use strict; | |
use warnings; | |
use base qw( MT::Plugin ); | |
use MT::Util qw(dirify); | |
sub load_config { | |
my $plugin = shift; | |
$plugin->SUPER::load_config(@_); | |
my ( $param, $scope ) = @_; | |
if ( $scope =~ /^blog:(\d+)$/ ) { | |
my $blog_id = $1; | |
# tweak $param as needed for values | |
# to pass to the config template | |
# for example, set dynamic_setting to the current time | |
$param->{dynamic_setting} = time; | |
require MT::Blog; | |
my $blog = MT::Blog->load($blog_id); | |
my $setting_name = dirify( $blog->name ); | |
$param->{custom_setting_loop} = [ | |
{ setting_name => $setting_name, | |
setting_value => | |
$plugin->get_config_value( $setting_name, $scope ) || ${ | |
$plugin->get_config_value( 'settings_hash', $scope ) || {} | |
}{$setting_name} | |
} | |
]; | |
} | |
} | |
sub save_config { | |
my $plugin = shift; | |
my ( $param, $scope ) = @_; | |
if ( $scope =~ /^blog:(\d+)$/ ) { | |
my $blog_id = $1; | |
require MT::Blog; | |
my $blog = MT::Blog->load($blog_id); | |
my $setting_name = dirify( $blog->name ); | |
my $setting_value = $param->{$setting_name}; | |
$param->{settings_hash} = { $setting_name => $setting_value }; | |
} | |
$plugin->SUPER::save_config(@_); | |
if ( $scope =~ /^blog:(\d+)$/ ) { | |
my $blog_id = $1; | |
require MT::Blog; | |
my $blog = MT::Blog->load($blog_id); | |
my $setting_name = dirify( $blog->name ); | |
my $setting_value = $param->{$setting_name}; | |
$plugin->set_config_value( $setting_name, $setting_value, $scope ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this example :-)
Here the setting "dynamic_setting" is declared beforehand in the YAML file. Is it possible to have no such declaration, and have the init procedure (or is that load_config?) declare dynamically a list of settings instead? That's what I meant by dynamic, not the value of the setting, but the list of settings (keys).
In other words, can I use $plugin->set_config_value($key, $value); at will here with keys that I create (from the list of existing Custom Fields keys in this specific case)? Something like: