Want to get Lullabot article, videocast, and podcast announcements delivered right to your in-box?
Let us know your email address (we won't share it) and we'll let you know when anything exciting happens.
What if a field title ends with a question mark?
I know this one has been bothering Matt for a long time. He's even submitted a core patch for it. But nothing has gotten in yet due to translation problems.
However I ran into this problem again while putting together our new contact form (using the amazing Webform Module). I decided to solve it in our theme and thought others might want to use this trick.
By copying the theme_form_element() function from the theme.inc and pasting it into our template.php file, we can do a little checking to see if the form element title ends with a punctuation character. And if so, suppress the trailing colon.
Here's what it looks like for Drupal 4.7. I'm guessing it'll be pretty similar, if not completely the same for Drupal 5:
<?php
/**
* Rewrite of theme_form_element() to suppress ":" if the title ends with a punctuation mark.
*/
function phptemplate_form_element($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE) {
$output = '<div class="form-item">'."\n";
$required = $required ? '<span class="form-required" title="'. t('This field is required.') .'">*</span>' : '';
if ($title) {
// I've added the next two lines
$punctuation = array(',', '.', '?', '!', ':');
$colon = in_array($title[strlen($title)-1], $punctuation) ? '' : ':';
if ($id) {
// I've modified this next bit
$output .= ' <label for="'. form_clean_id($id) .'">'
. t('%title%colon %required', array('%title' => $title, '%required' => $required, '%colon' => $colon))
. "</label>\n";
}
else {
// and this one too
$output .= ' <label>'
. t('%title%colon %required', array('%title' => $title, '%required' => $required, '%colon' => $colon))
. "</label>\n";
}
}
$output .= " $value\n";
if ($description) {
$output .= ' <div class="description">'. $description ."</div>\n";
}
$output .= "</div>\n";
return $output;
}
?>Comments on this post will automatically be closed three months from the original post date.



RSS Feed



Comments
Ah, bless you
That's one of my pet peeves solved. Thanks!
A good one
I like the idea of the post - since every little thing is important in the web design.
Updated
I found a little bug in the code. Fixed now.
Its not just me!
Its not just me then. I coded a similar fix a few months ago. Would be great to see this in the Drupal core code as well though.
Another slant on this
Well this is quite cool. Thanks.
I realize that you are simply providing an example of the php that enables the capacity to kill a trailing punctuation mark ("Can you tell us..."), but as an IA and a copywriter, I couldn't resist adding this note:
It's not very good practice to present a binary question (that is, one phrased in a way that solicits "yes" or "no" responses) when you are actually seeking a narrative response.
"Can you tell us a little bit about yourself?" is a binary (or maybe a multiple choice) question. To conform with the input you desire (presumably a narrative, or a list), you might better present the lead-in as a "kind directive," like "Please tell us a bit about yourself."
Interestingly (to me anyway), even then the trailing colon would need to go.
I would like to see the colon entirely removed from the core, allowing the form author to add punctuation, or not, as appropriate.
I'm sure this is way more than you care about. ;)
Good Case
You make a good case for removing the colon altogether. I wonder if/how/when there would be a case for the titles being presented without the colon... such that there might be a case for not just sticking them (when appropriate) into the title text. I can't think of one.
Error message output
One case would be outputting the field name in the validation error text. You wouldn't want the field names with colons in the $message.
There you go!
I knew there was a case somewhere. That's right.
Couldn’t the form
Couldn't the form generator just do a quick check to see if there is already some punctuation at the end of the field, and then only if there is no punctuation put the colon?
However, I still don't think the colon is necessary, the fact that there is a text box is enough to prompt the user to enter their data.
Hard to make the case for retaining the colon
I figure that the colon was simply, yet incorrectly, presumed to be the universal need, and therefore included in the core. This happened, I suppose, because developers are neither information architects nor copywriters… a corollary, Jeff, to your axiom about developers not being designers.
Long lines of code
Sorry about the weird output. Some of the lines are a bit long.
Simple question:why? I saw this problem on many Drupal sites. (drupal.org).
Is it so hard to avoid this?
Please take a look at my site:http://drupal.mdwp.de
Long lines of code don't run out of the container.
Best regards
Meinolf
An alternative solution
I finally got annoyed enough at this problem today to solve it as well. After reading this article, I decided to use a slightly different approach.
The short form (for Drupal 5, though 4.7 is similar):
function phptemplate_form_element($element, $value) {$output = theme_form_element($element, $value);
return preg_replace('@([.!?]):\s*(</label>)@i', '$1$2', $output);
}
The long form:
http://jaspan.com/removing-unwanted-colons-form-field-titles
Regex!
Barry,
This is a great example of how much more efficient a regular expression can be. The only argument I can come up with for my method is that it only parses the field title, rather than the whole form element. But I think if I did it again, I'd probably do it your way.
That's just sexy code!
A slight improvement.
Glad you like it! I improved the code slightly so it works for both Drupal 4.7 and 5:
function phptemplate_form_element() {$args = func_get_args();
return preg_replace('@([.!?]):\s*(</label>)@i', '$1$2',
call_user_func_array('theme_form_element', $args));
}
Thanks,
Barry
A small modification
To account for mandatory fields I slightly modified your code as follows:
function phptemplate_form_element($element, $value) {$output = theme_form_element($element, $value);
return preg_replace('@([.!?]):\s*(<span.*</span>){0,1}(</label>)@i', '$1$2$3', $output);
}
The mandatory fields (at least in webform-5.x-1.10.tar.gz) have, between the double column and the </label> a <span>...</span> block that adds the mandatory field marker (usually an asterisk).
So I added the subpattern (<span.*</span>) in the regex. And to make this work even for non mandatory fields I used the quantifier {0,1} .
Hope this helps
how about in drupal6?
they don't have for version6? It's nice if they have.
thanks....It's nice!