Skip to content

Instantly share code, notes, and snippets.

@rayners
Created May 17, 2010 13:56
Show Gist options
  • Save rayners/403783 to your computer and use it in GitHub Desktop.
Save rayners/403783 to your computer and use it in GitHub Desktop.
Example of a dynamic plugin setting
<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>
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
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 );
}
}
@padawan
Copy link

padawan commented May 21, 2010

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:

  • at plugin init or load_config:
  • get the list of CF keys for this blog
  • get existing config values for this blog
  • update the list of config keys to match the list of CF keys (add new ones when a CF has been added, remove those of CFs that have been deleted since the last launch)
  • at plugin normal run:
  • expose those settings through the plugin settings page via mtapp:setting blocks for editing values
  • use those values to perform certain actions or control on various edit screens (think of a length limit for text fields for example)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment