views:

52

answers:

2

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.

+2  A: 

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.

Erik Ahlswede
Are you sure hook_form_alter can be called at the theme level?
Mike Crittenden
Looked into that. I usually break out customizations into custom modules, so I never tried adding at the theme level.It doesn't look like you can. Thanks for checking that
Erik Ahlswede
Alright, I did the following, please note I'm a total noob with Drupal:1. I created a module called "longerfield" in sites/all/modules. 2. I created one file called "longerfield.module" and a file called "longerfield.info".3. I got the code to parse with the following for longerfield.module:function longerfield_form_alter( }}You stated: you will also need a "longerfield.module" file, which is were you include the call. I don't get the last comment?
Slinger Jansen
Thanks up to now! Sorry, my newlines don't seem to be included in my comment... :-(
Slinger Jansen
Basically my question is what you mean by "You will also need a MODULENAME.module file, which is where you will include your hook_form_alter call."
Slinger Jansen
Since I already have a modulename.module file with the code in it.
Slinger Jansen
You've already created the .module file with the form_alter in it.Did you enable the 'longerfield' module in your Drupal site?
Erik Ahlswede
Yes I have enabled the longerfield module. But the real question is where I need to place the actual function call. I've placed the full method in the .module file, but do I need to call the method somewhere as well? Or will it "magically" do that by default? And if it doesn't, how do I test? I'm RTFMing as well, I'll update you if I find anything.
Slinger Jansen
I'm testing. Hang on, I've got some stuff on my screen ;-) (in the screen where I want to change the field)
Slinger Jansen
Ah, I figured something interesting out. When I do: print_r($form) the field that I'm looking for is not in there, and probably for a good reason. When the form is called for the first time, it has a browse button. Once I have browsed and uploaded a picture, it shows me a URL field (id="edit-field-page-boeken-0-data-url"), but that's probably the reason why I am not seeing anything happening... Any other tips? I think I miss either the right hook, or I need to find the place where URLs are set to be 255 by default. Silly exceptions...
Slinger Jansen
You don't need to call the function, just define it. Because it is a 'hook', Drupal will take care of calling it.
Erik Ahlswede
I'm not sure what you mean by your last comment. I'm not sure what you mean by the upload button. Can you post a screenshot?
Erik Ahlswede
I fixed it! I'll formulate my answer here.
Slinger Jansen
A: 

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!

Slinger Jansen