Drupal - Changing Allowed HTML tags: @tags for help text input field

There isn't an easy way to do it really, the list of allowed tags is hard coded into the _field_filter_xss_allowed_tags() function.

This is in turn called by field_filter_xss(), not when the data is saved to the database but rather when it's displayed in field_default_form().

Fortunately there is a way but it'll take a small custom module to do it. You're basically looking to implement a form alter hook for the field's widget form and set the element's description to use filter_xss() with a more permissive set of tags.

Something along these lines:

// Implement hook_field_widget_form_alter()
function MYMODULE_field_widget_form_alter(&$element, &$form_state, &$context) {
  // If some condition is matched based on the element provided...
  if (isset($element[0]) && $element[0]['#field_name'] == 'field_test') {
    // Alter the description using your more permissive set of tags
    $element[0]['#description'] = filter_xss($context['instance']['description'], _MYMODULE_field_filter_xss_allowed_tags());
  }
}

// Provide a more permissive set of tags to be used with filter_xss()
function _MYMODULE_field_filter_xss_allowed_tags() {
  // Merge the new set of allowed tags with the less permissive defaults
  $new_tags = array('table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td');
  return array_merge(_field_filter_xss_allowed_tags(), $new_tags);
}

That's based on the premise that you only want to allow the new tags for an element called field_test, obviously if you want to do the same for other fields you'll need to change it to match a different condition. I advise using the excellent dpm() function included in the Devel module to inspect object in your form alter function.

I've just tested the above and it works (I now have a table where my file field description would normally be), it feels a bit like a hack even though it technically sticks to the rules but hopefully it'll get you on the way.


In case you are using D6, you can navigate to /admin/settings/filter. There you need to click the "configure" option of "Filtered HTML". Then click on the configure tab. you will be presented with a text box labeled "Allowed HTML tags". Enter the html tags that you need to use there.

In case of D7 go to admin/config/content/formats/filtered_html. Under Filter Settings, click on "Limit allowed HTML tags" tab. Enter the HTML tags you want to use in the text box.