http://diveintohtml5.org/forms.html#autofocus

We should be able to add $form['myelement']['#autofocus'] = TRUE;

This would also need a JavaScript backup solution, or we just say if your browser doesn't support it, then too bad.

Comments

jacine’s picture

I vote if your browser doesn't support it too bad. I commented about this here: #90324-37: Usability: Autofocus textfields.

dave reid’s picture

Yeah that makes sense to me rather than adding a browser-dependent JS fallback.

ericduran’s picture

with 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.

ericduran’s picture

StatusFileSize
new1.12 KB

Here'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

ericduran’s picture

Status: Active » Needs review
jacine’s picture

Title: Support the #autofocus FAPI property for native HTML5 autofocus support » Allow FAPI usage of the autofocus attribute

it'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.

joachim’s picture

StatusFileSize
new796 bytes

Patch 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.

ericduran’s picture

Issue tags: +Needs tests

we should replace the isset and the check for True with an empty.

Also tagin for test.

joachim’s picture

Really?

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?

ericduran’s picture

@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.

joachim’s picture

> 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 :(

Everett Zufelt’s picture

Does 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?

jacine’s picture

Status: Needs review » Closed (won't fix)
Issue tags: -Needs tests

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?

Not sure. I tried using 2 in this validator and did not get an error, but according to the spec:

There must not be more than one element in the document with the autofocus attribute specified.

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.

mgifford’s picture

Issue summary: View changes
Status: Closed (won't fix) » Active
Related issues: +#2096347: Add "autofocus" attribute to the module search

Sorry, 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

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +stale-issue-cleanup

Thank 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!

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Since it's been 3+ months without a follow up going to close out. If still a valid issue please please re-open!

Thanks all!