CKEditor にカスタムボタン ( Plugin )を追加する方法

前回掲載した記事「CMSを使うと誰でも簡単にカッコいいサイト更新ができるのか?」で、WYSIWYGエディターのカスタムボタン(Plugin)からの入力を紹介しました。

今回の記事では、その実装方法をご紹介いたします。

 

「Webサイトからお問い合わせが来ない…」とお悩みの方必見!
当サイトのノウハウを詰め込んだ『Web集客の無料ガイド』をご提供

CKEditor の Plugin 機能を使ってカスタムボタンを追加する手順

今回は Drupal CMS に CKEditor を導入し、CKEditor の Plugin 機能を使いカスタムボタンを実装します。

Drupal に実装しますが、基本は CKEditor のカスタマイズなので、その他 CMS でも同じようにカスタマイズすることができます。

 

今回の実装環境

  • Drupal7
  • CKEditor4

 

Drupal モジュール foobar を作成

CKEditor は基本「 ckeditor.config.js 」に設定を記述しますが、Drupal の場合設定がDatabaseに入ってしまうので、hook 関数を使用し、Plugin を追加します。
 

  1. ディレクトリ作成

    「/sites/all/modules/foobar」を作成します。
     

  2. foobar.infoファイルを作成
    1. name = foobar
    2. description = ckeditor custom button plugin
    3. core = 7.x
  3. foobar.moduleファイルを作成
    1. <?php
    2. function foobar_wysiwyg_plugin($editor, $version) {
    3. switch ($editor) {
    4. case 'ckeditor':
    5.  
    6. return array(
    7. 'foobar' => array(
    8. 'path' => drupal_get_path('module', 'foobar') . '/foobar',
    9. 'buttons' => array(
    10. 'foobar_button' => t('Do something awesome'),
    11. ),
    12. 'load' => TRUE,
    13. ),
    14. );
    15. break;
    16. }
    17. }
    18.  
  4. 作成したfoobarモジュールを有効にします。

 

CKEditor の Plugin 作成

続いて CKEditor の Plugin を作成します。ここからの設定はその他 CMS でも基本同じです。

 

  1. foobarディレクトリを作成

    「ckeditor/plugins/foobar」を作成します。
     

  2. plugin.jsファイルを作成
     
    1. (function($) {
    2. CKEDITOR.plugins.add( 'foobar', {
    3. init: function( editor )
    4. {
    5. editor.addCommand( 'my_command', {
    6.  
    7. // editor設定
    8. exec : function( editor ) {
    9.  
    10. //here is where we tell CKEditor what to do.
    11. editor.insertHtml( 'Insert Text ' );
    12. }
    13. });
    14.  
    15. // CKEditorボタン設定
    16. editor.ui.addButton( 'foobar_button', {
    17. label: 'Do something awesome',
    18. command: 'my_command',
    19. icon: this.path + 'images/icon.png'
    20. });
    21. }
    22. });
    23. })(jQuery);
  3. CKEditor コンフィグ設定(Drupalの場合は不要です)
    ckeditor.config.jsに下記行を追加します。
    1. CKEDITOR.editorConfig = function(config) {
    2. config.extraPlugins = 'foobar';
    3. }
  4. Pluginを有効にする(Drupalのみ)

    Durpal管理画面「環境設定 > CKEditor > プロフィール編集」を開き、作成した Plugin「 foobar 」を有効にします。

 

設定は以上です。
追加したカスタムボタン( Plugin )を WYSIWYG エディターに反映し、ボタンをクリックすると「 Insert Text 」が挿入されたかと思います。

 

最後に

 

CMSを使うと誰でも簡単にカッコイイサイト更新ができるのか?」で紹介させていただきましたが、CMS は Web の専門知識がないユーザーも入力を行うことを想定して設計しなくてはいけません。

誰が入力しても「デザインの統一」をするためには、今回紹介した WYSIWYG のテンプレート機能か、カスタムボタンを使うことで実現が可能です。

CMSを導入したが操作が難しいと感じるかたは、参考にしてみてください。

 

関連記事

CKEditor Document Configs (外部サイト)
Drupalとの連携にかかわらずCKEditorの設定を変更する場合にconfigの変更が必要です。

DrupalのCKEditorにテンプレート挿入機能を追加

CMSを使うと誰でも簡単にカッコイイサイト更新ができるのか??

google-code-prettifyでソースコードを見やすくする 

関連タグ:

CPIの最新情報をTwitterでチェックできます!
@cpiadjp
次へ
前へ