I am building a website in which a lot of the text comes from Word docs. The problem is that when you copy from a Word doc to WYSIWYG editor, the resulting HTML and CSS is horrible.

I want to implement a JS script that will run whenever a node (Drupal's basic page) is created or updated, to clean and optimize the data.

I've seen that I must create a custom module and use a hook. Looking through the list of Drupal 7's hooks, I've noticed hook_node_submit() which seems perfect but in it's documentation (here) is written:

This hook is intended for adjusting non-field-related properties.


  1. Do I have any other method simpler than module?
  2. What is the correct hook I should use?

2 Answers 2


You can look into either https://www.drupal.org/project/sanitizable or https://www.drupal.org/project/pasteformat both does the job.

and if you are looking hook based solution, i would suggest you hook_node_presave worth looking their documentation.

  • hook_node_presave() is invoked after a form has been submitted, and before Drupal saves the node; it cannot be used to add JavaScript code to a form.
    – apaderno
    Sep 13, 2015 at 17:53
  • agreed. not sure the above problem can easily be solved by JavaScript. so he can look hook_node_presave to strip_tags or something in that hook.
    – mks
    Sep 13, 2015 at 18:00
  • I agree on that, and I would make that point clear in the answer, since the OP is asking how to add JavaScript code and with which hook.
    – apaderno
    Sep 13, 2015 at 20:02
  • I can write it in PHP as well, I'm just not as good at it. I've looked at the modules and I think using a custom script will be easier. I basically want 3 steps: 1. Combine the CSS of a few elements that can turn to one instead, such as span in h2. 2. Remove all remaining excess elements. 3. Compare CSS left with default stylesheet and remove unnecessary duplicates. As I've seen, the "useless" stuff are usually exactly the same throughout the text.
    – Neta
    Sep 13, 2015 at 20:02

Give a try for Paste Format module. Module page says

Paste Format allows you to clean-up the text you copy/paste into your content from other web pages, MS documents, e-mail clients, etc.

There is another module as Sanitizable. Module page says

The Sanitize module allows administrators to configure a text format to be applied to formatted text before storage. This is useful to ensure clean and valid data in the database.

The major difference among two seems to the time even sanitization happens. Looks like paste format will do the time you will paste the text, which will help in showing the data which actually goes to database while the other module does it before node save, so user sees the unprocessed data before save.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.