Japanese plugin dev 3 1 - movabletype/Documentation GitHub Wiki

プラグインの設定方法

はじめに

プラグインの設定方法は環境変数を利用する方法もありますが、今回は管理画面のプラグイン設定で簡単に設定する方法を解説します。

プラグイン設定とは?

プラグイン設定とは、名前のとおり設定によりプラグインの挙動を変更するための設定方法です。

環境変数が「静」の設定だとすると、管理画面から行うのは「動」の設定と言えます。これはウェブブラウザ経由で簡単に設定を変更出きるためです。

各プラグインの管理画面に「設定」項目を表示し、そこで設定をする事でプラグインに情報が反映されます。

例)MultiBlogプラグインの設定画面

今回作成するプラグインの仕様

管理画面 プラグイン設定画面

5. テストドリブンでのプラグイン開発について」で作成した<MTHelloWorld>プラグインを改造し<MTHellowWorld2>を作成します。

  • システムでの設定
    • [システムメニュー]の[ツール] > [プラグイン] を選択し、[サンプルプラグイン プラグイン設定 1.0] > [設定] 画面を開く
    • [言葉] text入力欄の下に「表示させたい言葉を入力してください。’こんにちは、xxx!’」と表示
    • [言葉] text入力欄にフレーズを入力し、[変更を保存]ボタンをクリックして設定する
  • ブログ単位での設定
    • [ブログメニュー]の[ツール] > [プラグイン] を選択し、[サンプルプラグイン プラグイン設定 1.0] > [設定] 画面を開く
    • [言葉] text入力欄の下に「表示させたい言葉を入力してください。’こんにちは、xxx!’」と表示
    • [言葉] text入力欄にフレーズを入力し、[変更を保存]ボタンをクリックして設定する

プラグイン設定の画面仕様

ファンクションタグ(<MTHelloWorld2>)

プラグイン設定画面で設定された[言葉]により、「こんにちは、○○!」と表示するファンクションタグを作成します。

  • システムレベルの[言葉]を取得する
  • ブログレベルでの[言葉]を取得する
  • なにも設定されていなかった場合の初期値の[言葉]を「世界」にする
  • ブログレベルの[言葉]を設定していたら、「こんにちは、(ブログレベル設定)!」と出力しタグを終了する
  • ブログレベルの設定が無く、システムレベルの[言葉]を設定していたら、「こんにちは、(システムレベル設定)!」と出力しタグを終了する
  • ブログレベル、システムレベルの両方の[言葉]を設定していなかった場合は「こんにちは、世界!」と出力しタグを終了する

管理画面設定の開発

config.yaml

id: MyPlugin08
key: MyPlugin08
name: <__trans phrase="Sample Plugin plugin config">
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: MyPlugin08::L10N

blog_config_template: word_setting_blog.tmpl
system_config_template: word_setting_system.tmpl
settings:
    word_setting_blog:
        default:
        scope: blog
    word_setting_system:
        default:
        scope: system

tags:
    function:
        HelloWorld2: $MyPlugin08::MyPlugin08::Tags::_hdlr_hello_world_2
  • “blog_config_template”, “system_config_template” に、設定画面のテンプレート名を設定します(詳細後述)
  • "settings"で設定名を"word_setting_blog", "word_setting_system"とし、それぞれ初期値"default"は空欄、設定の"scope"をそれぞれ"blog"と"system"にします。

word_setting_blog.tmpl

ブログの設定画面で使用するテンプレートです。このファイルは"MyPlugin08/tmpl/word_setting_blog.tmpl"に配置します。
ブログ設定"word_setting_blog"を設定するためのテンプレートです。

<mtapp:setting id="word_setting_blog" label="<__trans phrase='_MyPlugin08_Word'>"
    hint="<__trans phrase="Please input the word, 'Hello, xxx!'">" show_hint=1>
    <input type="text" name="word_setting_blog" id="word_setting_blog"
        value="<mt:GetVar name="word_setting_blog">" />
</mtapp:setting>

この記述を行うと管理画面内のプラグイン設定で自動的に以下のようにHTMLを作成します。(わかりやすいよう整形してあります)

<!-- Plugin Config -->

<div id="plugin17-config" class="config">
    <form method="post" action="/cgi-bin/mt/mt.cgi" id="plugin-17-form">
        <input type="hidden" name="__mode" value="save_plugin_config" />
        <input type="hidden" name="blog_id" value="2" />
        <input type="hidden" name="return_args" value="__mode=cfg_plugins&amp;_type=blog&amp;blog_id=2&amp;id=2" />
        <input type="hidden" name="plugin_sig" value="MyPlugin08" />
        <input type="hidden" name="magic_token" value="(略)" />

        <fieldset>
            <div id="word_setting_blog-field" class="field field-left-label ">
                <div class="field-header">
                     <label id="word_setting_blog-label" for="word_setting_blog">言葉</label>
                </div>
                <div class="field-content ">
                    <input type="text" name="word_setting_blog" id="word_setting_blog" value="" />
                    <div class="hint">表示させたい言葉を入力してください。'こんにちは、xxx!'</div>
                </div>
            </div>
        </fieldset>

        <div class="actions-bar settings-actions-bar last-child">
            <button mt:mode="save_plugin_config" type="submit"
                class="save action primary-button first-child last-child">変更を保存</button>
        </div>
    </form>
</div>

word_setting_system.tmpl

システムの設定画面で使用するテンプレートです。このファイルは"MyPlugin08/tmpl/word_setting_system.tmpl"に配置します。
システム設定"word_setting_system"を設定するためのテンプレートです。

<mtapp:setting id="word_setting_system" label="<__trans phrase='_MyPlugin08_Word'>"
    hint="<__trans phrase="Please input the word, 'Hello, xxx!'">" show_hint=1>
    <input type="text" name="word_setting_system" id="word_setting_system"
        value="<mt:GetVar name="word_setting_system">" />
</mtapp:setting>

この記述を行うと管理画面内のプラグイン設定で自動的に以下のようにHTMLを作成します。(わかりやすいよう整形してあります)

<!-- Plugin Config -->

<div id="plugin17-config" class="config ui-tabs-panel ui-widget-content ui-corner-bottom last-child">
    <form class="first-child last-child" method="post" action="/cgi-bin/mt/mt.cgi" id="plugin-17-form">
        <input class="first-child hidden" name="__mode" value="save_plugin_config" type="hidden">
        <input class="hidden" name="return_args" value="__mode=cfg_plugins&amp;_type=blog&amp;blog_id=0&amp;id=2">
        <input class="hidden" name="plugin_sig" value="MyPlugin08" type="hidden">
        <input class="hidden" name="magic_token" value="(略)" type="hidden">

    <fieldset>
        <div id="word_setting_system-field" class="field field-left-label  first-child last-child">
            <div class="field-header first-child">
                <label class="first-child last-child" id="word_setting_system-label" for="word_setting_system">言葉</label>
            </div>
            <div class="field-content  last-child">
                <input class="first-child text" name="word_setting_system" id="word_setting_system" value="" type="text">
                <div class="hint last-child">表示させたい言葉を入力してください。'こんにちは、xxx!'</div>
            </div>
        </div>
    </fieldset>

    <div class="actions-bar settings-actions-bar last-child">
        <button mt:mode="save_plugin_config" type="submit"
            class="save action primary-button first-child last-child">変更を保存</button>
    </div>
</form>
</div>

これで管理画面で設定する準備が整いました。

ファンクションタグ プラグイン(<MTHelloWorld2>)の開発(Perl)

Tags.pm以外はMyPlugin03とあまり変わらないため、Tags.pmの注意する点に集中して解説します。

package MyPlugin08::Tags;
use strict;

sub _hdlr_hello_world_2 {
    my ($ctx, $args) = @_;
    my $blog_id = 'blog:' . $ctx->stash('blog_id');
    my $plugin = MT->component('MyPlugin08');

    my $word_blog = $plugin->get_config_value('word_setting_blog', $blog_id);
    my $word_sys = $plugin->get_config_value('word_setting_system', 'system');

    my $word_default = MT->translate('world');

    my $word = $word_blog ? $word_blog :
                 ( $word_sys ? $word_sys : $word_default );

    return MT->translate( 'Hello, [_1]!', $word );
}

1;
  • $ctx->stash('blog_id') をつかって、"blog:12"のような文字列 $blog_id を取得します
  • プラグインオブジェクトを MT->component('MyPlugin08') から取得します
  • ブログ設定を $word_blog、システム設定を $word_sys として $plugin->get_config_value((設定名), ('system' or $blog_id)) を実行し取得します。
  • $word_default として初期値「世界」を設定します
    • テンプレート内などではローカライズとして <__trans> を利用していましたが、プログラム内では MT->translate() を利用します
  • 三項演算子を利用して、ブログ設定がある場合は $word_blog を、ブログ設定が無い場合はシステム設定 $word_sys を、両方無い場合はデフォルトの $word_default$word に設定します。
  • 「こんにちは、 $word !」を返します。

ファンクションタグ プラグイン(<MTHelloWorld2>)の開発(PHP)

PHP版もPerl版とほとんど変わらない構造ですが、関数名などが違います。また、ローカライズもPerl版の物を利用しないため手順を追加する必要があります。

function.mthelloworld2.php

<?php
    function smarty_function_mthelloworld2($args, &$ctx) {
        $mt = MT::get_instance();
        $blog_id = 'blog:' . $ctx->stash('blog_id');

        $cfg = $mt->db()->fetch_plugin_data('MyPlugin08', 'configuration:' . $blog_id);
        $word_blog = $cfg['word_setting_blog'];

        $cfg = $mt->db()->fetch_plugin_data('MyPlugin08', 'configuration');
        $word_sys = $cfg['word_setting_system'];

        $word_default = $mt->translate('world');

        $word = $word_blog ? $word_blog :
                  ( $word_sys ? $word_sys : $word_default );

        return $mt->translate( 'Hello, [_1]!', $word );
    }
?>
  • MTオブジェクト $mt を、 MT::get_instance() を使って取得します
  • $ctx->stash('blog_id') をつかって、"blog:12"のような文字列 $blog_id を取得します
  • プラグイン設定 $cfg$mt->db()->fetch_plugin_data('MyPlugin08', 'configuration:' . $blog_id) を実行し取得します。
    • ブログ設定 $word_blog$cfg['word_setting_blog'] として取得します。
  • プラグイン設定 $cfg$mt->db()->fetch_plugin_data('MyPlugin08', 'configuration') を実行し取得します。
    • ブログ設定 $word_sys$cfg['word_setting_system'] として取得します。
  • $word_default として初期値「世界」を設定します
    • テンプレート内などではローカライズとして <__trans> を利用していましたが、プログラム内では $mt->translate() を利用します
  • 三項演算子を利用して、ブログ設定がある場合は $word_blog を、ブログ設定が無い場合はシステム設定 $word_sys を、両方無い場合はデフォルトの $word_default$word に設定します。
  • 「こんにちは、 $word !」を返します。

init.MyPlugin08.php

PHP版のプラグインでローカライズをする場合、Perl版のL10N.pmなどを使った方法が使えません。

そこで、プラグインを初期化する時に実行される init.(プラグイン名).php ファイルを用意し、Perl版の"L10N/ja.pm"などに変わる物を読み込ませます。

<?php
$lang = substr(strtolower($this->config('DefaultLanguage')), 0, 2);
if (!@include_once("lib/l10n_$lang.php")) {
    include_once("lib/l10n_en.php");
}
?>

処理自体はとても簡単で、DefaultLanguagenの設定を読み最初の2文字を使ってローカライズファイルを読み込みます。読み込めなかった場合、"l10n_en.php"が読み込まれます。

これらのコードはプラグインのハンドラ関数に直接書けばよいのですが、複雑なプラグインで関数が増えてくると毎回書かなくてはならないので、初期化時に読み込むようにします。

l10n_en.php

今回のPHP版プラグインでは英語文言を必要としないため、 $Lexicon_en は空になります。

<?php
global $Lexicon_en;
$Lexicon_en = array(
);
?>

l10n_ja.php

日本語のローカライズファイルです。Lexiconが $Lexicon_ja となっているのに気をつけてください。

<?php
global $Lexicon_ja;
$Lexicon_ja = array(
    'world' => '世界',
    'Hello, [_1]!' => 'こんにちは、[_1]!',
);
?>

ファイルの配置

$MT_DIR/
|__ plugins/
   |__ MyPlugin08/
      |__ config.yaml
      |__ lib/
      |  |_ MyPlugin08/
      |     |__ L10N.pm
      |     |_ L10N/
      |     |  |_ en_us.pm
      |     |  |_ ja.pm
      |     |_ Tags.pm
      |__ php/
      |  |_function.mthelloworld2.php
      |  |_init.MyPlugin08.php
      |  |_lib/
      |        |_ l10n_en.php
      |        |_ l10n_ja.php
      |__ t/
      |  |_00-compile.t
      |  |_01-tags.t
      |__ tmpl/
         |_word_setting_blog.tmpl
         |_word_setting_system.tmpl

<MTHelloWorld2>の動作確認

ブログ記事テンプレートにタグを追加

以下のようにブログ記事テンプレートにタグを"alpha-inner"直下に追加します。

                    <div id="alpha">
                        <div id="alpha-inner">
                        <p><mt:HelloWorld2></p>

設定の反映の確認

以下のような設定で表示結果を見ます。

システム設定 ブログ設定 表示結果
「こんにちは、世界!」
システム 「こんにちは、システム!」
ブログ 「こんにちは、ブログ!」
システム ブログ 「こんにちは、ブログ!」

注目していただきたいのは、システム設定に何かしら設定されていた場合でも、ブログ設定が当てはまるという事です。
これはプログラム内でそのように作ってあるからですが、もちろん逆(両方設定されていたらシステム設定を利用する)も可能です。
ただ、システムデフォルトでシステム設定しておき、ブログ設定でそれを上書きするのが通常の利用のされ方なので、あえて逆に作る意味は特殊な例になります。

まとめ

プラグインの設定を行うだけであれば、config.yamlに項目を追加しtmplフォルダにテンプレートファイルを配置するだけで済むのが今回のポイントでしょうか?

環境変数との使い分けですが、先にも書きましたが一度設定してしまえばシステム全体で利用される物を環境変数、システム構築後に設定変更する可能性のある物はプラグイン設定画面を用意するという使い分けがよいでしょう。

プラグイン設定画面を用意する事で、自分以外のプラグイン利用者に有益な事ですので、積極的に利用してみて下さい。

プラグインダウンロード

MyPlugin08.zip(8.11KB)

プラグイン開発ガイド インデックス

  1. プラグイン開発のためのファーストステップ
  2. レジストリ、YAMLについて
  3. 環境変数について
  4. プラグインのローカライゼーションについて
  5. テストドリブンでのプラグインの開発について
  6. グローバル・モディファイアプラグインの開発について
  7. ファンクションタグ プラグインの開発について
  8. ブロックタグ プラグインの開発について
  9. コンディショナルタグ プラグインの開発について
  10. プラグインのデバッグ
  11. プラグインの設定方法
  12. コールバックとフックポイント
  13. スケジュールタスクの開発
  14. MTオブジェクトの利用方法
  15. 独自オブジェクトの作成
  16. 新規アプリケーションの作成
  17. Transformerプラグインの開発
  18. 管理画面のメニュー修正
  19. リストアクションの追加
  20. 動作モードの追加とモーダルウィンドウの表示
  21. 外部Web APIとの連携
  22. 権限とロール
⚠️ **GitHub.com Fallback** ⚠️