Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Let's add native #placeholder FAPI property to existing Form API fields.
The placeholder attribute is a visual gimmick which puts some text in a textual input field which disappears on first focus and if it's not edited before submit it does not get into POST, it's a sole visual gimmick.
Comment | File | Size | Author |
---|---|---|---|
#42 | 1174694-5-fapi-placeholder-property.patch | 5.02 KB | ericduran |
#39 | 1174694-39-fapi-placeholder-property.patch | 5.02 KB | linclark |
#36 | 1174694.patch | 5.01 KB | ericduran |
#35 | 865536-drupal_add_js-add-browser-options_0.patch | 4.88 KB | ericduran |
#31 | 865536-drupal_add_js-add-browser-options_0.patch | 4.91 KB | ericduran |
Comments
Comment #1
Dave ReidComment #2
ericduran CreditAttribution: ericduran commentedSimple enough,
Apply patch, modified a couple of forms and added #placeholder => 'Testing' to a couple of elements.
I saw my placeholder text in my browser.
Not much more to do after that.
Comment #3
Everett Zufelt CreditAttribution: Everett Zufelt commented+1
I'm pretty comfortable with us adding the ability to use this, so I am good with this patch. I would be much more hesitant to approve of a patch that was actually adding placeholders in Core without some accessibility testing.
Comment #4
yched CreditAttribution: yched commentedIn a followup issue, would be cool to add corresponding features to the relevant Field API widgets (textfield, textarea for text and number fields)
Comment #5
Dave ReidOoh that would be great.
Comment #6
ericduran CreditAttribution: ericduran commented@yched, Yea. I think we need a tickets for Field API widgets. Is came up in a couple of issues, I've been trying to stay away from them for now. :)
Comment #7
yched CreditAttribution: yched commented@ericduran : sure, those deserve separate, followup issues
Comment #8
ericduran CreditAttribution: ericduran commentedThis is probably the simplest patch out of all the html5 issues.
Getting this patch would be a huge win, mainly because it'll mean we got something in ;)
Comment #9
ericduran CreditAttribution: ericduran commentedComment #10
ericduran CreditAttribution: ericduran commentedadding tag.
Comment #11
Anonymous (not verified) CreditAttribution: Anonymous commentedAdded a test for the textfield.
If this is the right direction, we'll need to add a test for textarea too.
Comment #12
Anonymous (not verified) CreditAttribution: Anonymous commentedWhoops... this time with functionality included :P
Comment #13
ericduran CreditAttribution: ericduran commented/me adding password and textarea now.
Comment #14
ericduran CreditAttribution: ericduran commentedHere's an updated patch. Now is testing textfield, password and the textarea.
Comment #15
ericduran CreditAttribution: ericduran commented*needs whitespace cleanup.
Comment #16
aspilicious CreditAttribution: aspilicious commentedNewline issues, a few.
Powered by Dreditor.
Comment #17
ericduran CreditAttribution: ericduran commentedComment #18
ruplApplied the patch in #17. In my test module, all three elements (textfield, textarea, and password) look and behave correctly. Marking RTBC.
Comment #19
JacineTagging.
Comment #20
iflista CreditAttribution: iflista commented#17: 865536-drupal_add_js-add-browser-options.patch queued for re-testing.
Comment #21
iflista CreditAttribution: iflista commented#11: 1174694-10-fapi-placeholder-property.patch queued for re-testing.
Comment #22
iflista CreditAttribution: iflista commented#1: 1174694-fapi-placeholder-property.patch queued for re-testing.
Comment #23
iflista CreditAttribution: iflista commented#12: 1174694-11-fapi-placeholder-property.patch queued for re-testing.
Comment #24
iflista CreditAttribution: iflista commented#14: 1174694-11-fapi-placeholder-property-2.patch queued for re-testing.
Comment #25
ericduran CreditAttribution: ericduran commented@iflista, is there a reason you keep retesting? also no need to test the previous patches only the last one.
Comment #26
iflista CreditAttribution: iflista commentedSorry. I was trying to figure out what I have to do here. It was the first time I've applied retesting. If you have any info about testing and reviewing patches, I will be very appreciated. Thanks anyway, ericduran.
Comment #27
JacineThe "Now lets test the textarea" comment for this test could be improved. It would make it easier to read if there was a comment saying "Test the input element." and "Test the textarea element." at the top of each chunk of code.
There shouldn't be a dash in between place and holder here.
This is a total nitpick on code style, but could we please remove the blank line at the beginning of the function?
Other than those nitpicks, this looks good to me and will probably be the first patch that goes in, which is exciting! :D
Comment #28
JacineAlso, it's been brought to my attention that the issue name implies full support with polyfills and all where needed, so I'm changing the title of the issue to better reflect what it's for.
Comment #29
sunBetter title. Don't have to add anything to #27.
Comment #30
JacineThat title isn't better. It's not grammatically correct. :P
Comment #31
ericduran CreditAttribution: ericduran commentedEasy enough.
Comment #32
ericduran CreditAttribution: ericduran commentedremoving tags.
Comment #34
ericduran CreditAttribution: ericduran commentedlol, thats what happens when you don't create proper patch and try editing your-self. lol New patch coming right up.
Comment #35
ericduran CreditAttribution: ericduran commentedComment #36
ericduran CreditAttribution: ericduran commentedAttempt number 4 maybe 5
Comment #37
ericduran CreditAttribution: ericduran commentedComment #38
JacineUgh, @ericduran, I'm sorry I missed this last time around, but there are some minor coding style issues with this. I swear this is the last time. Once we fix these, this one is ready to go. Thanks for all the awesome work you've been doing! :D
The comment is longer than 80 chars, and should be wrapped.
There should be a space after the comma ('textfield', 'password').
Comment #39
Anonymous (not verified) CreditAttribution: Anonymous commentedslow morning, typo fix sounds like about my speed today ;)
Comment #40
JacineSweet! Thank you Lin! :D
Comment #41
sunNo t() in hook_menu().
RTBC afterwards.
16 days to next Drupal core point release.
Comment #42
ericduran CreditAttribution: ericduran commentedWe should probably open up a separate issue to remove all the t() from all those menu callbacks in the form_test.module.
Comment #43
sunSorry, one final issue:
What's the expected HTML escaping?
In general, Form API array values are expected to be escaped already:Let's confirm proper escaping in that test. (also no double-escaping)
16 days to next Drupal core point release.
Comment #44
sunSorry, I was distracted and totally confused. It's the other way around -- form API array values are escaped automatically.
Comment #45
sunerr, but anyway, let's confirm this in a test, please. :) oh my, sorry for the noise ;)
Comment #46
Dave ReidWe have got to have tests for drupal_attributes somewhere - I don't think we need to add any specific tests for that behavior here.
Comment #47
Dave ReidWe already have tests for this in: DrupalAttributesUnitTest and DrupalRenderTestCase. I don't see the need to add specific testing for this new attribute being escaped here.
Comment #48
sunSleeping over this once more, I have to play devil's advocate:
Why do we need a Form API #placeholder property for this in the first place?
vs.
Comment #49
Anonymous (not verified) CreditAttribution: Anonymous commentedI think it makes sense to expose this in FAPI as something that the developer can set.
This is an attribute that can only be used on form elements, unlike something like class which can be used on any HTML element. There is a precendent for having form element attributes, such as size, directly set through FAPI. IMHO, having a dedicated placeholder attribute is consistent with developer expectations.
Comment #50
webchickYeah, I agree there. We did the same with #disabled and that was really big for DX; it's no longer the 2nd most asked question in IRC back when it was when it was an attribute.
Comment #51
ericduran CreditAttribution: ericduran commentedAs mention above is all for DX. Besides that this is all technically possible right now, but this just makes it SO much easier.
Comment #52
Dave ReidAlso placeholder is not valid on all elements. Back to RTBC.
Comment #53
sunThere are solid chunks of code throughout Form API / form.inc to ensure that #disabled really means #disabled. Form API does not accept user input on a #disabled input field, even if you manage to hack some fake input into your POST data.
You may set
#attributes['disabled']
if you want to skip this logic and make a form input field only "disabled" in the UI, possibly "enabling" it via JavaScript.Likewise, properties like #maxlength are additionally validated on the server-side.
While I get the DX argument to some extent, there's also a prominent counter-example:
#id
and#attributes['id']
-- if you know the difference, then you likely know Form API pretty well. (and because of that, I'd say that's worse DX)However, that example actually isn't good, since as with #disabled, there is a difference between both. So let's look at this one instead:
This does not exist, even though it's one of the topmost use-cases throughout core and contrib.
But why doesn't it exist? Because there's no special logic, parsing, or security involved. Duplicating
#attributes['class']
into#class
would add nothing but confusion.Simply consider that you set
#attributes['class']
in your code and someone else sets#class
in their code. Who wins?Going back to the examples, the properties with actual meaning behave differently: A custom
#attributes['id']
overrides#id
(which is auto-generated), whereas#disabled
overrides whatever value in#attributes['disabled']
.For those special properties it makes sense to have internal logic tied to them. The system needs it to work.
Now, I can of course see the argument that "we're going to add special logic for #placeholder, too! But later.", which is sound.
However, applying any kind of special logic boils down to replacing
with:
Duplicating all possible #attributes into element #properties is definitely not going to be an improvement. Especially not in terms of DX.
Comment #54
Dave ReidAgain I would point to the fact that the placeholder attribute is valid only on a select number of elements - that fact makes it eligible for special consideration. Classes can be put on anything. Therefore it can go in #attributes. This is ready for D8 as is.
Comment #55
JacineI agree this is RTBC.
Having it in #attributes doesn't make sense to me. If this were a global attribute, it'd be a different story, but it's not.
Comment #56
sunI'd like to see some other Form API maintainer(s) chime in here.
The argument that the attribute only applies to certain element #types doesn't really hold from my perspective. At least, that should not be the primary reason for introducing a new type-specific #property. As explained above, the primary reason for introducing new #properties is special handling on the server-side.
Please don't get me wrong, I'm absolutely in favor of html5 improvements. However, at times, I also have to put on my Form API maintainer hat in order to ensure and retain sanity in the API. And it's with that hat being on, where this change doesn't really make sense from a technical perspective.
Comment #57
JacineIs this documented somewhere? I'd be curious to hear from other FAPI maintainers too.
I think what you are getting at here has nothing to do with HTML5 and is a totally separate issue. I'm not trying to say it has no merit; I just disagree in this case. There are other properties that do not have server side handling such as #rows, #cols and #src.
Comment #58
ericduran CreditAttribution: ericduran commentedHere's a couple of other things to consider from a Form api (not maintainer but at least an avid user ;-) ) perspective.
The #disabled property is a good example here because it actually does a couple of things. At is core it just sets $element['#attributes']['disabled'] = 'disabled'; or $element['#attributes']['readonly'] = 'readonly'; It does other things, but this is what people use the $element['#disabled'] property for. Right now nothing is stoping a user from doing $element['#attributes']['readonly'] = 'readonly'; or $element['#attributes']['disabled'] = 'disabled'; except no-body does it because is a lot simpler to do $element['#disabled'] and you know you'll get the same effect.
Essentially doing #placeholder is a DX improvement and it'll also helps clear up what elements have the property available. If I look at the http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.... I can easily tell that I shouldn't use '#cols' in a textfield even thought if I put it in $element['#attributes']['cols'] = 10, is going to show up on the html.
All in all, I guess we're going to required more feedback on this one.
Comment #59
webchick"As explained above, the primary reason for introducing new #properties is special handling on the server-side."
That's news to me, and I have been involved in most FAPI core discussions since the original push to put FAPI into Drupal core back in 4.7.
From a FAPI user perspective, this makes total sense as a top-level property. The fact that 4 other people are saying the same as fellow FAPI users ought to help sway the "formal" definition in favour of meeting user expectations, IMO, unless there's some compelling technical reason not to.
However, it sounds like this is on hold pending chx's review.
Comment #60
chx CreditAttribution: chx commentedSorry! I can't chime in meaningfully as I have no idea what a #placeholder is, I surmise it's something like #default_value but how it's different?
Edit: i wrote an issue summary based on an IRC chat.
Comment #61
Jacinechx now knows what the placeholder attribute is.
We are going to need to have better summaries on these issues going forward.
Comment #62
chx CreditAttribution: chx commentedHiding something in the dumping ground of the of #attributes when we already have a dumping ground on the top is no good practice. Please file a followup for #class it is a good idea!
Comment #63
JacineThis is still RTBC, just tagging to keep track of it, while we wait.
Comment #64
webchickDries gets back from vacation this week, and I have added this to his to-do list. :)
Comment #65
Dries CreditAttribution: Dries commentedI reviewed this patch and it looks good. Committed it to 8.x.
Comment #66
sunAny takers? Please link to it from here.
Comment #67
ericduran CreditAttribution: ericduran commented@sun sure I'll see if I can do that later. Question: how do we update the d8 form API reference to include the new attribute?
Comment #68
sunUnfortunately, the Form API reference guide is still buried in... nirvana, actually no idea. But if I'm not mistaken, "Needs Documentation" should make the right folks aware of this change.
Comment #69
Dave ReidCan we also consider this for backport to Drupal 7 as well?
Comment #70
yched CreditAttribution: yched commentedOpened #1241938: Add support for #placeholder to relevant Field API widgets as a followup.
Opinions / proposals welcome over there :-)
Comment #71
ericduran CreditAttribution: ericduran commented@sun #1244338: Add generic #class property for form elements "Please file a followup for #class it is a good idea!"
Comment #72
jhodgdonThe FAPI doc is now buried in the Documentation project. Could you possibly file an issue in the Documentation project with component "api documentation files" and explain what needs to be documented, and for which versions of Drupal?
Also, is this an API change -- if so, please create an API change node to document the change.
Comment #73
JacineRemoved sprint tags and unassigned Dave Reid. Still needs docs, but hopefully someone can jump in to help with that. This is a summary of what the patch does: http://jsfiddle.net/jacine/7HKXu/embedded/result/
@jhodgdon This my need a change notification too, but I'm not sure. Can you please let me know? There will be more like this in the future, so I'm hoping to get a better handle on this. It's not actually implemented on any form elements (and may or may not be in the future) but it's an API change that needs to be documented.
Comment #74
jhodgdonRE change notification: the guidelines are that if a module developer, themer, Coder module maintainer, Examples project maintainer, or user-facing doc writer would need to know about it, it needs to have a change notification.
And if it needs docs, can you please file separate docs issues explaining what docs are needed?
Comment #75
Anonymous (not verified) CreditAttribution: Anonymous commentedI have filed an issue in the docs queue about this, #1312016: FAPI: document #placeholder. I will work on getting that completed.
Documenting this has brought up some questions:
What about date... if the widget is a text field, does it make sense to have a placeholder? Probably another issue.Comment #76
jhodgdonSomeone I think still needs to create a change notification node for this issue:
http://drupal.org/node/add/changenotice
Comment #77
webchickTagging.
Comment #78
ericduran CreditAttribution: ericduran commentedHmm, Can someone explain to me what a changenotice is? lol I've never had to do one before.
Thanks.
Comment #79
ericduran CreditAttribution: ericduran commentedComment #80
jhodgdonericduran: see comment #76 above
Comment #81
Anonymous (not verified) CreditAttribution: Anonymous commented[#1315186]
Change notice created, I declare this fixed!
Comment #82
ericduran CreditAttribution: ericduran commentedComment #83
ericduran CreditAttribution: ericduran commentedComment #84.0
(not verified) CreditAttribution: commentedAdded an explanation.