2

I have fields called

  • 'title'
  • 'field_article_summary'

The HTML IDs of the form elements are

  • edit-title
  • edit-field-article-summary-und-0

Is there a way to programmatically get these HTML IDs? (without writing the code myself to do it - Drupal must have something that is creating them)

1
  • What are you trying to accomplish? There may be a Drupal method to accomplish what you need w/o explicitly needing the IDs.
    – mpdonadio
    Mar 26 '12 at 14:09
2

I know this is two years late but for anyone looking back at this, a way to get the element IDs is to use Drupal Form API's #after_build attribute to add a callback once the form has rendered.

$form['#after_build'] = array('mymodule_afterbuild'); // The callback can be called anything.

Then your function would look something like:

function mymodule_afterbuild(&$form, $form_state) {
  // Get the element ID.
  $id = $form['title']['#id'];
  // Then do something, such as passing it into Drupal settings to access with Javascript.
  drupal_add_js(array('mymodule_js_settings' => array('title_id' => $id)), 'setting');
  return $form;
}

Edit to say that the #after_build attribute can be used on individual elements in a form like so:

$form['title']['#after_build'] = array('mymodule_afterbuild');

This actually makes it easier to get the IDs, especially if you're using fieldsets, field collections or other complex field data structures.

1
  • 1
    This is one clever method to do this, though you could use #attached to add the settings.
    – AKS
    Jul 15 '14 at 18:30
1

Drupal uses the following two functions to prepare HTML classes and IDs:

  • drupal_html_class(), for generating a valid class name for an element
  • drupal_html_id(), for generating a valid ID for an element. This method also guarantees that the returned ID is unique.
6
  • drupal_html_id takes an id as an argument. I don't see how that helps me get the id from the drupal form element. Mar 26 '12 at 12:58
  • Drupal runs all elements through drupal_html_id() to get their ID...it's a one-way function though as there's an internal counter used to differentiate between duplicates. If you want the ID that has already been generated for a specific form element you'll need to check the element with something similar to this: $id = $form['element']['#attributes']['id'];.
    – Clive
    Mar 26 '12 at 13:02
  • thanks for your help. When I use $form['element']['#attributes']['id'] (or $form['element']['id'] or any variation) it is not set - so hence I need to get it programmatically. I have had a look at the source code for forms, and unfortunately it does not look to me like there is a single piece of code that does this, hence they may not be a 'good' way. Mar 26 '12 at 13:16
  • Ah I see, yeah that's going to be quite tricky I think :/
    – Clive
    Mar 26 '12 at 13:34
  • I think I can work around it by manually setting the IDs. Is that bad? Is there a good reason not to do that? Mar 26 '12 at 13:36

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.