2

Correct me if I'm wrong, after reading drupal fapi related articles, I got the impression that fapi generates 'id' attributes by itself. It allows developers to assign 'name' attribute only. If that's the case, is there a way I can set desire 'id' value for elements? Because, I want my elements to have meaningful 'id' so that html/jquery code would be easier to read as well as save my time from going through already written jquery code to change those all 'id's that I've used inside.

P.S:drupal version - 6.x

googletorp
  • 33,075
  • 15
  • 67
  • 82
Andrew
  • 1,035
  • 7
  • 22
  • 40

4 Answers4

2

Ok found the solution. I can use the #attributes key of the $form element to set any additional attributes (such as class, id, etc.). Thanks for your help so far.

Agi Hammerthief
  • 2,114
  • 1
  • 22
  • 38
Andrew
  • 1,035
  • 7
  • 22
  • 40
1

It's true that you can set $element['#attributes']['id'] and that will apply to the form field. However, it will break labels and #states in Drupal 7 because the rest of the rendering pipeline reads the ID from somewhere else. So for your labels and #states to keep working, use set the ID to $element['#id'] instead (an undocumented property that nonetheless is how the form API watches ID internally).

Make sure to pass your ID through drupal_html_id as well to ensure no conflicts.

meustrus
  • 6,637
  • 5
  • 42
  • 53
1

I had a similar issue to deal with. I needed to have multiple forms on the same page so I had to change the ids of the form and its elements to prevent duplicate ids. I did something like the following:

function voci_comment_form($form, &$form_state, $cid) {
  $form['#attributes']['id'] = 'voci-comment-form-' . $cid;
  $form['#attributes']['class'][] = 'voci-comment-form';
  $form['body'] = array(
    '#title' => 'Post a comment',
    '#type' => 'textarea',
    '#resizable' => FALSE,
    '#rows' => 1,
  );
  $form['comment'] = array(
    '#type' => 'submit',
    '#value' => 'Comment',
  );

  foreach ($form as $k => &$element) {
    $k = str_replace('_', '-', $k);
    $element['#attributes']['id'] = "edit-$k-$cid";
    $element['#attributes']['class'][] = "edit-$k";
  }

  return $form;
}

This basically sets unique ids based on the $cid that is passed in. The code also adds classes to each element in the form so you can style it easily. I'm sure a more robust solution is possible but this is the basic idea. Tested in Drupal 7.

Venkat D.
  • 2,979
  • 35
  • 42
0

This problem doesn't really have much to do with the Drupal-FAPI itself, but more with how Drupal theme forms (create the markup).

  • If you want to alter all forms on your site, you can overwrite the theming functions that is used for forms and the different type of form fields.

  • If you just want to overwrite some forms or form fields, you can set the #theme attribute on the form or an element, to change which function should be used for creating the markup.

googletorp
  • 33,075
  • 15
  • 67
  • 82
  • Does it mean I can set my own Id attribute to form elements inside themeing functions? If that's the case, how can I refer to ID attribute of an element? I think I'm kind of lost here. Thanks a bunch. – Andrew May 14 '10 at 11:35
  • Ok found the solution. I can use attribute key of form element to set any additional attributes(such as class,id..). Thanks for your help so far. – Andrew May 14 '10 at 13:18