Drupal 7.
I've got an (ECK module) entity form. The form's only field is a multi-value field of the field_collection (Field Collection module) type. The field collection contains two fields: a textarea, and a set of radio buttons. The user submits the form (AHAH), the entity is saved and the form is presented back to the user with the submitted values. For this project that is the intended (and necessary) behavior.
My (stripped down) validation handler is as follows:
/**
* My custom validation handler.
*/
function my_module_eck_entity_form_validate($form, &$form_state) {
$values = &$form_state['values'];
$lang = $values['entity']->language;
$fields = array('field_my_text', 'field_my_radios');
foreach ($fields as $field_name) {
// the values which get saved to the entity object
foreach ($values['field_my_field_collection'][$lang] as &$item) {
foreach ($item['entity']->{$field_name}[$lang] as &$child) {
$child['value'] = filter_xss($child['value']);
}
}
// the values which get output as part of the form on page-reload
foreach ($form_state['complete form']['field_my_field_collection'][$lang] as &$item) {
if (is_array($item) && isset($item[$field_name][$lang])) {
foreach ($item[$field_name][$lang] as &$child) {
if (is_array($child)) {
$child['value']['#value'] = filter_xss($child['value']['#value']);
}
}
}
}
}
}
It works and does what I want, but it is an ugly monstrosity that'll keep me up at night. There has got to be a nicer, cleaner way of doing this.
For the intents and purposes of this question, let's not discuss the pros & cons of Drupal's filter_xss()
vs other tools - plenty of existing questions about that.