English plugin dev 3 2 - movabletype/Documentation GitHub Wiki
Until now, we built plugins that introduce additional tag to Movable Type. Now we are going to reach into MT’s operation itself, and extend it using the callbacks mechanism
Throughout MT’s code there are a lot of “hook points”, to which it is possible to register callbacks, affect and extend the normal operation of Movable type.
Examples for hook points: on object save, page creation and API operations.
In your config.yaml you can define and connect a callback to a hook point
The following is an example of using a callback that is called after an entry save
The hook-point that we use is called cms_post_save.entry
- After blog entry is saved, the hook point
cms_post_save.entry
is called - If this is a new entry, we want to add to the log “save entry title (blog_id): Entry title”
- If this is an edit of existing entry, we want to add to the log “edit entry title (blog_id): Entry title”
- The log level will be set to the ‘debug’
id: MyPlugin09 key: MyPlugin09 name: <__trans phrase="Sample Plugin callback"> version: 1.1 description: <__trans phrase="_PLUGIN_DESCRIPTION"> author_name: <__trans phrase="_PLUGIN_AUTHOR"> author_link: http://www.example.com/about/ doc_link: http://www.example.com/docs/ l10n_class: MyPlugin09::L10N callbacks: MT::App::CMS::cms_post_save.entry: $MyPlugin09::MyPlugin09::Callbacks::post_save_entry
- MT::App::CMS::cms_post_save.entry
- This is the hook for which we want this callback to be called
- To be called after (post) a blog post (entry) is saved (save) =>
cms_post_save.entry
- $MyPlugin09::MyPlugin09::Callbacks::post_save_entry
- The handler function to use
- ($Plugin name) :: (Perl Module) :: (handler function)
package MyPlugin09::Callbacks; use strict; sub post_save_entry { my ($cb, $app, $obj, $org_obj) = @_; my $id = $obj->id; my $title = $obj->title; if (defined($org_obj->id)) { doLog("edit entry title ($id): $title"); } else { doLog("save entry title ($id): $title"); } } sub doLog { my ($msg, $class) = @_; return unless defined($msg); require MT::Log; my $log = new MT::Log; $log->message($msg); $log->level(MT::Log::DEBUG()); $log->class($class) if $class; $log->save or die $log->errstr; } 1;
- sub post_save_entry
- ($cb, $app, $obj, $org_obj) => (Callback handler reference, application handle, the object after it was saved, the object before changes)
- Gets the current title and id of the object
- If id was set before the object was saved (meaning that it is an edit of an existing object)
- Print “edit entry title (entry id): entry title” to the log (using the doLog function)
- If the id was not set (meaning that this is a new object)
- Print “save entry title (entry id): entry title” to the log (using the doLog function)
- sub doLog
- This logging function was discussed in the Plugin Debugging chapter
- Saves the message in the MT log, using the ‘debug’ log level
- This logging function was discussed in the Plugin Debugging chapter
- Write a new blog post
- Update the blog post that your have just wrote
- Go to System view → Tools → Log
You will probably see the plugin messages on the top of the log. If you happen to do it on a busy site, (playing with your production site again, are we?) you may need to filter the log to show only debug-level messages.
As seen above, MT have a lot of hook points that a plugin can connect to, and extend MT’s behavior
Most of the important hook point are among the following categories:
- Object: save, delete and edit
- オブジェクトを参照、保存、削除できるかの権限チェック(フィルタ)
- After building a page, after saving a file
- ブログ記事やウェブページ、アーカイブなどを出力するかどうかのチェック(フィルタ)
- upload files and images using the XML-RPC / Atom API,
- Application initialization, before application start, after starting
- Backup, restore
- Comments, trackbacks (filter)
- A template set was modified
For more information, refer to the following document:
MOVABLETYPE.org : Events and Callbacks in Movable Type: Table of Contents
As these are the most frequently used callbacks, we describe them here in details
A hook-point that is placed just before an object is saved, will be called MT::App::CMS::cms_pre_save.(OBJECT_NAME)
A hook-point that is placed just after an object is saved to the database, will be called MT::App::CMS::cms_post_save.(OBJECT_NAME)
sub callback_handler { my ($cb, $app, $obj, $org_obj) = @_; # do something }
The arguments are: ($cb, $app, $obj, $org_obj) => (Callback handler reference, application handle, the object after it was saved, the object before changes)
This hook point will be called immediately after deleting an object. MT::App::CMS::cms_post_delete.(OBJECT_NAME)
sub callback_handler { my ($cb, $app, $obj) = @_; # do something }
Parameters: ($cb, $app, $obj) => (callback handler, app handler, the deleted object)
This hook point will be called before displaying the edit screen to the user. It is given the object to be edited, and most of the template parameter. MT::App::CMS::cms_edit.(OBJECT_NAME)
sub callback_handler { my ($cb, $app, $id, $obj, $param_ref) = @_; # do something }
Parameters: ($cb, $app, $id, $obj, $param_ref) => (callback handler, app handler, object id, the object to be edited, template parameters)
Not all object types have hook points set for, but all the important ones are covered. If you feel that a callback is missing, fill a bug and we will consider it.
The “OBJECT_NAME” string that we used above is the representative object type for MT. Below is a vary partial list of object names
- author
- website
- blog
- category
- folder
- comment
- ping (trackback)
Did you know that before a template is being rendered, a callback is called? you can use this to customize template with minimal intervention in the normal operation of Movable Type.
You can use callback to extend MT in small ways, or write huge plugins. Try it!
Prev:Plugin Configuration << Index >> Next:Adding Scheduled Tasks