5

I'm using Drupal 7, for which the standard length of an input text field is 60. Is there any way to shorten this for ALL fields at once? I want all the forms on my site to be no wider than 40.

I know that hook_form_alter() can be used to do this one-by-one, but I have a rather large number of fields on my site, and I would like to edit their lengths all at once if possible.

7

Pretty much all textfields on a Drupal site should have a "form-text" class attached to them so you could set the size/width limitation via CSS: input.form-text {max-width: 250px;}. Adding a quote from w3schools to that:

Since the size attribute is a visual design attribute, it is recommended to use CSS instead.

  • Thanks-- this did the trick. I also tried the theme override described above, but it didn't pick up the fields from the location module, which is probably non-standard (still in dev). I really appreciate both of your answers! – Patrick Kenny Feb 7 '12 at 8:16
8

There are a couple of things you can do depending on whether you want to do this on a theme level or site-wide level (i.e. independent of theme).

On the theme level you can override theme_textfield() and set the size there:

function MYTHEME_textfield($variables) {
  $new_size = 40;
  $variables['element']['#attributes']['size'] = $new_size;

  // EDITED per kiamlaluno's comment regarding infinite loops
  return theme_textfield(array('element' => $variables['element']));
}

On a site-wide level you can write a small module and implement hook_element_info_alter() to change the default that's provided by system_element_info():

function MYMODULE_element_info_alter(&$type) {
  $new_size = 40;
  $type['textfield']['#size'] = $new_size;
}

You'll want to clear Drupal's cache once you've implemented either of these methods to clear the relevant registry.

It's worth noting that if you use the first method any specific sizes for textfields set by other modules (e.g. size 10 for numeric fields) will be overwritten with your new size. If you use the second method that will become the default but other modules will still be able to provide a specific size for specific textfields.

EDIT

I've just had another thought on this, probably the best way is to do it is in your theme function and check the existing length. That would match your statement "I want all the forms on my site to be no wider than 40" a bit better:

function MYTHEME_textfield($variables) {
  $new_size = 40;
  if (isset($variables['element']['#attributes']['size']) && $variables['element']['#attributes']['size'] > $new_size) {
    $variables['element']['#attributes']['size'] = $new_size;
  }

  // EDITED per kiamlaluno's comment regarding infinite loops
  return theme_textfield(array('element' => $variables['element']));
}
  • 2
    Would not the call to theme('textfield') cause any infinite loop? Isn't it supposed to call MYTHEME_textfield(), which then calls theme('textfield') again? – kiamlaluno Feb 5 '12 at 18:58
  • @kiamlaluno You're right of course :) I've edited the code, the theme_textfield() function should be called directly to avoid that recursion – Clive Feb 5 '12 at 19:06

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.