Home

Lullabot

Lullabot Ideas

We know stuff. We empower you to know stuff too.

Drupal Module Development Deep Dive Week

London, UK
September 20-24, 2010

What if a field title ends with a question mark?

Article by Jeff RobbinsOctober 27, 2006 - 8:26pm

Isn't it kind of annoying that there is a colon right after the 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;
}
?>
Sorry about the weird output. Some of the lines are a bit long.

Comments

Rob Cottingham (not verified) on October 28, 2006 - 12:05am

Ah, bless you

That's one of my pet peeves solved. Thanks!

Amitai (not verified) on October 28, 2006 - 6:30am

A good one

I like the idea of the post - since every little thing is important in the web design.

October 28, 2006 - 2:38pm Jeff Robbins

Updated

I found a little bug in the code. Fixed now.

jakeg (not verified) on October 29, 2006 - 5:06am

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.

Jim (not verified) on October 29, 2006 - 12:21pm

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. ;)

October 29, 2006 - 5:17pm Jeff Robbins

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.

budda (not verified) on October 31, 2006 - 7:05pm

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.

October 31, 2006 - 9:10pm Jeff Robbins

There you go!

I knew there was a case somewhere. That's right.

Anonymous (not verified) on December 5, 2006 - 8:38am

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.

Jim (not verified) on October 30, 2006 - 8:52pm

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.

M. Droste (not verified) on December 2, 2006 - 8:19am

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

Barry Jaspan (not verified) on December 8, 2006 - 1:12am

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

December 8, 2006 - 8:07am Jeff Robbins

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!

Barry Jaspan (not verified) on December 8, 2006 - 8:46am

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

Maxferrario (not verified) on April 8, 2008 - 12:02pm

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

lalyn (not verified) on June 4, 2009 - 4:15am

how about in drupal6?

they don't have for version6? It's nice if they have.

thanks....It's nice!

About this 'bot

Jeff Robbins

Jeff Robbins is co-founder and CEO of Lullabot. He worked at O'Reilly as an illustrator and systems administrator as the World Wide Web came into being. He was involved in the early stages of the first commercial website, O'Reilly's Global Network Navigator, but left to start one of the first web development companies...

more

Recent

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Drupal Voices 158: Emma Jane Hogbin on PHP for Designers

Podcast 8.31.2010

Command Line Basics: More Editing with Vi/Vim

Video 8.31.2010

Lullabot's Back to School Sale

Blog 8.30.2010

Popular

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Photo galleries with Views Attach

Article 6.01.2009

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Announcing BeautyTips, a jQuery Tooltip Plugin

Article 10.20.2008

Install a Local Web Server on Ubuntu

Video 11.14.2007
 
  • Home
  • Services
  • Events
  • Ideas
  • Store

Connect the Bots:

Twitter Facebook YouTube blip.tv All Posts Newsletter
  • Ideas
  • Blog
  • Podcasts
  • Videos
  • About
  • Contact
  • Jobs
  • Services
    • Training
  • Events
    • Training Workshops
    • Other Events
    • Conferences
    • Calendar
  • Products
    • Videos
    • Books
    • Swag
  • Ideas
    • Blog
    • Podcast
    • Videos
  • About
    • Philosophy
    • Team
    • Presskit
  • Contact
    • General
    • Work Inquiries
    • Mailing List