Community Documentation

Change the size of a CCK input field

Last updated August 25, 2009. Created by LinL on October 23, 2007.
Edited by lisarex, NonProfit, bekasu, LeeHunter. Log in to edit this page.

Overriding the default width of a CCK input field is best done within a custom module. Use the hook_form_alter() function to target your form's ID. If you don't know the ID of the form you want, use <?php print($form_id); ?> at the top of your custom module, and Drupal will show you the ID of all the forms it knows about. Now that you have the form ID, use form_alter to change the #size element of your CCK field:

<?php
function nameOfYourModuleGoesHere_form_alter($form_id, &$form) {
  if (
$form_id == 'idOfYourFormGoesHere') {
   
$form['nameOfYourCCKfieldGroup']['nameOfYourCCKinputField']['#size'] = 40;
  }
}
?>

Use <?php print_r($form); ?> if you need to see those CCKfieldGroup names or CCKinputField names.

Setting the #size attribute of the form field in this way is better than using CSS to override the width of a form's input class, since CSS won't target the "file" fields in a form due to browser security restrictions. This form_alter method has the added bonus of working on forms when CSS support is disabled, such as some of those early handheld browsers. Enjoy!

For the Zen Theme

Add this to your sub-theme template.tpl.php:

<?php
function zen_file($element) {
 
_form_set_class($element, array('form-file'));
  return
theme('form_element', $element, '<input type="file" name="'. $element['#name'] .'"'. ($element['#attributes'] ? ' '. drupal_attributes($element['#attributes']) : '') .' id="'. $element['#id'] .'" size=\"40\"' ."\" />\n");
}
?>

Implementation Notes

This works: $form['nameOfYourCCKfieldGroup']['nameOfYourCCKinputField'][0]['value']['#size'] = 20; BUT only after increasing the weight of the module to 10. CCK's Fieldgroup module runs at a weight of 9 and this change was being ignored until it ran AFTER the Fieldgroup module.

Also be aware that even if this is working the css classes hide the change: visually nothing seems to have happened. One must deal with ".node-form .form-text" css classes too.

About this page

Audience
Designers/themers

Theming Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.