English plugin dev 5 1 - movabletype/Documentation GitHub Wiki

Adding an action list

Up until now, for modifying the management screens, we saw the transformer API for modifying a screen, or adding a new screen and a new menu item.

Now we will see an example of adding operations inside an existing screen, without having to mess with the template using the transformer API, but using the MT registry.

What is the action list?

When you look on a listing of things, (for example, blogs, websites, entries, comments, users and so on) there are two action bars – one on the top and one on the bottom. In these bars there can be buttons and a drop-down menu with additional operations that can be applied to these items.

We will explore how to add to these actions.
This feature is part of the listing framework, and there is more technical-complete (with all the available options) in List Action Documentation

In this example, we will add operations to the list users screen

Plugin Development

Specification

  • Add to the screen at [System Overview] > [Users]
  • Add the ability to change the user’s language
  • Support the languages that are supported by MT: English, Japanese, German, Spanish, French, Dutch

config.yaml

id: MyPlugin15
key: MyPlugin15
name: <__trans phrase="Sample Plugin Add Action">
version: 1.0
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: MyPlugin15::L10N

applications:
    cms:
        list_actions:
            author:
                author_language_ja:
                    label: Language Japanese
                    order: 100
                    permit_action: administers
                    handler: $MyPlugin15::MyPlugin15::Actions::hndl_author_language_ja
                author_language_en_US:
                    label: Language English
                    order: 200
                    permit_action: administers
                    handler: $MyPlugin15::MyPlugin15::Actions::hndl_author_language_en_US
                author_language_de:
                    label: Language Deutsch
                    order: 300
                    permit_action: administers
                    handler: $MyPlugin15::MyPlugin15::Actions::hndl_author_language_de
                author_language_es:
                    label: Language Español
                    order: 400
                    permit_action: administers
                    handler: $MyPlugin15::MyPlugin15::Actions::hndl_author_language_es
                author_language_fr:
                    label: Language Français
                    order: 500
                    permit_action: administers
                    handler: $MyPlugin15::MyPlugin15::Actions::hndl_author_language_fr
                author_language_nl:
                    label: Language Nederlands
                    order: 600
                    permit_action: administers
                    handler: $MyPlugin15::MyPlugin15::Actions::hndl_author_language_nl

Commentary

id: MyPlugin15
key: MyPlugin15
name: <__trans phrase="Sample Plugin Add Action">
version: 1.0
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: MyPlugin15::L10N
  • If you don’t know these already by heart, I have some bad news for you…
applications:
    cms:
        list_actions:
            author:
  • Specifying that the following should be added to the user listing screen
                author_language_ja:
                    label: Language Japanese
                    order: 100
                    permit_action: administers
                    handler: $MyPlugin15::MyPlugin15::Actions::hndl_author_language_ja
  • author_language_ja :list action
    • label: Language Japanese :The label to be shown to the user
    • order: 100 :the actions are sorted, the action with the smallest order first
    • permit_action: administers :Show this only to administrators
    • handler: $MyPlugin15::MyPlugin15::Actions::hndl_author_language_ja :Set the handler

And the same is done for the rest of the languages

MyPlugin15/Action.pm

package MyPlugin15::Actions;
use strict;

sub hndl_author_language_ja {
    my $app = shift;
    _set_author_language($app, 'ja');
}

sub hndl_author_language_en_US {
    my $app = shift;
    _set_author_language($app, 'en_US');
}

sub hndl_author_language_de {
    my $app = shift;
    _set_author_language($app, 'de');
}

sub hndl_author_language_es {
    my $app = shift;
    _set_author_language($app, 'es');
}

sub hndl_author_language_fr {
    my $app = shift;
    _set_author_language($app, 'fr');
}

sub hndl_author_language_nl {
    my $app = shift;
    _set_author_language($app, 'nl');
}

sub _set_author_language {
    my ($app, $language) = @_;
    require MT::Author;
    my @author_ids = $app->param('id');
    for my $author_id (@author_ids) {
        my $author = MT::Author->load($author_id);
        $author->preferred_language($language);
        $author->save;
    }
    $app->redirect( $app->return_uri );
}

1;

Commentary

package MyPlugin15::Actions;
use strict;
  • Package name and the usual use strict;
sub hndl_author_language_ja {
    my $app = shift;
    _set_author_language($app, 'ja');
}
  • sub hndl_author_language_ja {} – the handler for Japanese
    • We call the internal function _set_author_language(); with $app and 'ja' to do the real work

We won’t go over each language’s handler, as they are the same

sub _set_author_language {
    my ($app, $language) = @_;
    require MT::Author;
    my @author_ids = $app->param('id');
    for my $author_id (@author_ids) {
        my $author = MT::Author->load($author_id);
        $author->preferred_language($language);
        $author->save;
    }
    $app->redirect( $app->return_uri );
}
  • sub _set_author_language {} – internal function
  • Required parameters: $app and $language
  • $app->param('id') contains the list of the author to do the operation on. we store it in @$author_ids
  • for each $author_id in the list of authors:
    • Load $author
    • Set the preferred language
    • Save $author
    • After processing all the authors, return to previous screen

Directory Structure

$MT_DIR/
|__ plugins/
   |__ MyPlugin15/
      |__ config.yaml
      |__ lib/
         |_ MyPlugin15/
            |__ Actions.pm
            |__ L10N.pm
            |_ L10N/
               |_ en_us.pm
               |_ ja.pm

Plugin Download

MyPlugin15.zip(2.57KB)

Navigation

Prev:Modifying the management screen menu << Index >> Next:Add and display a modal window

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