1

I'm having a whole mess of issues adding text fields to a form in Drupal 6. I want to add a text field with an autocomplete. I tried adding the field in hook_form_alter() like so:

$form['my_field_name'] = array(
  '#type' => 'textfield',
  '#title' => 'My Title',
  '#required' => true,
  '#default_value' => 'my_default_value',
  '#autocomplete_path' => 'url/to/autocomplete'
);
$output .= drupal_render($form['my_field_name']);

When I output this, the field output to html does not have an id or name attribute, and the autocomplete doesn't work (presumably because the id is missing. Additionally, the default value is not populated in the field.

I can overcome some of these issues by adding in a custom #id, #name, and #value, like so:

$form['my_field_name'] = array(
  '#type' => 'textfield',
  '#title' => 'My Title',
  '#id' => 'my_field_id',
  '#name' => 'my_field_name',
  '#required' => true,
  '#value' => 'my_default_value',
  '#autocomplete_path' => 'url/to/autocomplete'
);
$output .= drupal_render($form['my_field_name']);

This gets the autocomplete working as expected, gives a name and id attribute, and fills in the default value, but unfortunately now the autocomplete does not actually change the value of the field, so I can't enter and submit a new value.

How can I overcome these problems with form_alter?

  • Could you paste your entire hook_form_alter function? It's not clear what is going on with the output variable, or the function declaration. – Mikey P Sep 6 '11 at 21:20
1

AFAIK, hook_form_alter() does not require you to perform a drupal_render() in it. And I suspect that is why you are not getting a proper rendered for item.

Also, each custom field needs to processed (by you) on form submit. Thus, you need to add $form['#submit'][] = 'my_submit_function_submit';, where my_submit_function_submit() handles the database inserts.

  • Thanks, this was basically the issue, as I later discovered that I wasn't actually using hook_form_alter, but hook_node_form. When I moved the custom fields into hook_form_alter and the rendering into hook_node_form, that resolved the issue. – Travis Sep 7 '11 at 17:39
0

It's not very clear what you are doing with the $output variable since the return value from hook_form_alter() isn't used anyway. zerolab is correct that you should not be using drupal_render, you're just adding elements to the form, and the form will be rendered as a whole all at once after it has been fetched, altered, built, etc.

While I can't see what's going on, I wonder if you're not passing the $form argument by reference, and that is why you added the drupal_render to try to make your element show up, make sure that the signature for your function looks like this (note the & symbols):

function hook_form_alter(&$form, &$form_state, $form_id) {

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.