Handlers

Some configuration directives may have a default value, but a default value that may be dependent upon other variables in the system. Therefore a static default value may not suffice. For example, you may have a configuration directive that is dependent upon where you have Apache installed, and it will formulate a path based on that if a explicit value is not set by an administrator.

To achieve this, you need to use the handler option like so:

config_settings:
    MyLogPath:
        handler: $Example::Example::Plugin::MyLogPath
        path: 1

Then, in your Plugin.pm file you add the following subroutine (comments have been added to the code to help make sense of what is going on):

sub MyLogPath {
    my $mgr = shift; # A reference to MT::ConfigMgr

    # if this method is invoked with the intent to set the value,
    #    go ahead and set the value, then return.
    return $mgr->set_internal( 'MyLogPath', @_ ) if @_;

    # user is attempting to retrieve the value
    my $name = $mgr->get_internal('MyLogPath');

    # if a value has been explicitly set, return it
    return $name if defined $name;

    # Ok, guess what the value should be:
    if ($ENV{HTTPD_HOME}) {
        return $ENV{HTTPD_HOME} . '/logs';
    } else {
        return 'logs/';
    }
}
This page was last updated on 2008-09-17, 14:41.  

Leave a note

Have a question, please use the MT Forums. Notes sumbitted here should pertain to tips & hints regarding documentation. Your note may be removed once it's contents has be integrated into the body of the page.