Search code examples
drupal-7htmlpurifier

HTMLPurifier custom filter configuration


I've seen many posts on various sites which talk about adding "to your HTML Purifier config", like Sonny's excellent response to this question: HTMLPurifier iframe Vimeo and Youtube video

However, try as I might, I can't seem to discover exactly how to set up my HTML Purifier config. I'm actually trying to add a custom filter a la Sonny's comment, but the closest I can come to discovering how to set up my HTML purifier config in my Drupal 7 environment is from /sites/all/modules/htmlpurifier/config/sample.php:

<?php

/**
 * @file
 * This file is a sample advanced PHP configuration file for the HTML Purifier
 * filter module. In reality, this file would be named N.php, where N is the
 * integer identifying the filter this is configuring. The configure page
 * for HTML Purifier (advanced) will tell you what file to copy this to.
 *
 * See this URI:
 *
 *    http://htmlpurifier.org/live/configdoc/plain.html
 *
 * For full information about permitted directives. The most interesting ones
 * for custom configuration are ones with the 'mixed' type, as they cannot
 * be configured using the webform.

But this hasn't helped me at all - I don't see anything on the HTML Purifier (advanced) configure page (on the text filter config page? I don't see anything there...), and the doc at the URI above didn't help, either.

Edit: much thanks to @Chris for his answer to my original question. I subsequently found that creating the filtered_html.php file removes the ability for me to configure HTML Purifier for Filtered HTML from the UI! Is this supposed to happen?

All that I see from the UI when looking at HTML Purifier (Advanced) is the checkbox for "Display help text". (I already added this as a comment below, but I thought I'd modify my question as well in the hopes that more people would see this...)


Solution

  • First, make sure you have the HTML purifier library installed as show in the INSTALL.txt file. The status report page will show you the version if it's installed correctly.

    In Drupal 7 the filter system now uses string keys rather than integer. So to create a configuration file for the format "Filtered HTML" under sites/all/modules/htmlpurifier/config/, copy sample.php to filtered_html.php, and update the hook function name like this:

    function htmlpurifier_config_filtered_html($config) {
    

    The rest of the tutorial above worked for me, and now my preFilter and postFilter functions are being called.

    I thought you could do this through the UI, but it looks like the "Filters" custom configuration box is not evaluated as PHP.