Working with the Registry
The most common way of adding things to Movable Type’s registry is via a plugin’s config.yaml
whose content ultimately gets merged into the global registry during Movable Type’s initialization phase. However, there are other ways of making modifications and additions to the registry using Movable Type’s API. This section will discuss a number of techniques you can employ to access and make maximal use of the registry.
Registry Code Handlers
In some circumstances, the contents you wish to add to the registry is based upon criteria that may change over time, and thus, a static set of registry keys is insufficient. Movable Type makes it possible to add items to the registry on the fly using a simple technique. The technique requires that for any key in the registry that you wish to be dynamic in nature, you use a code handler in place of a value you might normally associate with the key.
An example might best explain this scenario. Let’s look at two different ways to define the same set of menu items via a plugin:
applications:
cms:
menus:
manage:myobject:
label: My Objects
mode: list_myobject
order: 100
Now let’s produce the same results, but doing so programmatically. First, use a code handler as a value for your menus
key:
applications:
cms:
menus: $DemoPlugin::DemoPlugin::Plugin::load_menus
Then implement your menu loader like so:
package DemoPlugin;
use strict;
sub load_menus {
return {
'manage:myobject' => {
label => 'My Objects',
mode => 'list_myobject',
order => 100,
}
};
}
You should notice that the data structure defined by the config.yaml
version is identical to that returned by the menu loader handler. The only requirement is that your handler must return a hash. If it returns anything else, it will be likely that Movable Type will begin producing critical errors.
Removing keys from the Registry
In working directly with the registry, it is possible for your plugin not only to programatically add items to the registry, but also to remove them as well. To remove an item from the registry, you first obtain a handle to the registry and then delete
any nodes from that handle that you want to remove. Let’s look at an example in which you want to remove the “Manage” menu from Movable Type.
sub load_menus {
my $mt = MT->component('Core');
delete $mt->{registry}->{applications}->{cms}->{menus}->{manage};
return {};
}
The example above takes a hatchet to the registry, but you may also use a scalpel. In the following example, we will remove a single menu item, the “Styles” menu supplied by Style Catcher.
sub load_menus {
my $sc = MT->component('StyleCatcher');
delete $sc->{registry}->{applications}->{cms}->{menus}->{design:styles};
return {};
}