Convert 'class' attribute to use an array, not a string

cha0s - October 27, 2008 - 08:08
Project:Drupal
Version:7.x-dev
Component:forms system
Category:task
Priority:normal
Assigned:Unassigned
Status:closed
Issue tags:class, form API
Description

$form['#attributes']['class'] is currently implemented as a string, but multiple classes are allowed, and that's currently solved by cat'ing onto the end of it ($form['#attributes']['class'] .= ' classname'), but this is bad. Since class in this context is actually a list of classes, we need to convert this element into an array.

I'm by no means familiar with 99.9% of the internal workings of Drupal 'cause I haven't looked at it yet. DamZ mentioned that we could do this in a backwards-compatible manner, using is_array(), and I'm down for that... but personally with something like this, I'd prefer to have a patch that would convert them all at once. Is this kosher? It would most likely be a huge patch. I just don't like the idea of adding cruft to the code.

Also, tests will have to be updated (...or written?) to test the class element working as it should.

#1

Damien Tournoud - October 27, 2008 - 09:20
Status:active» needs review

Here is a started patch.

AttachmentSizeStatusTest resultOperations
326539-array-attribute.patch881 bytesIdleFailed: Failed to apply patch.View details | Re-test

#2

chx - October 29, 2008 - 02:57

I do not like this patch, it affects an important function in a rather arbitrary manner. Why implode with a space...?

#3

cha0s - October 31, 2008 - 05:23

Here's a patch that leaves backwards compatibility in the dust... also I created a _drupal_attribute function to handle rendering individual attributes. That takes out the generalized "if array then implode with spaces" and targets that behavior to only the 'class' case.

AttachmentSizeStatusTest resultOperations
drupal.class_array.patch64.54 KBIdleFailed: Failed to apply patch.View details | Re-test

#4

Rob Loach - October 31, 2008 - 05:38
Status:needs review» needs work

Whoa, this is huge... Could you make it use both a string or an array to save on making changes everywhere?

#5

Rob Loach - October 31, 2008 - 15:44
Status:needs work» needs review

Cha0s, what are your thoughts on something like this....

AttachmentSizeStatusTest resultOperations
326539.patch722 bytesIdleFailed: Failed to apply patch.View details | Re-test

#6

cha0s - October 31, 2008 - 18:05

While I'm somewhat a supporter of backwards compatibility personally, as far as I know, the philosophy in Drupal is to change something and no leave cruft around to handle old behavior. Based on that impression I get, I don't think we should be supporting strings as classes.

For one thing, how could someone interfacing with your code add to/remove from your classes without string manipulation? That's extremely dirty; the reason I propose the change to begin with.

EDIT: The best solution I can think of to reconcile that behavior would be to add some rule to the Coder module to check for this, but from my experience of actually making the changes, that's probably going to raise a fair amount of false positives.

#7

chx - November 1, 2008 - 07:20

well, theme table comes to my mind as something that supports different data types. but then it's obviously obscenely complex.,, so I am on the fence alas whether i like or not this. It should be noted despite noone said in the isseu the patch actually reacts to my earlier comment and only suppprts arrays for classes so other attributes wont be imploded by a space (or anythign else).

#8

cha0s - November 2, 2008 - 08:22

Got a new patch rolled.

AttachmentSizeStatusTest resultOperations
drupal.class_array.patch78.22 KBIdleFailed: Invalid PHP syntax.View details | Re-test

#9

cha0s - November 2, 2008 - 22:52

... Rolled again to remove the function call.

AttachmentSizeStatusTest resultOperations
drupal.class_array.patch77.5 KBIdleFailed: Invalid PHP syntax.View details | Re-test

#10

Anonymous (not verified) - November 13, 2008 - 08:55
Status:needs review» needs work

The last submitted patch failed testing.

#11

ultimateboy - July 15, 2009 - 22:43
Assigned to:cha0s» ultimateboy
Status:needs work» needs review

Well.. the above patch is not even close to applying and now that #493746: Enhance drupal_attributes() for multiple valued values went in, I think it's time to bring this back onto the table. I kinda started from scratch with this patch.

AttachmentSizeStatusTest resultOperations
326539-11.patch25.03 KBIdleFailed: 11691 passes, 0 fails, 132 exceptionsView details | Re-test

#12

System Message - July 15, 2009 - 23:00
Status:needs review» needs work

The last submitted patch failed testing.

#13

ultimateboy - July 27, 2009 - 21:44
Status:needs work» duplicate

After talking with JohnAlbin in IRC and seeing his post http://drupal.org/node/493746#comment-1858392 I think everything covered by this patch will be consumed by the forthcoming patch that JohnAlbin is working on. There's really no need for this issue any more.

#14

JohnAlbin - July 27, 2009 - 22:07
Title:Convert $form['#attributes']['class'] to use an array, not a string» Convert 'class' attribute to use an array, not a string
Status:duplicate» needs work

Heh. Actually, I think it might be quicker to do this issue as a follow up to #493746: Enhance drupal_attributes() for multiple valued values. But let me paste my previous comment here:

Regarding the already-committed change to drupal_attributes() in #493746, it isn't going to help much unless we actually go through all of Drupal’s code and make all 'class' implementations into arrays.

Otherwise, at any given point, we won't know if 'class' is an array or a string. And we will just end up changing this awkward code:

<?php
   
if (isset($options['attributes']['class'])) {
     
$options['attributes']['class'] .= ' active';
    }
    else {
     
$options['attributes']['class'] = 'active';
    }
?>

with this awkward code:

<?php
   
if (!isset($options['attributes']['class']) || is_array($options['attributes']['class'])) {
     
$options['attributes']['class'][] = 'active';
    }
    else {
     
$options['attributes']['class'] = array($options['attributes']['class'], 'active');
    }
?>

Which, you may notice, is actually longer then the previous awkward code.

Here's my half-implemented patch merged with ultimateboy's patch. Its still not yet complete. I searched through D7's code base for 'class' and only got as far as changing stuff through profile.admin.inc. So there's more work to do.

AttachmentSizeStatusTest resultOperations
class-as-array-326539-14.patch72.75 KBIdleFailed: 11678 passes, 157 fails, 22929 exceptionsView details | Re-test

#15

JohnAlbin - July 28, 2009 - 07:33
Assigned to:ultimateboy» Anonymous
Status:needs work» needs review

I finished converting all the 'class' bits I could see. Let's see what the testbot has to say.

[edit: 38 fails, 22956 exceptions. Woot! ;-) ]

AttachmentSizeStatusTest resultOperations
class-as-array-326539-15.patch102.28 KBIdleFailed: 11786 passes, 38 fails, 22956 exceptionsView details | Re-test

#16

System Message - July 28, 2009 - 07:50
Status:needs review» needs work

The last submitted patch failed testing.

#17

JohnAlbin - July 28, 2009 - 09:05
Status:needs work» needs review

Another try.

[edit: 3 fails, 110 exceptions. Getting closer.]

AttachmentSizeStatusTest resultOperations
class-as-array-326539-17.patch102.37 KBIdleFailed: 11830 passes, 3 fails, 110 exceptionsView details | Re-test

#18

System Message - July 28, 2009 - 09:20
Status:needs review» needs work

The last submitted patch failed testing.

#19

JohnAlbin - July 28, 2009 - 11:22
Status:needs work» needs review

Another try. Also re-rolled after the comment rendering patch went in.

AttachmentSizeStatusTest resultOperations
class-as-array-326539-19.patch102 KBIdleFailed: Invalid PHP syntax in modules/comment/comment.module.View details | Re-test

#20

JohnAlbin - July 28, 2009 - 11:24

Whoops. parse error after re-roll from missing ).

AttachmentSizeStatusTest resultOperations
class-as-array-326539-20.patch102 KBIdleFailed: Failed to apply patch.View details | Re-test

#21

JohnAlbin - July 28, 2009 - 12:15

Success! Ok, who wants to review the 100k patch? :-D

It’s 90% wrapping strings in array()

#22

ultimateboy - July 28, 2009 - 16:23

Dont have time to wrap these changes into a patch.. but a quick review gives me the following. After reading every line of this patch, all I've got are a few code comment issues.. fantastic.

// This
- * $form['my_elements'][$region][$delta]['weight']['#attributes']['class'] = "my-elements-weight my-elements-weight-" . $region;
+ * $form['my_elements'][$region][$delta]['weight']['#attributes']['class'] = array('my-elements-weight my-elements-weight-' . $region);

// Should be
- * $form['my_elements'][$region][$delta]['weight']['#attributes']['class'] = "my-elements-weight my-elements-weight-" . $region;
+ * $form['my_elements'][$region][$delta]['weight']['#attributes']['class'] = array('my-elements-weight', 'my-elements-weight-' . $region);

Given how large this patch is, its probably best to get a few more eyes on it, but from my point of view, it is good to go.

[edit] I simply read the patch, didnt actually test it out. I have no idea if every instance of "class" was covered, but everything that is changed by this patch is good.

#23

JohnAlbin - July 28, 2009 - 16:37

Re-rolled with ultimateboy's comment change.

AttachmentSizeStatusTest resultOperations
class-as-array-326539-23.patch102.01 KBIdleFailed: Failed to apply patch.View details | Re-test

#24

System Message - July 29, 2009 - 16:55
Status:needs review» needs work

The last submitted patch failed testing.

#25

JohnAlbin - July 29, 2009 - 21:15
Status:needs work» needs review

Re-rolled.

AttachmentSizeStatusTest resultOperations
class-as-array-326539-25.patch102.01 KBIdleFailed: Failed to apply patch.View details | Re-test

#26

System Message - July 30, 2009 - 21:31
Status:needs review» needs work

The last submitted patch failed testing.

#27

ultimateboy - July 31, 2009 - 15:14
Status:needs work» needs review

Re-roll

AttachmentSizeStatusTest resultOperations
class-as-array-326539-27.patch102.37 KBIdleFailed: Failed to apply patch.View details | Re-test

#28

System Message - August 4, 2009 - 19:58
Status:needs review» needs work

The last submitted patch failed testing.

#29

JohnAlbin - August 6, 2009 - 06:43
Status:needs work» needs review

Please review while its still fresh! :-D

AttachmentSizeStatusTest resultOperations
class-as-array-326539-29.patch101.21 KBIdleFailed: Failed to apply patch.View details | Re-test

#30

ultimateboy - August 6, 2009 - 22:33

I re-looked over this and am happy.. but I really want another pair of eyes on this many changes.

#31

System Message - August 12, 2009 - 08:58
Status:needs review» needs work

The last submitted patch failed testing.

#32

Zarabadoo - August 19, 2009 - 23:17

I am subscribing to this.

#33

Rob Loach - August 19, 2009 - 23:39

Went through and found one...............

+++ includes/form.inc 6 Aug 2009 06:42:04 -0000
@@ -2440,13 +2434,7 @@ function theme_button($element) {
function theme_image_button($element) {
-  // Make sure not to overwrite classes.
-  if (isset($element['#attributes']['class'])) {
-    $element['#attributes']['class'] = 'form-' . $element['#button_type'] . ' ' . $element['#attributes']['class'];
-  }
-  else {
-    $element['#attributes']['class'] = 'form-' . $element['#button_type'];
-  }
+  $element['#attributes']['class'] = 'form-' . $element['#button_type'];

   return '<input type="image" name="' . $element['#name'] . '" ' .

Should that be $element['#attributes']['class'][] = 'form-' . $element['#button_type']; ?

Beer-o-mania starts in 12 days! Don't drink and patch.

#34

sun - August 20, 2009 - 01:21
Status:needs work» needs review

All I ever wanted.

Some hunks failed. Incorporated RobLoach's finding.

AttachmentSizeStatusTest resultOperations
drupal.class-array.patch101.16 KBIdleFailed: Failed to install HEAD.View details | Re-test

#35

yched - August 20, 2009 - 01:29

I'm afraid Field UI just added a couple more...

#36

sun - August 20, 2009 - 01:39

Additionally accounting for new code, especially the new code that has no tests yet (field_ui).

AttachmentSizeStatusTest resultOperations
drupal.class-array.35.patch105.64 KBIdleFailed: Invalid PHP syntax in modules/field_ui/field_ui.admin.inc.View details | Re-test

#37

Rob Loach - August 20, 2009 - 03:15

There was an extra end parameter in field_ui.admin.inc..........

+++ modules/field_ui/field_ui.admin.inc 20 Aug 2009 01:29:58 -0000
@@ -22,7 +22,7 @@ function field_ui_fields_list() {
       $rows[$field_name]['data'][0] = $field['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name;
       $rows[$field_name]['data'][1] = t($field_types[$field['type']]['label']);
       $rows[$field_name]['data'][2][] = l($bundles[$bundle]['label'], $admin_path . '/fields');
-      $rows[$field_name]['class'] = $field['locked'] ? 'menu-disabled' : '';
+      $rows[$field_name]['class'] = $field['locked'] ? array('menu-disabled') : array(''));
     }
   }

Errr, here!

AttachmentSizeStatusTest resultOperations
drupal-class-326539.patch97.67 KBIdleFailed: Failed to apply patch.View details | Re-test

#38

webchick - August 21, 2009 - 17:11

I think this is a good change, and would like to commit it. Is it possible to get one more review/sanity check?

#39

System Message - August 22, 2009 - 08:16
Status:needs review» needs work

The last submitted patch failed testing.

#40

sun - August 22, 2009 - 13:27
Status:needs work» reviewed & tested by the community

Re-rolled for new code.

And did another sanity check.

AttachmentSizeStatusTest resultOperations
drupal.class-array.40.patch105.45 KBIdlePassed: 11993 passes, 0 fails, 0 exceptionsView details | Re-test

#41

webchick - August 22, 2009 - 13:48
Status:reviewed & tested by the community» needs work

Needs another re-roll. :(

#42

sun - August 22, 2009 - 13:57
Status:needs work» reviewed & tested by the community

Re-rolled for new code + additional sanity check.

AttachmentSizeStatusTest resultOperations
drupal.class-array.42.patch106.03 KBIdlePassed: 11265 passes, 0 fails, 0 exceptionsView details | Re-test

#43

webchick - August 22, 2009 - 14:49
Status:reviewed & tested by the community» needs work
Issue tags:+Needs Documentation

Ok! Testing bot comes back green. This is a great improvement for themers. Committed to HEAD.

Please document this in the theme upgrade guide http://drupal.org/update/theme/6/7

#44

ultimateboy - August 22, 2009 - 15:41
Status:needs work» fixed

This is really module update and not theme. Themes cannot really take too much advantage of this at this time. This is more of a coding standards change in modules, so I updated: http://drupal.org/update/modules/6/7#class_attribute_array

#45

ultimateboy - August 22, 2009 - 15:41
Issue tags:-Needs Documentation

Tag

#46

webchick - August 22, 2009 - 15:45

Oops, my mistake. Thanks, ultimateboy!

#47

System Message - September 5, 2009 - 15:50
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.