Problem/Motivation
Some field types (actually, most core fied types) have only one available widget type.
In the "add new field" / "add existing field" rows on "Manage fields" pages, users tend to click the "widget type" dropdown select, only to discover that the pre-selected value is the only one available :

Proposed resolution
In the JS code that handles populating the "widget types" dropdown according to the currently selected "field type", mark the dropdown as "disabled" if there's only one widget type available.
Remaining tasks
The patch includes some CSS rules to explicitly grey-out disabled dropdowns, which some browsers / OSs (OSX ?) apparently fail to do by themselves - see #12, #13, #15.
This sounds like something that would belong to drupal's base CSS rather than applied to those specific Field UI elements ?
User interface changes
On Field UI "Manage fields" pages (e.g. admin/structure/types/manage/page/fields), select "field type : File" in the "Add new field" row : the "widget type" select will get grayed out and disabled, displaying the pre-selected "File" widget.
Before : http://skitch.com/zserno/ne3a3/34ewrwef-localhost-1
After : http://skitch.com/zserno/ne32g/34ewrwef-localhost
API changes
None.
Original report by webchick
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.
I have a feeling this might (!) be easy to solve, so tagging as novice.
| Comment | File | Size | Author |
|---|---|---|---|
| #44 | widget-select-box-601952-44.patch | 4.82 KB | yched |
| #44 | disable_widget_selection.png | 19.2 KB | yched |
| #39 | widget-select-box-601952-39.patch | 5.2 KB | yched |
| #38 | widget-select-box-601952-38.patch | 3.05 KB | pillarsdotnet |
| #34 | 601952_dont_show_widget_select_box_34.patch | 2.37 KB | pillarsdotnet |
Comments
Comment #1
webchickCoining a tag.
Comment #2
quicksketchAll that needs to be done here is something like this (pseudo-code):
The solution should probably be JavaScript-only.
Comment #3
dmitrig01 commentedComment #4
dmitrig01 commentedLeft a little bit of debug code in and now it works for existing fields as well
Comment #6
MichaelCole commentedIMHO, 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!
Comment #7
dries commentedI'm not (yet) convinced that should be worked around using Javascript. It doesn't optimize the behavior of people who don't have Javascript.
Comment #8
yched commentedre 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).
Comment #9
yched commentedRegarding 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.
Comment #10
yched commentedAlso : 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
Comment #11
yched commented#578544: field creation widgets in different columns from existing fields has been committed.
Comment #12
zserno commentedReworked 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.
Comment #13
yched commented"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...
Comment #14
yched commentedAlso, 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.
Comment #15
zserno commentedOk, 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.
Comment #16
yched commentedCool ! Approach looks fine, now code review:
Hmm, I think we use real #rgb in core.
2 occurrences - I think this could be moved inside fieldPopulateOptions().
+ comment is not to date with the current approach (disable, not "don't show")
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 :
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?
Comment #17
zserno commented@yched: Thank you for your thorough review, I'll re-roll the patch according to your remarks.
Comment #18
zserno commentedRerolled the patch according to yched's remarks in #16.
Please review and test it.
Comment #19
yched commentedPlease make it clear that it relates to the JS feature of disabling the select if there is only one valid widget type.
Hm - can't we do better than this ? Why doesn't
options.lengthwork ?+ 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.
Comment #20
zserno commentedRerolled patch from #18 according to @yched's review.
Unfortunately .length property is 'undefined', so we can't use that. I spent quite some time on researching this question and this was the best solution I found. Now I'm really curious if there's any better way to do this. :)
About the corresponding test: could you please give me some hints about what this test should check?
Comment #22
zserno commentedFixed diff.
Comment #23
zserno commentedComment #24
asrobzserno, I got an error message when I applied your patch.
Comment #25
zserno commentedThanks asrob!
Attached is an updated patch, please test it.
Comment #26
asrobI reproduced again and it works. Thanks zserno.
Comment #27
asrobSorry, it works but needs more tests.
Comment #28
summit commentedHi, I have this feature request on Drupal6/Ubercart2 with selectbox of an attribute with only one option.
Will this method also work for that?
Greetings,
Martijn
Comment #29
FreekyMage commentedHi, I'm pretty new to this, but is it good if I try to write a test so this patch can get applied? I'm not sure if that's what asrob meant.
Comment #30
zserno commented@Summit I doubt it. That patch will not work with D6 for sure. However you can adjust it following the ideas behind it.
Comment #31
zserno commented@FreekyMage That would be fantastic. Here is yched's comment about the test from #19:
Comment #32
damien tournoud commentedComment #33
asrobI tested again but it seems to me that does not work.
Comment #34
pillarsdotnet commentedPatch in #25 no longer applies. Partial re-roll needs testing because I'm not sure if it's all there anymore.
Comment #35
pillarsdotnet commented#34: 601952_dont_show_widget_select_box_34.patch queued for re-testing.
Comment #36
LarsKramer commentedShouldn't this be moved to Drupal 8?
Comment #37
pillarsdotnet commentedComment #38
pillarsdotnet commentedRe-rolled, with trailing whitespace correction.
Comment #39
yched commented- Fixed flawed logic in _field_ui_field_overview_form_validate_add_new() (the $widget_types variable was tested in an "if" condiftion but defined only within the conditional branch)
- Added similar logic in the validate function for the "add existing field" row, which needed the same treatment
- Reworded comments a bit.
I'm not sure about the field_ui-specific CSS rule to explicitly grey out disabled select boxes. If some browsers need it, then this sounds like something that belongs to drupal's generic CSS, not just field_ui.
Comment #40
yched commentedAdjusting title for the current approach
Comment #40.0
yched commentedissue summary
Comment #41
yched commentedCreated issue summary
Comment #41.0
yched commentedoriginal report by webchick
Comment #42
yched commentedComment #43
Bojhan commentedWhere is the image?
Comment #44
yched commentedRerolled after the /core move.
+ @Bojhan : screenshot attached. The select in the 'widget' column is disabled if there is only one choice for the field type.
Comment #45
Bojhan commentedI am not really convinced this is a good improvement, although it sucks to have a one option dropdown. It sucks even more if people are confused whether something is wrong or not, because it looks disabled..
Comment #46
yched commented#45 feels like a won't fix.
Comment #46.0
yched commentedinlined the image