Problem/Motivation
The documented required variable is missing from the form-element.html.twig template variables.
The problem applies to any form with required fields (e.g. core forms, webform). The variable does not exist for non-required fields or required fields, so there is no indicator if the wrapped element is required or not.
Example: The classy template file is /core/themes/classy/templates/form/form-element.html.twig and the documentation for that shows the required variable:
required: The required marker, or empty if the associated form element is not required.
Steps to reproduce
- Find a form with a required field
- Print the
requiredvariable for form fields within the theme template file - Variable will be empty for required fields
- Expected: Variable will be set for required fields
Proposed resolution
Debug why the required variable is not set and fix the problem.
Remaining tasks
- Debug
- Create patch
- Review patch
- Test patch
- Commit :)
User interface changes
API changes
Data model changes
Release notes snippet
Original report by @thomas.frobieter
Context:
https://api.drupal.org/api/drupal/core%21themes%21classy%21templates%21f...
says:
- "required: The required marker, or empty if the associated form element is"
Problem:
The documented "required" key / value is completely missing in the template variables of "form-element.html.twig". The problem applies to all kind of forms with required fields, core forms, webform, ...
It's neither existing für non-required fields nor for required fields. So there's no indicator at this point if the wrapped element is required or not.
The used template file is "/core/themes/classy/templates/form/form-element.html.twig".
Proposed solution:
Check why "required" flag variable is not set and correct it to be always set true / false.
| Comment | File | Size | Author |
|---|---|---|---|
| #50 | 2928521-50.patch | 5.55 KB | majid.ali |
| #47 | 2928521-47.patch | 9.64 KB | hchonov |
| #39 | interdiff_2928521_32-39.txt | 5.26 KB | andregp |
| #39 | 2928521-39-test-only.patch | 5.79 KB | andregp |
| #39 | 2928521-39.patch | 13.74 KB | andregp |
Issue fork drupal-2928521
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
thomas.frobieterComment #3
anybodyComment #4
anybodyComment #5
anybodyComment #7
bdlangton commentedHi Thomas,
The required field is passed through the form element theme and can be accessed in twig by:
{{ element['#required'] }}. Let us know if that fixes it for you.Comment #8
thomas.frobieterHi,
yes, thank you! You're right, but it's mentioned as direct available variable in the file comments:
https://api.drupal.org/api/drupal/core%21themes%21classy%21templates%21f...
* required: The required marker, or empty if the associated form element is not required.
Comment #9
bdlangton commentedYou're right. I can submit a patch a little later this weekend so it'll match the documentation. It would be nice to have it as a direct variable.
Comment #10
bdlangton commentedSo I took a look, and the comments indicate that there should be a required marker available, not just required TRUE/FALSE. I believe this is outdated and was probably removed at some point, but the documentation kept saying that there would be a "required" element passed in. Personally, I think it would be nice to have the required value available (as "required" instead of "element['#required']", so I'm supplying a patch that fixes that and updates the documentation text. If this is not approved, then the alternative is to remove the required text from the form-element.html.twig files so it isn't confusing as to why "required" isn't available.
Comment #11
anybodyThank you bdlangton, the patch does exactly what the documentation says and makes a lot of sense. Its the most clean solution from my point of view.
RTBC +1 but let's wait for further feedback. THANK YOU!! :)
Comment #16
tanubansal commentedTested #10 on 9.1, works as expected.
RTBC + 1
Comment #17
anybodyComment #18
lauriiiWe should expand the docs fix to all themes that have form-element.html.twig.
Comment #19
raman.b commentedComment #21
raman.b commentedForgot to update the hash for
themes/bartik/templates/classy/form/form-element.html.twigComment #22
samiullah commentedI can see the required flag available in template after applying the patch.
This can be moved to RTBC if no more code review is needed
Comment #24
abhijith s commentedApplied patch #21 and it works fine.The required variable is passed to the preprocess function of form element and documentation is added by this patch.

RTBC +1
Comment #25
anybodyDid someone check if #18 by @lauriii is fixed by #21? (Which means: Did someone check that all themes that have form-element.html.twig are fixed by the patch)?
Comment #26
bhumikavarshney commentedHi @Anybody,
Just check after applying #21 patch all themes that have form-element.html.twig are fixed by the patch.
Thanks.
Comment #27
gauravvvv commentedAbhijith S, BhumikaVarshney, Thank you for looking into this issue.
Posting screenshots of your codebase or CLI does not advance the issue, since the automated testing infrastructure tells us whether the patch applies correctly.
Comment #30
kristen polTagging for manual testing per #25.
This means going beyond just checking the patch applies. You will need to check each theme with a debugger or print to show that the
requiredvariable is indeed available and shows up only when the field is actually required. i.e. check both required and un-required fields to see therequiredvariable only is available when it's a required field.You can screenshot the debugger results but no need to screenshot the patch applying as per #27.
Comment #31
joshua1234511Manually tested the patch provided in #21 with the steps from issue summary.
- Patch applied
- Printed the required variable in template and checked the value for required and non required fields.
- required value is passed correctly.
Manual testing Pass.
Noticed patch is not updating all the theme docs.
Not updated
-- starterkit_theme
Updating issue to Needs Work.
Comment #32
joshua1234511Updated the patch from #21
Updated the doc comment in starterkit_theme theme file.
Comment #33
anybodyThank you very much @joshua1234511! :)
Also for the documentation improvements!
@Kristen Pol can we remove Needs manual testing and set this RTBC? I'm a bit afraid to do this ;) :)
So textual: RTBC+1 for #32
Comment #34
kristen polThanks for the updated patch and for the tentative RTBC. I updated the issue summary to use the template and improve the wording and formatting.
Comment #35
kristen polI'm marking RTBC based on the following. Note that it's unclear to me if a test can be added for this but, if so, move this back to "Needs work" and tag with "Needs tests".
1. Patch applies cleanly to 9.4.
2. Tests pass.
3. Code change is simple and only addresses the issue in the issue summary.
4. Manual testing passed in #31 and in #22.
5. Only change since latest manual testing is to the comments.
6. Comments have been consistently changed across all
form-element.html.twigfiles.Comment #36
larowlanRemoving credit for screenshots that show patch attached.
Please see https://www.drupal.org/node/2888804 for more information.
Comment #37
larowlanQueued a test-run on 10.0.x as that's where this will go first.
I think we can add a test for this as follows:
Starting with
common_test_themeadd a new theme hook that extends fromform_elemente.g.
Then add a new template to that module
form-element--common-test.html.twigand copy what's inform-element.html.twigand then add some twig that outputs the required variable, like @joshua1234511 has done in his screenshots above.Then in
\Drupal\Tests\system\Kernel\Common\FormElementsRenderTest::testDrupalRenderFormElementsRender a new form element, and pass
'#theme_wrappers' => ['form_element__common_test']with the element, e.g.Which should use our template, and then you can assert that the required variable is output.
Great work until this point, adding issue credits for mentoring and issue summary updates.
Comment #38
andregp commentedI'll try to work on a test for it.
Thanks @larowlan for showing how.
Comment #39
andregp commentedI did this patch following @larowlan suggestions, but I got a question regarding the new
common-test-form-element.html.twigI added.It is basically a copy of
core/modules/system/templates/form-element.html.twigwith a new element at the end. I was thinking: Is a shorter twig file with just the new element a best approach? (Something similar tocore/modules/system/templates/datetime-wrapper.html.twig)Sending also a test-only patch (expected to fail).
Comment #41
andregp commentedThe error on D10 patch seems unrelated to the issue (It was on the BlockFormMessagesTest). I added a retest to be sure if it's random.
Comment #42
kristen pol@andregp That's probably because the test only patch was added second. As far as I know, you need to add it first.
Comment #45
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
@Kristen Pol you are correct. Actually surprised it didn't get moved back automatically after 2 weeks by the bot.
The patch in #39 will have to be updated for D10 as some themes have been removed.
Since we are updating all the templates think we will need a change record since from what I can tell required can be NULL. If a contrib theme is doing a certain check could this cause errors or warnings?
Comment #47
hchonovRe-roll for 10.1.x
Comment #48
smustgrave commentedThanks! Still think this will need a change record.
Comment #50
majid.ali commentedRerolled patch for Drupal 11. Here is a draft for change record requested in #48.
Draft Change Record
Title:
requiredvariable is now available inform-element.html.twigtemplatesDescription
The documentation for
form-element.html.twighas historically listed arequiredvariable as being available to themers. However, this variable was missing from the template's preprocessing, forcing themers to access the raw element property (e.g.,element['#required']) to determine if a field was required.This issue has been fixed. The
template_preprocess_form_element()function now explicitly passes arequiredvariable to the template. This aligns the code with the documentation and provides a cleaner variable for themes to use when styling form elements.This change affects
form-element.html.twigin the System module and all core themes (including Claro and Olivero).Impacts
requiredboolean variable directly inform-element.html.twigoverrides.element['#required']will continue to work without modification.Code Examples
Before (Old way)
Themers had to dig into the
elementarray to check if a field was required.After (New way)
Themers can now use the documented
requiredvariable directly.