Forcing Paste as Text in CKEditor

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
lostcarpark's picture

My client wants a rich text editor, but a lot of the content of the site will be coming from Word documents, and he also wants clean HTML, so he's insisting that all text be pasted as text only, and reformatted by hand. I agree with him, as even the "Paste from Word" command leaves a lot of messy styling in.

I've discovered that with a 1-line code change, CKEditor does exactly what we want.

The change is to the file: "sites/all/modules/wysiwyg/editors/js/ckeditor-3.0.js"

The line I added is "CKEDITOR.config.forcePasteAsPlainText = true;":

/**
* Attach this editor to a target element.
*/
Drupal.wysiwyg.editor.attach.ckeditor = function(context, params, settings) {
  // Apply editor instance settings.
  CKEDITOR.config.forcePasteAsPlainText = true;
  CKEDITOR.config.customConfig = '';

This does the job, but would probably be better in the theme, rather than hacking the module.

Better still, wouldn't it be a useful feature to have a "force paste as text" checkbox in the WYSIWYG module? I can't be the only one who would find this useful.

Anyway, posting here in case it helps anyone.

James

Comments

thumbs up!

Fannon's picture

thumbs up!

Good tip. I'm also in favor

brianmercer's picture

Good tip. I'm also in favor of adding this to the module after the Paste as Word option.

I looked up the correct way

brianmercer's picture

I looked up the correct way to do this without hacking. Until they implement advanced configuration in the wysiwyg module, you should create a custom module and use this code:

/**
* Implementation of hook_wysiwyg_editor_settings_alter().
*/
function ckeditor_config_wysiwyg_editor_settings_alter(&$settings, &$context) {
  if($context['profile']->editor == 'ckeditor') {
    $settings['forcePasteAsPlainText'] = TRUE;
    $settings['disableNativeSpellChecker'] = FALSE;
  }
}

I added that other setting to get back my red squiggles in Firefox. That's optional, of course.

NOTE: this is incompatible with PHP 5.3 unless you change "&$context" to "$context".

Thanks! Could it be that this

whatdoesitwant's picture

Thanks!
Could it be that this hook is usable in other ways? (See:
http://drupal.org/node/964978 and http://drupal.org/node/313497#comment-2607762)

I mean, would this principle work for the entire ckeditor api? I would love to turn ckeditor on wysiwyg into something usable. Information on how to do so in combination with wysiwyg module seems a bit scarce however.

Although I respect the wysiwyg module's way of organizing stuff, i've become convinced that the best way to speed up wysiwyg development in Drupal would be to apply changes to the ckeditor plugin first (because of its jquery adapter and api) and backport to other plugins like tinymce later; have wysiwyg's ckeditor plugin become the Chromium to the module's IE's.

thank you.

MataHari's picture

late to the party, but this looks neat. thanks!

------------------------------------

(<>..<>)

Twitter: RomyEatsDrupal

Is there a way to do this but

oranges13's picture

Is there a way to do this but for TinyMCE instead?

Woohoo...thanks a lot

2pha's picture

Woohoo...thanks a lot

Awesome

soundboy89's picture

Thanks! I was afraid of the mess that was going to become of my content with tons of tags being pasted all over the place.

89decibeles.com - Música, arte y cultura joven en Costa Rica

Thanks!

squarecandy's picture

Perfect - made a little module based on brianmercer's code.
Always glad not to be hacking at existing popular modules.
Be sure to change to "$context" as he mentions there if you're on a new version of PHP... missed that bit at first.

6 or 7

squarecandy's picture

works on WYSIWYG > CKeditor in both Drupal 6 and 7...

Force editor to process rich text...

Phix500's picture

I was hoping I could do the opposite (sort of) I'm reading text from a node->body which is html. I want the editor to display as rich text.

Actually, if I use the editor

Phix500's picture

Actually, if I use the editor within the Drupal UI to write
rich text, my PHP script reads it and displays rich text.
It's when my PHP script writes to the node it doesn't get
saved as rich text.
If I then open the node with the UI, it will appear
as "

header

Forcing plain text pasting in drupal 8

neardark's picture

For anyone looking to do this in D8, use hook_editor_js_settings_alter():

Example:

<?php
function hook_editor_js_settings_alter(array &$settings) {
  if (empty(
$settings['editor']['formats']['full_html'])) {
    return;
  }

 
$full_html = &$settings['editor']['formats']['full_html'];
 
$full_html['editorSettings']['forcePasteAsPlainText'] = 'true';
}
?>

Drupal 8 Issues

BD3's picture

@neardark

I wasn't able to get this to work properly. Is the above code still relevant for Drupal 8? Would you mind posting your full module here so I can attempt the same?

You need to change the input

Lukas von Blarer's picture

You need to change the input format ID:

<?php
function hook_editor_js_settings_alter(array &$settings) {
  if (!empty(
$settings['editor']['formats']['FORMAT_ID'])) {
   
$settings['editor']['formats'][''FORMAT_ID']['editorSettings']['forcePasteAsPlainText'] = 'true';
  }
}
?>

Works for me.

This is works for Drupal 8.7!

maxdev's picture

This is works for Drupal 8.7! You should use boolean value instead of string 'true'.

<?php
/**
* Implements hook_editor_js_settings_alter().
*/
function jysk_editor_js_settings_alter(array &$settings) {
  if (!empty(
$settings['editor']['formats']['full_html'])) {
   
$full_html = &$settings['editor']['formats']['full_html'];
   
$full_html['editorSettings']['forcePasteAsPlainText'] = TRUE;
  }
}
?>

Wysiwyg

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week