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>
name: Plugin Name
settings:
dynamic_setting:
default: 0
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 );
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;
}
}
@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