Yesterday’s Thoughts

June 22, 2005

Movable Type Plugins

I decided that I wished that my Movable Type activity log display were sorted in reverse order, so that the most recent activity was at the top of the page. A couple of minutes poking around in the source showed me the line that I need to change to reverse the sort order of the results. It was a single line change and the code was only used for this purpose. The temptation to modify the source was strong.

This would have been my second alteration to the source. The first was an addition of new code. I would be possible to pull this new code out into a patch, and then patch the next version, but I was getting edgy about these accumulated changes. Someday Movable Type 3.2 will come out, and it will have some feature I really crave, and I will install it and then spend days extracting my changes and then patching them back in.

I needed to understand Movable Type plugins.

It turns out, it isn’t really that difficult. I think I first looked at the plugins on the day that I installed MT, and there was quite a bit of apparent complexity. Now, five months later, I have looked at the code off and on, installed alternate templates, and come to understand the basic Movable Type objects. Plugins aren’t much more than that.

For the first plugin I just took the code that I had written earlier to add a MTCategoryHandle tag and packaged it up as a plugin. There was really only one line that I had to change. Where I had previously registered the new tag with the line:

$ctx->register_handler(CategoryHandle => \&_hdlr_category_handle);

in MT::Template::Context. Now that same registration was expressed as:

MT::Template::Context->add_container_tag(MTCategoryHandle => \&_hdlr_category_handle);

in CategoryHandle.pl. Everything else was a matter of packaging.

The second plugin was a little hairier, but not appreciably so. Remember what I wanted to do. I wanted to replace a function that retrieved results from the database sorted in the default ascending order, and replace it with a function that returns results in descending order.

I had figured out enough to know that all I had to do was pass the hash key “direction” with the value “descend” to get the behaviour I wanted. (I had actually made this change in the source and reverted it, before I reversed the change.)

For this change, I lifted the MT::App::CMS::view_log function verbatim and made the single change. I had a little more trouble with this case though. There weren’t any example plugins where a standard part of the MT api had been replaced. I knew it had to be possible, but the first couple of tries had some problems.

Then I recalled the there had just been a patch to MT for a problem with UTF-8 characters in directory names. I had read about this patch, but I didn’t think about how it worked. It replaces the installed utf8_dirify function. Replacing is what I wanted to do with the view_log function.
All I really had to do was shut off warnings for redefining of a sub and then to assign the new anonymous sub to MT::App::CMS::view_log. There was some other fiddling to do to call in the other code I was accessing, but that was obvious.

This was pretty straight-forward case once I put my code hat on. It is amazing how quickly I forget things like how to debug and where to put semi-colons. When I am coding regularly, these things become second nature and it is like there is no thought involved. When I am not coding regularly, there is just an extra beat between action and response. I have to think. It also helps to have my toolset lined up. Some large percentage of the effort in figuring out why some code works or doesn’t is in being able to see it happening. The other large percentage is mapping what you can see (error messages, odd behavior) to the problem.

I’d like to add some more functionality to the view_log code. I’d like to be able to sort the log either way with a button, to make the default sort order configurable, or to restrict errors to a certain IP address.

1 Comment(s)

  1. ZenTiger | Jun 26, 2005 | Reply

    Howdy. I’ve just started learning about blogging, and have been using MT 3.15 since March. With minimal knowledge I’ve been working “step by step” as I need to do something, and its been fun.

    I liked setting up an “auto-display” of the last post of the category “Satori” for example. Saves me editing the template when I want to update my sidebar. I just do a post by a certain category, and even after it scrolls off the main list, its still up in the sidebar!

    I’ve just come across your blog and found the MT experiences you’ve documented interesting. I don’t think I’d have the patience to note the same stuff in the same detail – but it’s great.

    If you notice any item on my site that you are curious about, maybe I can save you 5 minutes of time if you leave a note. In a month or two, I may have progressed enough to save you 10 minutes!! It would be the least I could do, since it looks like you are going to save me a bit of time back!

Sorry, comments for this entry are closed at this time.