Don't show widget select box if there's only one selection

webchick - October 12, 2009 - 04:00
Project:Drupal
Version:7.x-dev
Component:field_ui.module
Category:task
Priority:normal
Assigned:zserno
Status:needs work
Issue tags:D7 UX freeze, JavaScript, Needs usability review, Novice
Description

Every time I end up clicking this to see what other wonderful widgets might be in store, and each time I am terribly disappointed to see the answer is none.

Let's not tease people. If there aren't multiple selections, let's remove the select box.

No.

I have a feeling this might (!) be easy to solve, so tagging as novice.

#1

webchick - October 12, 2009 - 04:01
Issue tags:+D7 UX freeze

Coining a tag.

#2

quicksketch - October 12, 2009 - 04:05
Issue tags:+JavaScript

All that needs to be done here is something like this (pseudo-code):

if ($('option:visible', element).size() == 1) {
  $(element).hide()
}

The solution should probably be JavaScript-only.

#3

dmitrig01 - October 12, 2009 - 08:16
Status:active» needs review
AttachmentSizeStatusTest resultOperations
601952.patch2.49 KBIdlePassed: 14415 passes, 0 fails, 0 exceptionsView details | Re-test

#4

dmitrig01 - October 12, 2009 - 08:21

Left a little bit of debug code in and now it works for existing fields as well

AttachmentSizeStatusTest resultOperations
601952.patch3.38 KBIdlePassed: 14427 passes, 0 fails, 0 exceptionsView details | Re-test

#5

System Message - October 15, 2009 - 09:50
Status:needs review» needs work

The last submitted patch failed testing.

#6

MichaelCole - October 18, 2009 - 21:28
Status:needs work» reviewed & tested by the community

IMHO, the testbot is incorrectly failing this patch.
The file is JS. It shouldn't be tested for PHP syntax.
See this issue: http://drupal.org/node/608044

Here's how I tested this patch:
- Applied patch - success
- Code review of patch - looking good. Could use a comment.
- Manual testing:
. create new content type "test"
. added boolean field (shows dropdown) - success
. added image field (doesn't show) - success
. created new content of type "test" and uploaded image - success

Looks good. Ship it!

#7

Dries - October 19, 2009 - 02:06

I'm not (yet) convinced that should be worked around using Javascript. It doesn't optimize the behavior of people who don't have Javascript.

#8

yched - October 23, 2009 - 13:19

re Dries #7:
People without JS are out of this anyway, because they will always get a 'Select widget' dropbox filled with *all* available widgets for all field types (selecting an invalid 'field type / widget' pair then comes back with a form error). Restricting the list of widgets to the ones available for the selected field type is already done through JS (obviously).

#9

yched - October 23, 2009 - 13:25

Regarding patch in #4, I don't think I see why the two .end() calls are needed, but I might be missing it.
Also, is *hiding* the select really what we want ? Simply disabling it might be less confusing.

#10

yched - October 23, 2009 - 14:32
Status:reviewed & tested by the community» needs work

Also : It would be best to commit #578544: field creation widgets in different columns from existing fields first. This fixes a trivial WTF on colspans which should make this patch clearer (there's actually no need to mess with the 'operations' column here).

Thus, back to CNW

#11

yched - October 24, 2009 - 17:34

#12

zserno - November 23, 2009 - 23:08
Status:needs work» needs review

Reworked patch from #4. Please review.
Changes:
- Got rid of code that messes with the operations column as suggested in #10.
- Instead of simply hiding select lists that hold only one option, it shows their only option as text (see attached jpg). Note: I tried to simply use the disabled attribute, but it shows no visual difference in my (mac) browsers (FF 3.5, Safari 4).
- Added some comments.

I'm a bit concerned though about the copy/paste nature of this code. Is it acceptable here? I would gladly introduce a new function to avoid copy/paste programming.

AttachmentSizeStatusTest resultOperations
601952_dont_show_widget_select_box_12.patch1.87 KBIdlePassed on all environments.View details | Re-test
601952_dont_show_widget_select_box_12.jpg70.07 KBIgnoredNoneNone

#13

yched - November 24, 2009 - 11:54

"Text only" doesn't look too great, it appears misaligned, and I'm not sure we can find a set of CSS margins that can make it look aligned in all browsers.
It's a shame OSX doesn't make any visual distinction between enabled and disabled selects - win does ;-)

Dunno, maybe a disabled textfield, then ? Might require some work in field.js - so maybe this should be validated first with a hacked sreenshot by modifying the HTML in firebug...

#14

yched - November 24, 2009 - 11:55

Also, any solution we come up with, we'll need to make sure the form submit callback handles the case where it receives no value for the widget.

#15

zserno - November 24, 2009 - 15:15

Ok, found what causes this weird behavior: if you change style of a select list then OS X browsers will use this new style even for disabled elements. So simply changing color to gray helps a lot. See:

I re-rolled the patch to use the disabled attribute on widget select boxes that hold only one option. Please try. Although I'm not sure if this problem appears only on this form thus changes made to field_ui.css might need to be moved to a higher level.

AttachmentSizeStatusTest resultOperations
601952_dont_show_widget_select_box_15.patch2.46 KBIdlePassed on all environments.View details | Re-test

#16

yched - November 27, 2009 - 12:53
Status:needs review» needs work

Cool ! Approach looks fine, now code review:

+++ modules/field_ui/field_ui.css 24 Nov 2009 15:01:45 -0000
@@ -16,3 +16,7 @@
+  color: GrayText;

Hmm, I think we use real #rgb in core.

+++ modules/field_ui/field_ui.js 24 Nov 2009 15:01:46 -0000
@@ -24,7 +24,14 @@ function attachUpdateSelects(context) {
+      // Don't show widget select box if there's only one selection.
+      if (this.targetSelect.fieldPopulateOptions(options).find('option').size() > 1) {
+        this.targetSelect.removeAttr('disabled');
+      }
+      else {
+        this.targetSelect.attr('disabled', 'disabled');
+      }

2 occurrences - I think this could be moved inside fieldPopulateOptions().
+ comment is not to date with the current approach (disable, not "don't show")

+++ modules/field_ui/field_ui.js 24 Nov 2009 15:01:46 -0000
@@ -48,12 +55,26 @@ function attachUpdateSelects(context) {
+  // Make sure that disabled select box values get submitted too.
+  $('#field-ui-field-overview-form', context).submit(function () {
+    $('#field-overview select[disabled]').removeAttr('disabled');
+    return true;
+  });

Sounds like a hack.
+ Won't this cause a visual blip between the moment you hit Submit and the moment the new page loads ?

I'd rather have the case where there's no 'widget type' in the incoming form values dealt with in field_ui's form handlers.

Something like :

if (!isset($form_state['values']['_add_new_field']['widget_type']) {
  $widget_type = the only valid widget type;
  form_set_value($form['_add_new_field']['widget_type'], $widget_type);
}

in _field_ui_field_overview_form_validate_add_new() - and similar in _field_ui_field_overview_form_validate_add_existing()

I'm on crack. Are you, too?

#17

zserno - November 27, 2009 - 13:04
Assigned to:Anonymous» zserno

@yched: Thank you for your thorough review, I'll re-roll the patch according to your remarks.

#18

zserno - November 28, 2009 - 10:54
Status:needs work» needs review

Rerolled the patch according to yched's remarks in #16.
Please review and test it.

AttachmentSizeStatusTest resultOperations
601952_dont_show_widget_select_box_18.patch4.13 KBIdlePassed on all environments.View details | Re-test

#19

yched - November 30, 2009 - 19:30
Status:needs review» needs work

+++ modules/field_ui/field_ui.admin.inc 28 Nov 2009 10:33:02 -0000
@@ -402,13 +402,20 @@ function _field_ui_field_overview_form_v
+      // If there's only one possible widget type, use that.

Please make it clear that it relates to the JS feature of disabling the select if there is only one valid widget type.

+++ modules/field_ui/field_ui.js 28 Nov 2009 10:33:02 -0000
@@ -59,9 +59,18 @@ function attachUpdateSelects(context) {
+    var counter=0;
+
+    for (i in options) {
+      counter++;
+    }

Hm - can't we do better than this ? Why doesn't options.length work ?
+ the code comment "Disable widget select box if selection is obvious" doesn't really apply to the (already in HEAD) case where there is 0 option to select because no field type has been selected yet.

Other than that, code looks good to me. We'd need a corresponding test in Field UI, though.

This review is powered by Dreditor.

 
 

Drupal is a registered trademark of Dries Buytaert.