Anything in the registry can be defined statically, or dynamically. This allows for Movable Type to adapt and respond to user defined data and preferences, as well as to content defined by developers.
A good example of this can be found in the list of quick filters found on the Manage Assets screen. There a list of quick filters is defined by scanning the list of all register asset types or "classes" and automatically generating a quick filter for each one.
Static Registry Items
A static registry item never changes. It is defined by the developer and is hard coded into the application. Here is an example of a static registry item that defines a quick filter for the Manage Entries screen:
sub init_registry {
my $plugin = shift;
$plugin->registry({
'applications' => {
'cms' => {
list_filters => {
entry => {
published => {
label => sub {
$app->translate( 'Published Entries' );
},
order => 100,
handler => sub {
my ( $terms, $args ) = @_;
$terms->{status} = 2;
},
},
},
},
},
},
});
}
Dynamic Registry Items
Dynamic registry items are determined at run time. To declare a registry item dynamically the value of a registry property must a code reference. For example, the following code sample shows how to have the list of quick filters determined by a subroutine defined elsewhere in your application. That routine will return a hash that will be inserted into the registry in the subroutines place.
sub init_registry {
my $plugin = shift;
$plugin->registry({
'applications' => {
'cms' => {
list_filters =>
asset => sub { $app->asset_list_filters(@_) }, ## DYNAMIC
},
},
},
});
}
The subroutine that is called, listed below, loops over the list of all registered asset types, or classes, defining a list_filter for each.
sub asset_list_filters {
my $app = shift;
my %filters;
my $types = MT::Asset->class_labels;
foreach my $type ( keys %$types ) {
my $asset_type = $type;
$asset_type =~ s/^asset\.//;
$filters{$asset_type} = {
label => sub { MT::Asset->class_handler($type)->class_label_plural },
handler => sub {
my ( $terms, $args ) = @_;
$terms->{class} = $asset_type eq 'asset' ? '*' : $asset_type;
},
};
}
my @types =
sort { $filters{$a}{label} cmp $filters{$b}{label} } keys %filters;
my $order = 100;
foreach (@types) {
$filters{$_}{order} = $order;
$order += 100;
}
$filters{'asset'}{order} = 0;
$filters{'asset'}{label} = MT->translate("All Assets");
return \%filters;
}
2 User Contributed Notes
I know that Perl coders don't add comments to their code, but could you please make an exception? This code is not self-explanatory.
I agree.. I'm new to MT and the code so far (a few hours) seems pretty tough to follow. I'm reading up to see which CMS to go with for some enterprise solutions and comments would make a world of difference in helping me decide!
I heard good things about MT though but comments would be much nicer and be very helpful in giving me more insight into structure.
Thanks