11

I want to display a simple table under a form field as part of the help text for that field (file field to be exact). Am I being totally mental or is there really no easy way of changing the allowed html tags for this text area? At the moment I have displayed:

Instructions to present to the user below this field on the editing form. Allowed HTML tags: a b big code del em i ins pre q small span strong sub sup tt ol ul li p br img

If there is no easy way, what is the next easiest way of doing it?

UPDATE:

Clive came up with a great way of doing it below with a custom module. You can also add to this using ctools to make the help text collapsible as follows:

// 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
    $reworked = filter_xss($context['instance']['description'], _MYMODULE_field_filter_xss_allowed_tags());
    $element[0]['#description'] = theme('ctools_collapsible', array('handle' => 'Help text', 'content' => $reworked, 'collapsed' => TRUE));
  }
}

// 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);
}
  • Are you talking about a text field (title of question) or file field (description of question). – googletorp Jan 27 '12 at 9:31
  • @7wonders What you added to the question would be good as answer. – kiamlaluno Mar 28 '12 at 18:07
7
+50

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.

  • It worked perfectly. Someone should definitely create a module to do this properly (how this cant be a core feature I don't know!). – 7wonders Jan 27 '12 at 18:32
  • @7wonders Yeah, definitely seems like an opportunity missed but I guess from the developers' points of view this would really have meant the field module maintaining its own text format and that might have been deemed overkill. A custom option to change it would have been nice though. I might turn this into a more useful module and post it on drupal.org actually, will keep you posted :) – Clive Jan 27 '12 at 18:35
  • Sounds like a good idea. Let me know when you got a sandbox up and I will happily test it for you. – 7wonders Jan 27 '12 at 19:13
2

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.

  • Im using D7 and trust me, that was the first thing I did and was shocked when it did not work! (yes I have cleared cache several times also). Its really baffling and seems to be the case on all help text fields for my site. Even if I add the allowed html filter to plain text and add the table tr td it does not work. Very annoying. – 7wonders Jan 24 '12 at 0:20
  • I just added a d7 fresh install and can confirm its default behaviour. It will not allow to change tags in the help section = madness! What hook can override this? – 7wonders Jan 24 '12 at 0:26
  • 1
    Try Setting "Full html" as a default input format and then check. – aerozeppelin Jan 24 '12 at 18:00
  • I can confirm that I was able to change the allowed tags for Filtered HTML with Drupal 7.10, and the filter worked as well as the 'allowed tags' display. So really aerozeppelin's answer is correct. :-) – paul-m Jan 29 '12 at 4:45
  • 1
    But if you read the question Paul it is in regards to the allowed tags in the help text for a field so the answer is not correct. – 7wonders Jan 29 '12 at 14:41

protected by kiamlaluno Mar 28 '12 at 16:48

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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