I just created the first poll on my web site and was annoyed by having a colon appear after the question mark in the block title, like this:
I remembered that Jeff Robins at Lullabot published an article about how to prevent this problem, but his solution involved copying and modifying the entire theme_form_element function.
In Drupal as with Perl, there is always more than one way to do it. My solution also overrides theme_form_element(), but is far simpler:
function phptemplate_form_element() {
$args = func_get_args();
return preg_replace('@([.!?]):\s*(</label>)@i', '$1$2',
call_user_func_array('theme_form_element', $args));
}It uses a regular expression to find and remove any colon preceeded by punctuation and followed only by whitespace before the </label>. By using func_get_args() and call_user_func_array(), it also avoids having to know anything about the argument list for theme_form_element(). This code should work for both Drupal 4.7 and 5, though I've only tested it with 5.
The above code snippet does not work for required form elements using Drupal 5's default theme because the requiredness-indicator appears in an extra <span> tag between the colon and the </label>. Here is a revised version that works in both cases:
function phptemplate_form_element() {
$args = func_get_args();
return preg_replace('@([.!?]):\s*(<span.*?>.*?</span>)?\s*(</label>)@i', '$1$2$3',
call_user_func_array('theme_form_element', $args));
}
Comments
It's worth noting the core
It's worth noting the core patch for this issue: http://drupal.org/node/67211
I didn't know about that;
I didn't know about that; thanks. However, it's in the 6.x-dev queue and I wanted a solution now. :-)
A nice one, thanks ! The 6th
A nice one, thanks !
The 6th version is still months away, so i would say that as well as a patch as Barry's solution are to be applied.
There are so many developers working on the same problems, so when one developer finds a solution,
the other one may repeat this job without knowing it.
Fantastic little bit of
Fantastic little bit of code! Worked like a charm.
Darn it, Barry, I just came
Darn it, Barry, I just came to post a solution for the required fields and find this update. :)
The Compact Forms module purports to remove trailing colons. I don't think it works yet.
cheers mate, just what i was
cheers mate, just what i was looking for
Hi there! Excuse the silly
Hi there!
Excuse the silly question, but where exactly should this code go? I tried putting it at the bottom of template.php, but the colon is still there.
Any advice you could provide would be greatly appreciated!
Dave
template.php is the correct
template.php is the correct place to put it. If it is not working, use my contact form to send me a snippet of HTML form your site containing a form element with a colon that should have been removed.
nice code. i like this!
nice code. i like this!
I think I've finally found
I think I've finally found someone who cares as much about punctuation and grammar as I do! :D
Excellent. Thankyou! Using
Excellent. Thankyou! Using this to add .classes to theme_form_elements will finally make my forms presentable!
Many thanks for this, Barry.
Many thanks for this, Barry. A compact and elegant solution to an annoying problem. I took this a step further: I don't want any colons or red asterisks on my field labels. The following seems to do the trick:
preg_replace('@(\w):\s*(<span.*?>.*?</span>)?\s*(</label>)@i', '$1$3',call_user_func_array('theme_form_element', $args));
or to remove all colons but keep red asterisks:
preg_replace('@(\w):\s*(<span.*?>.*?</span>)?\s*(</label>)@i', '$1$2$3',call_user_func_array('theme_form_element', $args));
By the way I've used these solutions with Drupal 6.
Post new comment