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

  1. Find a form with a required field
  2. Print the required variable for form fields within the theme template file
  3. Variable will be empty for required fields
  4. 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.

Issue fork drupal-2928521

Command icon 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

thomas.frobieter created an issue. See original summary.

thomas.frobieter’s picture

Title: Reqired flag not available in form-element.html.twig » Required flag not available in form-element.html.twig
anybody’s picture

Title: Required flag not available in form-element.html.twig » "required" flag not available in form-element.html.twig
Version: 8.4.2 » 8.5.x-dev
Issue summary: View changes
anybody’s picture

Issue summary: View changes
anybody’s picture

Issue summary: View changes

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

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.

bdlangton’s picture

Status: Active » Needs review

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

thomas.frobieter’s picture

Hi,

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.

bdlangton’s picture

Assigned: Unassigned » bdlangton
Status: Needs review » Active

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

bdlangton’s picture

Assigned: bdlangton » Unassigned
Status: Active » Needs review
StatusFileSize
new2.91 KB

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

anybody’s picture

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

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

tanubansal’s picture

Tested #10 on 9.1, works as expected.

RTBC + 1

anybody’s picture

Status: Needs review » Reviewed & tested by the community
lauriii’s picture

Status: Reviewed & tested by the community » Needs work

We should expand the docs fix to all themes that have form-element.html.twig.

raman.b’s picture

Status: Needs work » Needs review
StatusFileSize
new6.18 KB
new3.53 KB

Status: Needs review » Needs work

The last submitted patch, 19: 2928521-19.patch, failed testing. View results

raman.b’s picture

Status: Needs work » Needs review
StatusFileSize
new7.14 KB
new816 bytes

Forgot to update the hash for themes/bartik/templates/classy/form/form-element.html.twig

samiullah’s picture

I 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

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

abhijith s’s picture

StatusFileSize
new75.48 KB

Applied 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.
after

RTBC +1

anybody’s picture

Did 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)?

bhumikavarshney’s picture

StatusFileSize
new73.27 KB

Hi @Anybody,
Just check after applying #21 patch all themes that have form-element.html.twig are fixed by the patch.
Thanks.

gauravvvv’s picture

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

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kristen pol’s picture

Tagging 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 required variable is indeed available and shows up only when the field is actually required. i.e. check both required and un-required fields to see the required variable 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.

joshua1234511’s picture

Status: Needs review » Needs work
StatusFileSize
new97.05 KB
new90.43 KB

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

joshua1234511’s picture

Status: Needs work » Needs review
StatusFileSize
new7.96 KB
new680 bytes

Updated the patch from #21
Updated the doc comment in starterkit_theme theme file.

anybody’s picture

Thank 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

kristen pol’s picture

Thanks for the updated patch and for the tentative RTBC. I updated the issue summary to use the template and improve the wording and formatting.

kristen pol’s picture

Status: Needs review » Reviewed & tested by the community

I'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.twig files.

[drupal-9.4.x-dev/9.4.x] [drupal-9.4.x-dev]$ grep -r "\- required:" . | grep form-element.html.twig
./core/profiles/demo_umami/themes/umami/templates/classy/form/form-element.html.twig: * - required: An indicator for whether the form element is required.
./core/modules/system/templates/form-element.html.twig: * - required: An indicator for whether the form element is required.
./core/themes/stable/templates/form/form-element.html.twig: * - required: An indicator for whether the form element is required.
./core/themes/stable9/templates/form/form-element.html.twig: * - required: An indicator for whether the form element is required.
./core/themes/bartik/templates/classy/form/form-element.html.twig: * - required: An indicator for whether the form element is required.
./core/themes/classy/templates/form/form-element.html.twig: * - required: An indicator for whether the form element is required.
./core/themes/seven/templates/classy/form/form-element.html.twig: * - required: An indicator for whether the form element is required.
./core/themes/starterkit_theme/templates/form/form-element.html.twig: * - required: An indicator for whether the form element is required.
larowlan’s picture

Removing credit for screenshots that show patch attached.

Please see https://www.drupal.org/node/2888804 for more information.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Queued 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_theme add a new theme hook that extends from form_element
e.g.


'form_element__common_test' = [
  'base hook' => 'form_element',
  'render element' => 'element',
],

Then add a new template to that module form-element--common-test.html.twig and copy what's in form-element.html.twig and 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::testDrupalRenderFormElements
Render a new form element, and pass '#theme_wrappers' => ['form_element__common_test'] with the element, e.g.


$element = [
  '#type' => 'textfield',
  '#title' => $this->randomMachineName(),
  '#value' => $this->randomMachineName(),
  '#theme_wrappers' => ['form_element__common_theme'],
  '#required' => TRUE,
];

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.

andregp’s picture

Assigned: Unassigned » andregp

I'll try to work on a test for it.
Thanks @larowlan for showing how.

andregp’s picture

Assigned: andregp » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new13.74 KB
new5.79 KB
new5.26 KB

I did this patch following @larowlan suggestions, but I got a question regarding the new common-test-form-element.html.twig I added.

It is basically a copy of core/modules/system/templates/form-element.html.twig with a new element at the end. I was thinking: Is a shorter twig file with just the new element a best approach? (Something similar to core/modules/system/templates/datetime-wrapper.html.twig)

Sending also a test-only patch (expected to fail).

Status: Needs review » Needs work

The last submitted patch, 39: 2928521-39-test-only.patch, failed testing. View results

andregp’s picture

Status: Needs work » Needs review

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

kristen pol’s picture

@andregp That's probably because the test only patch was added second. As far as I know, you need to add it first.

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

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs change record

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

Version: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

hchonov’s picture

Status: Needs work » Needs review
StatusFileSize
new9.64 KB

Re-roll for 10.1.x

smustgrave’s picture

Status: Needs review » Needs work

Thanks! Still think this will need a change record.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

majid.ali’s picture

StatusFileSize
new5.55 KB

Rerolled patch for Drupal 11. Here is a draft for change record requested in #48.

Draft Change Record

Title: required variable is now available in form-element.html.twig templates

Description

The documentation for form-element.html.twig has historically listed a required variable 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 a required variable 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.twig in the System module and all core themes (including Claro and Olivero).

Impacts

  • Themers: You can now use the required boolean variable directly in form-element.html.twig overrides.
  • Backward Compatibility: This change is non-breaking. Existing themes accessing element['#required'] will continue to work without modification.

Code Examples

Before (Old way)

Themers had to dig into the element array to check if a field was required.

{# core/themes/mytheme/templates/form/form-element.html.twig #}

{% if element['#required'] %}
  <span class="form-required">*</span>
{% endif %}

After (New way)

Themers can now use the documented required variable directly.

{# core/themes/mytheme/templates/form/form-element.html.twig #}

{% if required %}
  <span class="form-required">*</span>
{% endif %}

meeni_dhobale made their first commit to this issue’s fork.