开发者

Longer Form Fields in Drupal

I have a really silly problem that has cost me a load of time already.

开发者_运维技巧

I have created a content template with a URL in there. When I look at the HTML code for it, I see a big fat "maxlength=256" in the form tag. I'd like to expand the length of this field, because my customer wishes to enter really long links (over 500 characters). Any idea how I can change it? When I do a generic search through the code I see so many occurences of 256, but the length might just as well be in the database somewhere. I have of course made the database field a longer varchar (1024 sounded poetic to me), so that's something I don't have to worry about.

I think it's silly, but the customer's always right, as we know.

I am using Drupal 6.14.


You want to use a hook_form_alter() in your templete.php or a custom module.

It will look something like this:

MODULE_form_alter(&$form, &$form_state, $form_id) {
  if($form_id = 'name_of_form_you_want_to_alter') {
    form['name_of_url_field']['#maxlength'] = 500;
  }
}

Just replace MODULE with the name of your theme (if in template.php) or replace it with the name of the custom module your using.

To find the id of the form, inspect the element with firebug. Same goes for the id of the url field.

Let me know if you need more detail.

EDIT: As pointed out, it looks like you can't call hooks from the theme level.

The best way to go about this is to create a small custom module for you site. You can call it something like SITENAME_customizations.

All you need is a simple .info file named MODULENAME.info which will look something like this:

name = SITE customizations
description = "Customizations"

You will also need a MODULENAME.module file, which is where you will include your hook_form_alter call.

PS. Make sure that you don't close your php tag (?>) in your .module file.


Yahoooooo! I fixed it, thanks to the helpful Drupal pages: http://drupal.org/node/300705

I figured out I could edit the form after it has been generated completely. The solution presented by Erik is good, but doesn't appear to work for CCK fields. In my case Erik's solution could have worked if it wasn't for this generation step that needs to happen first.

My new code is as follows:

function longerfield_form_alter(&$form, &$form_state, $form_id) {
      $form['#after_build'][] = 'longerfield_after_build';
    }

function longerfield_after_build($form, &$form_state) {
  // This is for a node reference field:
  $form['field_page_boeken'][0]['data']['url']['#maxlength'] = 1024;
  return $form;
}

Now, I too see that it's ugly, especially because there might be other form elements here (just increment from 0), but it works for the first element! Yippeee!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜