LoginSignup
0
1

More than 1 year has passed since last update.

【Drupal】CKEditorの設定モジュールの作成

Last updated at Posted at 2021-07-29

コンテンツ登録の際に、CKEditorのフィールドタイプを使用した場合に例えばspanの空タグを入力すると消去されます。
空タグを許可するために、CKEditorの設定モジュールを作成したので備忘録として残しておきます。(コントリビュートモジュールでCKEditorの設定モジュールは転がっていると思いますが)

モジュールのディレクトリ名は「ckeditor_custom_setting」としておきます。

MY_MODULE.info.yml

modules/custom/ckeditor_custom_setting/ckeditor_custom_setting.info.yml
name: CKEditor Custom Setting
description: CKEditorのカスタム設定を有効化します。
type: module
core: 8.x
core_version_requirement: ^8 || ^9
package: Custom

MY_MODULE.info.module

hook_editor_js_settings_alterを利用します。

modules/custom/ckeditor_custom_setting/ckeditor_custom_setting.module
<?php

function ckeditor_custom_setting_editor_js_settings_alter(array &$settings) {
  foreach (array_keys($settings['editor']['formats']) as $text_format_id) {
    if ($settings['editor']['formats'][$text_format_id]['editor'] === 'ckeditor') {
      $settings['editor']['formats'][$text_format_id]['editorSettings']['customConfig'] = base_path(). drupal_get_path('module', 'ckeditor_custom_setting'). '/config.js';
    }
  }
}

config.js

設定用のjsファイルを作成する。

modules/custom/ckeditor_custom_setting/config.js
CKEDITOR.editorConfig = function( config ) {
  config.fillEmptyBlocks = false;
  config.allowedContent = true;
  config.autoParagraph = false;

  // 空のspanタグを許可
  CKEDITOR.dtd.$removeEmpty.span = 0;

  // 他に例えばh2タグをaタグで囲いたい時は以下のように記す
  CKEDITOR.dtd['a']['h2'] = 1;
};
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1