it is possible to change the form element id through #attributes, but the label does not respect that.

example:
form element:

  $form['test'] = array(
    '#title' => 'Test',
    '#type' => 'checkbox',
    '#attributes' => array('id' => 'my-checkbox'),
  );

rendered:

  <input type="checkbox" class="form-checkbox" value="1" name="test" id="my-checkbox">
  <label for="edit-test" class="option">Test </label>

problem:
Info: reference to non-existent ID "edit-test"

possible solution:
this code needs to be improved in form.inc

function theme_form_element_label() {
  ...
  if (!empty($element['#id'])) {
    $attributes['for'] = $element['#id'];
  }
  ...
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

maciej.zgadzaj’s picture

Status: Active » Fixed

I see this already fixed in the most recent 7.x-dev from 2012-09-14.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

dawitti’s picture

Version: 7.14 » 7.x-dev
Status: Closed (fixed) » Needs review
FileSize
558 bytes

I just tested this on the current (as of Oct 11, 2012) dev version and the issue is not fixed.

The reason why this happens is because form_builder() sets $element['#id'] regardless of $element['#attributes']['id'].
Later in the forms processing theme_form_element_label() uses $element['#id']to set the label's for attribute value.

But the theme functions for textfields, selects,... ( e.g. theme_textfield() ) only use $variables['element']['#attributes']['id']

So we can have to different id values that are being used in different places.

In order to prevent different id attributes we synchronise $element['#id'] and $element['#attributes']['id'] in form_builder().

According to the current Forms Api documentation:

#id
Description: Internal. Used to populate form elements' id property. In rare cases, you can set this value yourself on a form element, to override the default setting.

...we should not use #id, so we let $element['#attributes']['id'] override $element['#id'] if set.

Old code:

if (!isset($element['#id'])) {
  $element['#id'] = drupal_html_id('edit-' . implode('-', $element['#parents']));
}

New code:

if (isset($element['#attributes']['id'])) {
  $element['#id'] = $element['#attributes']['id'];
}
elseif (!isset($element['#id'])) {
  $element['#id'] = drupal_html_id('edit-' . implode('-', $element['#parents']));
}
dawitti’s picture

Title: Wrong reference id in form element label » Wrong html attribute id in form element label using $el['#attributes']['id']
dawitti’s picture

Assigned: Unassigned » dawitti
JulienD’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7
FileSize
615 bytes

Like @dawitti I just tested on the latest D7 dev and it's still there.

This issue is also available for Drupal 8, so tagging as D8.

@dawitti, as marked in the comment "In rare cases, you can set this value yourself on a form element", why do you force the override of $element['#id'] if it is already set ?

Status: Needs review » Needs work

The last submitted patch, drupal-fix_form_element_ids-1679284-6.patch, failed testing.

jaffaralia’s picture

Status: Needs work » Needs review
FileSize
648 bytes

Here i corrected that patch..

Status: Needs review » Needs work

The last submitted patch, drupal-fix_form_element_ids-1679284-6.patch, failed testing.

JulienD’s picture

What did you corrected ? Have you found a case where $element['#id'] were not properly set ?

According to the first check on $element['#id'] a line above, the NULL value could not be assigned to $element['#id'] in the second ternary.

if (!isset($element['#id'])) {
  $element['#id'] = isset($element['#attributes']['id']) ? drupal_html_id($element['#attributes']['id']) : !isset($element['#id']) ? drupal_html_id('edit-' . implode('-', $element['#parents'])) : NULL;
}
jaffaralia’s picture

Your patch #6 is correct.
The form element rendered correctly. Still finding why its fails

mgifford’s picture

Version: 8.0.x-dev » 8.1.x-dev
Assigned: dawitti » Unassigned
Issue summary: View changes

This still a concern in D8? Unassigned issue too.

David_Rothstein’s picture

Title: Wrong html attribute id in form element label using $el['#attributes']['id'] » $element['#id'] should respect $element['#attributes']['id'] if it's set, since that is often used to specify the HTML ID
Priority: Minor » Normal
Related issues: +#974502: Replace $element['#id'] with $element['#attributes']['id']

I'm making the title more general, given what the patch is trying to accomplish. (There are other parts of Drupal that make decisions based on #id too.)

Not sure if there are other places in the codebase this would need to be changed as well.

David_Rothstein’s picture

-    $element['#id'] = drupal_html_id('edit-' . implode('-', $element['#parents']));
+    $element['#id'] = isset($element['#attributes']['id']) ? drupal_html_id($element['#attributes']['id']) : drupal_html_id('edit-' . implode('-', $element['#parents']));

I would think the first drupal_html_id() call shouldn't be there - if ['#attributes']['id'] is set shouldn't that just be used as is?

bogdan.racz’s picture

Is this really referring to 8.1.x? I only find the issue in Drupal 7, shouldn't the version of the issue be changed to 7.x-dev?
Regarding the drupal_html_id() removal in patch #6, I've attached a new one for 7.x-dev. But a question remains ... what if, in a custom module, the ID is erroneously misspelt and it doesn't conform to the standard?

Status: Needs review » Needs work

The last submitted patch, 15: drupal-fix_form_element_ids-1679284-15.patch, failed testing.

bogdan.racz’s picture

Issue tags: +SprintWeekend2016
rakesh.gectcr’s picture

Version: 8.1.x-dev » 7.x-dev
Status: Needs work » Needs review
rakesh.gectcr’s picture

I am not able find this issue any more with Drupal 8.x So I changed to Drupal 7.x-dev,

bogdan.racz’s picture

Issue tags: +SprintWeekendTM
David_Rothstein’s picture

Version: 7.x-dev » 8.1.x-dev

But a question remains ... what if, in a custom module, the ID is erroneously misspelt and it doesn't conform to the standard?

Then Drupal will probably output an invalid HTML ID. But isn't that what it will do already in that case? I'm not sure it's related to this issue, or that the code should try to change an ID that was specifically set.

I am not able find this issue any more with Drupal 8.x

For Drupal 8 this code is in https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormBuilder....

bogdan.racz’s picture

Assigned: Unassigned » bogdan.racz
Status: Needs review » Active
bogdan.racz’s picture

@David_Rothstein thanks for pointing out where the problem was.
I've attached a patch for 8.1.x

bogdan.racz’s picture

Assigned: bogdan.racz » Unassigned
Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 23: drupal-fix_form_element_ids-1679284-23.patch, failed testing.

bogdan.racz’s picture

Assigned: Unassigned » bogdan.racz
Status: Needs work » Active
bogdan.racz’s picture

Assigned: bogdan.racz » Unassigned
Status: Active » Needs review
FileSize
967 bytes

I've attached a new patch, to fix the failing tests.

The problem that I found, is that in some places the ['#attributes']['id'] is an array, like bellow:

$element['#attributes']['id'] = array('views-display-top');

in renderDisplayTop() in ViewsEditForm.php
Is this ok?

dawehner’s picture

I'm wondering whether the alternative implementation could be to reference $element['#id'] from $element['#attributes']['id']
so multiple code paths changing it would not just ignore each other.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ocastle’s picture

Patch 27 works for me on Drupal 8.8.1

ocastle’s picture

Upon further review, this doesn't work when used with Ajax. The label 'for' attribute is regenerated but the input is left as defined.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
160 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.