7.25 プラグインの開発

P.739

Foo.pl

package MT::Plugin::Foo;
 
use strict;
use base qw( MT::Plugin );
 
our $VERSION = '0.01';
 
my $plugin = MT::Plugin::Foo->new({
 name => 'Foo',
 version => $VERSION,
 description => 'Foo Plugin.',
 author_name => 'foo',
 author_link => 'http://user-domain/',
 plugin_link => 'http://user-domain/plugin.html',
 doc_link => 'http://user-domain/sample.html',
 l10n_class => 'Foo::L10N',
});
MT->add_plugin($plugin);
 
sub init_registry {
 my $plugin = shift;
 $plugin->registry({
...略...
 });
}
sub _foo {
...略...
}
1;

ブロックタグ

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    tags => {
      block => {
        'FooBlock' => \&_hdlr_foo_block,
      },
    },
  });
}

P.740

ファンクションタグ

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    tags => {
      function => {
        'FooFunction' => \&_hdlr_foo_function,
      },
    },
  });
}

グローバルモディファイア

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    tags => {
      modifier => {
        'foo_bar' => \&_fltr_foo_bar,
      }, 
    },
  });
}

テキストフィルタ

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    text_filters => {
      'my_filter' => {
        label => 'Transform this Text',
        handler => 'MT::Foo::text_transform',
      },
    },
  });
}

コールバック

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    callbacks => {
      'cms_post_save.entry' => \&_save_foo,
    },
  });
}

ダッシュボードウィジェット

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    applications => {
      cms => {
        widgets => { 
          'Foo' => {	
            label    => 'Foo Widget',
            handler  => \&_widget_handler,
            template => 'widget.tmpl',
            set      => 'main',
            singular => 1,
          },
        },
      },
    },
  });
}

P.741

widget.tmpl

<mt:app:widget class="widget" label="Foo Widget" can_close="1">
...中略...
</mt:app:widget>

データベースへのカラム追加

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    object_types => {
      'entry' => {
        'recommend' => 'string(255)',
      },
    },
  });
}

プラグイン一覧画面の初期化フィールドの定義

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    blog_config_template => 'config.tmpl',
    settings => MT::PluginSettings->new([
        ['def_entry_title', { Default => undef, Scope => 'blog' }],
        ['def_entry_content', { Default => undef, Scope => 'blog' }],
    ]),
  });
}

P.742

init_registryへの複数データ定義

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    tags => {
      block => {
        'FooBlock' => \&_hdlr_foo_block,
      },
      function => {
        'FooFunction' => \&_hdlr_foo_function,
      },
  });
}

データベースへのカラム追加とコールバックの設定

sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    object_types => {
      'entry' => {
        'recommend' => 'string(255)',
      },
    },
    callbacks => {
      'cms_post_save.entry' => \&_save_foo,
    },
  });
}

標準テンプレートタグをオーバーライドする

sub init {
    my $plugin = shift;
    $plugin->SUPER::init(@_);
    my $core = MT->component('core');
    my $r = $core->registry("tags", "block");
    $r->{EntryCategory} = \&_my_hdlr_entry_category;
}

P.743

add_char.pl

package MT::Plugin::add_char;
use strict;
use MT;
use base qw(MT::Plugin);
 
our $VERSION = '0.01';
 
my $plugin = MT::Plugin::add_char->new({
    name => 'add_char',
    description => 'add char.',
    doc_link => 'http://user-domain/sample.html',
    author_name => 'foo',
    author_link => 'http://user-domain/',
    version => $VERSION,
});
MT->add_plugin($plugin);
 
sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    tags => {
      modifier => {
       'add_char' => \&_fltr_add_char,
      },
    }
 });
}
sub _fltr_add_char {
  my ($str, $val, $ctx) = @_;
  return $val . $str;
}
 
1;

BlogArchiveRelativeURL.pl

package MT::Plugin::BlogArchiveRelativeURL;
use strict;
use MT;
use base qw(MT::Plugin);
 
our $VERSION = '0.01';
 
my $plugin = MT::Plugin::BlogArchiveRelativeURL->new({
    name => 'BlogArchiveRelativeURL',
    description => 'blog arvehive relative URL.',
    doc_link => 'http://www.koikikukan.com/archives/2008/06/09-013333.php',
    author_name => 'Yujiro Araki',
    author_link => 'http://www.koikikukan.com/',
    version => $VERSION,
});
MT->add_plugin($plugin);
 
sub init_registry {
    my $plugin = shift;
    $plugin->registry({
        tags => {
            function => {
               'BlogArchiveRelativeURL' => \&_hdlr_blog_arcehive_relative_url,
            },
        }
   });
}
 
sub _hdlr_blog_arcehive_relative_url {
    my ($ctx, $args, $cond) = @_;
    my $blog = $ctx->stash('blog');
    return '' unless $blog;
    my $host = $blog->archive_url;
    return '' unless defined $host;
    if ($host =~ m!^https?://[^/]+(/.*)$!) {
        return $1;
    } else {
        return '';
    }
}
 
1;

P.744
P.745

<mt:templates>
  <mt:templateTypeIfIndex>
    <mt:templateName/>
  </mt:templateTypeIfIndex>
</mt:templates>

Templates.pl

package MT::Plugin::Templates;
use strict;
use MT;
use base qw(MT::Plugin);
 
our $VERSION = '0.01';
 
my $plugin = MT::Plugin::Templates->new({
    name => 'Templates',
    description => 'Templates plugin.',
    doc_link => 'http://user-domain/sample.html',
    author_name => 'foo',
    author_link => 'http://user-domain/',
    version => $VERSION,
});
MT->add_plugin($plugin);
 
sub init_registry {
  my $plugin = shift;
  $plugin->registry({
    tags => {
      block => {
        'Templates' => \&_hdlr_templates,
        'TemplateTypeIfIndex?' => \&_hdlr_template_type_if_index,
      },
      function => {
        'TemplateName' => \&_hdlr_template_name,
      },
    }
 });
}
sub _hdlr_templates {
  my ($ctx, $args, $cond) = @_;
  my $iter = MT::Template->load_iter({ blog_id => $ctx->stash('blog_id') },
                                     { 'sort' => 'name' });
  my $res = '';
  my $tokens = $ctx->stash('tokens');
  my $builder = $ctx->stash('builder');
  while (my $tmpl = $iter->()) {
    local $ctx->{__stash}{'tmpl'} = $tmpl;
    defined(my $out = $builder->build($ctx, $tokens, $cond)) or
            return $ctx->error( $builder->errstr );
    $res .= $out;
  }
  $res;
}
sub _hdlr_template_type_if_index {
    my ($ctx, $args) = @_;
    my $tmpl = $ctx->stash('tmpl')
        or return _error($ctx, 'MT'.$ctx->stash('tag'));
    return $tmpl->type =~ /index/;
}
sub _hdlr_template_name {
    my ($ctx, $args) = @_;
    my $tmpl = $ctx->stash('tmpl')
        or return _error($ctx, 'MT'.$ctx->stash('tag'));
    $tmpl->name;
}
sub _error {
    my ($ctx, $tag) = @_;
    $ctx->error(MT->translate
       ('You used an [_1] tag outside of the proper context.', $tag));
}
 
1;

ウェブページ

Powered by Movable Type 4.261

このアーカイブについて

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。