Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Note: I had this problem with 'select' and 'textfield' form '#type' elements so I'm not sure if it applies to all.
In creating a ajax-enabled form element I have something like the following:
<?php
$element = array(
'#type' => 'textfield',
'#default_value' => '',
'#attributes' => array('id' => 'textfield_css_id'),
'#ajax' => array(
'wrapper' => 'ajax_update_css_id',
'callback' => 'MYMODULE_some_ajax_callback
),
);
The rest of my functions/callbacks are already in and working.
The ajax functionality works when the "#attribute => array('id' => 'SOMENAME')' is commented out.
The ajax functionality fails to work when the line remains.
I'm not sure if there is another thread on this but I wanted to point it.
Comment | File | Size | Author |
---|---|---|---|
#6 | drupal_duplicate-ids-ajax-processed_1268886-6-D7.patch | 712 bytes | scottalan |
Comments
Comment #1
rfayI confirm this. The JS code that sets up triggers seems to be dependent on the ID created by drupal_html_id().
Comment #2
alx_benjamin CreditAttribution: alx_benjamin commentedCan confirm the same.
Comment #3
alx_benjamin CreditAttribution: alx_benjamin commentedTo work around this I'd add a class to the ajax element and later get its id through the class:
Comment #4
stevenlafl CreditAttribution: stevenlafl commentedWhy is this still a fucking issue?
Comment #5
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedTo set a custom ID on a form element, you should use $element['#id'] rather than $element['#attributes']['id'].
Does that fix it? (It seems to for me.)
If so we should probably just close this issue since the #id vs. ['#attributes']['id'] thing is general to the form API (not specific to the Ajax system) and there are already other issues about making it less confusing:
#2582869: #attributes example in the form API reference should give clearer examples, including examples of when not to use it
#974502: Replace $element['#id'] with $element['#attributes']['id']
Comment #6
scottalan CreditAttribution: scottalan at Phase2 commentedI ran into a similar issue when dealing with links. Posting a patch here to keep this together until something better comes around.
This is just against 7.x-dev, for now.