[development] Programmatically update allowed_html filter settings

Bob Hutchinson hutchlists at midwales.com
Tue Mar 13 10:43:15 UTC 2012

On 13/03/12 07:28, Cameron B. Prince wrote:
> Hi,
> The baseline module I'm developing needs to add the<span>  tag to the
> allowed_html value for the filtered_html text filter when enabled.
> I've searched the core modules, the standard installation profile,
> Drupal.org and Google, but I haven't been able to find an example of the
> proper way to do this in a module.install's hook_enable().
> I assume I should use filter_format_save() and tried the following:
> filter_format_save('filtered_html', 'filter_html', 1, 1, array(
>    'settings' =>  array(
>      'allowed_html' =>  '<a>  <em>  <strong>  <cite>  <blockquote>  <code>  <ul>
> <ol>  <li>  <dl>  <dt>  <dd>  <span>'
>      )
>    )
> );
> This resulted in "PDOException: SQLSTATE[23000]: Integrity constraint
> violation: 1048 Column 'format' cannot be null" which I don't understand
> because the first argument to filter_format_save() is the format.
> Could someone point me in the right direction?

I don't know of an all-in example that would 'just work' but there are a 
few things you can do.

First of all filter_format_save() expects just one parameter, not five, 
and that parameter should be an object.

Secondly you cannot make the assumption that there is a format 
'filtered_html', it might have been deleted so you will need to check 
that it exists and is enabled. There are functions in filter.module that 
can help.

The examples module has a filter_example.module that might help you 
implement hook_filter_info().

Once you have got hold of the existing 'settings' string, you should use 
explode() to turn it into an array, then use in_array() to check if what 
you are wanting to add is already there or not, add it if not and turn 
it back into a string with implode() ready for saving (or not if it was 
already there).

I also see in the code for filter_format_save() that there is a module 
hook 'filter_format_update' so you probably should use that, eg create a 
function in your module in the form of 'mymodule_filter_format_update()' 
and feed the object into that.

The structure of the object is described in the comments above 

This should be enough to get you started.

> Thanks,
> Cameron

Bob Hutchinson
Midwales dot com

More information about the development mailing list