I need to insert HTML markup (spans, classes, h2 tags). However, webform appears to be filtering out all markup from labels. Has anybody succeeded in inserting html markup in labels, and how did you do that?

  • 2
    Just a note - an h2 can't go inside a label, it's invalid HTML – Clive Feb 21 '14 at 12:36
  • See this question: How to theme a webform – Felix Eve Feb 21 '14 at 12:37
  • Hey Felix, using templates or preprocesses is of course an option. Though, I would really like to know how come the html is preserved in my client's webform labels, but not in mine. I pulled the code base thru git, and imported the db. Something really differs. – Alexei Rayu Feb 21 '14 at 13:05
  • Does it need to be in the label, or could you just use a Markup webform component? – Laryn - CEDC.org Oct 19 '14 at 5:00
  • Yes I ended up doing it as #markup. – Alexei Rayu Oct 21 '14 at 9:29

You won't be able to use HTML markup within the label inputs within the Webform module itself, as it most likely cleans out all HTML markup before displaying to the user to prevent XSS attacks etc.

Most likely you'll need to write a small custom module which implements hook_form_alter, which will allow you to change the markup used for the labels.

I'm not actually sure if Drupal will allow markup within the #title attribute of your form elements, so you might need to use the #prefix property instead to add your custom HTML replacement labels.

If this is all Greek to you then you'll probably need to checkout these tutorials:

Creating modules - a tutorial: Drupal 7.x

Hook snippets: hook_form_alter

  • My client has the same code base, that I pulled though git. And in his stage environment, the html in labels works. (Though xss javascripts are filtered). We share the same codebase. This is surrealistic. – Alexei Rayu Feb 21 '14 at 12:44
  • how are you setting the labels then? Via the Webform's GUI? – Alex Kirsten Feb 21 '14 at 12:49
  • I am setting labels via webform GUI. And html in labels works and shows in his dev environment but not in mine. We run the same code base, and we run the same php version. – Alexei Rayu Feb 21 '14 at 12:58
  • hmmm that's very strange. I can't think of any reason off hand for the different results. This method seems a bit hacky to me, I'd personally rather implement my method above so that your specials labels can be commited to your git repo too. – Alex Kirsten Feb 21 '14 at 13:06
  • Yes, this this very strange. I will keep diggind and post findings if I succeed. If not - then I will just use templates and preprocesses. – Alexei Rayu Feb 21 '14 at 13:08

the "Options with Markup" module allows HTML formatting of labels for radio button/checkbox.https://www.drupal.org/project/webform_optionsmarkup


I added the following to the bottom of my settings.php page. I read it somewhere... works like a charm. :)

$conf['webform_allowed_tags'] = array('a', 'em', 'strong', 'code', 'img', 'ul', 'li', 'u', 'span style="font-style:italic', 'span' , 'a', 'i', 'italic');  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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