Closed (outdated)
Project:
Drupal core
Version:
11.x-dev
Component:
forms system
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
1 Jun 2011 at 02:38 UTC
Updated:
24 Jun 2025 at 13:50 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
jacineI vote if your browser doesn't support it too bad. I commented about this here: #90324-37: Usability: Autofocus textfields.
Comment #2
dave reidYeah that makes sense to me rather than adding a browser-dependent JS fallback.
Comment #3
ericduran commentedwith this one, we're going to run into the same issue as #1174938: Natively support the HTML5 required and aria-required FAPI properties which is the #property currently can't support booleans, because if the value is FALSE, they shouldn't be printed on the page.
If we can agree that the solution on #1174938: Natively support the HTML5 required and aria-required FAPI properties works for supporting boolean on any #property then we can submit a patch for this issue rather easily.
Comment #4
ericduran commentedHere's a patch for this.
One thing I would like to bring up is that we should have a more generic implementation that can support any boolean attribute.
In other words we need something that will print the element when is true, but not printed at all when is false.
Boolean attribute explanation over at http://www.w3.org/TR/html5/common-microsyntaxes.html#boolean-attribute
Comment #5
ericduran commentedComment #6
jacineit's been brought to my attention that the issue title implies full support with polyfills and all (where needed), so I'm changing it to better reflect what it's for.
Comment #7
joachim commentedPatch works perfectly for me -- I tried setting #autofocus on node titles which is perhaps a nifty enhancement we should consider in a feature request.
Rerolling it for fuzz, and also because I think the comment doesn't need to be that long.
Comment #8
ericduran commentedwe should replace the isset and the check for True with an empty.
Also tagin for test.
Comment #9
joachim commentedReally?
What would we like developers to be able to use to set this?
'#autofocus' => TRUE, // obviously
'#autofocus' => 1, // handy to have too
'#autofocus' => 'I forgot to feed the cat', // why not
'#autofocus' => FALSE, // this is just silly
Admittedly, autofocus="false" in the HTML will work.
But I'm thinking of DX when it comes to setting defaults and form altering: it's easier and nicer to say '#autofocus' = FALSE than to unset it.
Another thing to consider is uniqueness. The spec says:
> There must not be more than one element in the document with the autofocus attribute specified.
Should we check that only one element has this?
Comment #10
ericduran commented@joachim, I just figured empty would simplify it, but it doesn't really matter to me. Also '#autofocus' => FALSE will not print, when checked against empty it will return false. So autofocus="false" will never be printed. Also you will notice we're always printing autofocus="autofocus" which is explain on the initial patch with all the comments that were removed ;-p
So if we do switch it to empty you will still be able to do '#autofocus' = FALSE and have the expected behavior. Also if someone happens to do '#autofocus' => 'I forgot to feed the cat', well then thats there problem not ours. ;)
All in all, is really just semantics, the thing we do need is tests.
Comment #11
joachim commented> Also you will notice we're always printing autofocus="autofocus" which is explain on the initial patch with all the comments that were removed ;-p
Oh. Duh. Sorry. Brain not in gear :(
Comment #12
Everett Zufelt commentedDoes fapi offer a way to ensure that a property (e.g. #autofocus) is used only once per page load? What about when there are two forms on a page and one is retrieved from cache, but the other is not?
Is having autofocus once per page a normative requirement of the spec, or non-normative? I.e. does having two instances of autofocus on a page cause the validator to raise an error?
Comment #13
jacineNot sure. I tried using 2 in this validator and did not get an error, but according to the spec:
I don't see how FAPI could possibly ensure that there is only one per page. Per form, sure, but per page...? I don't know. Given that #90324: Usability: Autofocus textfields has been marked wont fix, I think we should do the same here and not pursue this further. If anyone needs to add this, they can do so using #attributes.
Comment #14
mgiffordSorry, but isn't this in FAPI now? It's used here #2096347: Add "autofocus" attribute to the module search
Just need to add an html5 attribute here:
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h...
Right? Does provide JS for those browsers that don't support this..
Just seeking autofocus clarification for D8. It's already in use in /user
Comment #28
smustgrave commentedThank you for sharing your idea for improving Drupal.
We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #29
smustgrave commentedSince it's been 3+ months without a follow up going to close out. If still a valid issue please please re-open!
Thanks all!