form_builder() applies the following defaults to all form elements:

  // Assign basic defaults common for all form elements.
  $element += array(
    '#required' => FALSE,
    '#attributes' => array(),
    '#title_display' => 'before',
  );

The default for #attributes is a wonky serious and weird problem, when using trying to render themeable elements within the context of a form, like this:

  $form['links']['#theme'] = 'links';
  $form['links']['#links']['foo'] = array(...);

Due to form_builder()'s default for #attributes, theme_links() no longer applies the default theme variable value for 'attributes', as defined in drupal_common_theme() for theme_links().

Comments

sun’s picture

sun’s picture

Status: Active » Needs review
StatusFileSize
new391 bytes

Status: Needs review » Needs work

The last submitted patch, drupal8.form-attributes-default.2.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new1.38 KB

I expected more, so if that's all...

Status: Needs review » Needs work

The last submitted patch, drupal8.form-attributes-default.4.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
Issue tags: +API change
StatusFileSize
new2.83 KB

Re-rolled against HEAD and fixed the test failures.

jacine’s picture

We actually ran into a similar issue with this (I think), when trying to use a different class for the wrapper of contextual links vs. the links inside of it. There seemed to be no way to use a different class for the wrapper without hardcoding it with #prefix and #suffix, which sucks. So... you can set both #theme_wrappers and #theme, but in the situation where they both share the #attributes argument (or any other maybe any argument I guess), you run into a problem where those will end up being shared, regardless. Maybe that is a separate issue or I was doing something wrong though?

Due to form_builder()'s default for #attributes, theme_links() no longer applies the default theme variable value for 'attributes', as defined in drupal_common_theme() for theme_links().

I guess what I am asking is... Is this problem specific to form_builder() and how does this really solve anything?

+++ b/core/includes/form.incundefined
@@ -3230,7 +3229,12 @@ function theme_tableselect($variables) {
+    'attributes' => isset($element['#attributes']) ? $element['#attributes'] : array(),

Is this something that will have to be done everywhere? Because that would be a tad annoying.

sun’s picture

Yes, the problem is specific to form_builder(), because of the #attributes default value being applied here:

+++ b/core/includes/form.inc
@@ -1725,7 +1725,6 @@ function form_builder($form_id, &$element, &$form_state) {
   // Assign basic defaults common for all form elements.
   $element += array(
     '#required' => FALSE,
-    '#attributes' => array(),
     '#title_display' => 'before',
   );
@@ -3230,7 +3229,12 @@ function theme_tableselect($variables) {
+ 'attributes' => isset($element['#attributes']) ? $element['#attributes'] : array(),

Is this something that will have to be done everywhere? Because that would be a tad annoying.

Most affected functions are only element-specific Form API #process functions; i.e., not in the theme layer.

As kinda visible through this patch, only in theme functions that take a render element in the first place and that actively try to process or do something special with the passed in #attributes, which is not very usual.

And of course, this change kinda highlights how intertwined theme functions can be. The fact that theme_tableselect() relied on #attributes being set means that it wasn't really used outside of a form.

sun’s picture

StatusFileSize
new2.88 KB

Re-rolled against HEAD.

sun’s picture

Issue tags: -API change, -API clean-up

Status: Needs review » Needs work

The last submitted patch, 9: drupal8.form-attributes-default.9.patch, failed testing.

mgifford’s picture

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

This still a concern in D8? Unassigned issue too.

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.

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.

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.

quietone’s picture

Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

8 years it was asked if this is still relevant. There hasn't been a reply which suggests this is outdated. So, one last check on that.

Is this still relevant for Drupal 10+?

Since we need more information to move forward with this issue, I am setting the status to Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

quietone’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

It has been 5 months and there has been no confirmation that this is still a problem. Therefore closing.