English plugin dev 3 2 - movabletype/Documentation GitHub Wiki

Callbacks and Hooks

Introduction

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

Callbacks and Hooks? are we pirates now?

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

A simple example of a Callback

The following is an example of using a callback that is called after an entry save

Specification

The hook-point that we use is called cms_post_save.entry

  1. After blog entry is saved, the hook point cms_post_save.entry is called
  2. If this is a new entry, we want to add to the log “save entry title (blog_id): Entry title”
  3. If this is an edit of existing entry, we want to add to the log “edit entry title (blog_id): Entry title”
  4. The log level will be set to the ‘debug’

config yaml

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

Commentary

  • 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)

Callbacks.pm

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;

Commentery

  • 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

Trying it out

  1. Write a new blog post
  2. Update the blog post that your have just wrote
  3. 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.

Hook Point

As seen above, MT have a lot of hook points that a plugin can connect to, and extend MT’s behavior

Main Hook points

Most of the important hook point are among the following categories:

  1. Object: save, delete and edit
  2. オブジェクトを参照、保存、削除できるかの権限チェック(フィルタ)
  3. After building a page, after saving a file
  4. ブログ記事やウェブページ、アーカイブなどを出力するかどうかのチェック(フィルタ)
  5. upload files and images using the XML-RPC / Atom API,
  6. Application initialization, before application start, after starting
  7. Backup, restore
  8. Comments, trackbacks (filter)
  9. A template set was modified

For more information, refer to the following document:

MOVABLETYPE.org : Events and Callbacks in Movable Type: Table of Contents

Object Save, edit and delete

As these are the most frequently used callbacks, we describe them here in details

Object: before saving, after saving

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)

Object Delete

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)

Object Edit

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)

Object and Object Name

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)

Summery

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!

Download Plugin

MyPlugin09.zip(2.30KB)

Navigation

Prev:Plugin Configuration << Index >> Next:Adding Scheduled Tasks

⚠️ **GitHub.com Fallback** ⚠️