Problem/Motivation
The Drupal field template is quite verbose and can be hard to read. We start off checking for the label, then put in a conditional for if there are more than 1 field item (whether it's a multivalue field or not), then output certain divs depending on if the field has multiple values, then give an else version if not, and back to checking again about that label ... it gets tiresome.
Steps to reproduce
Open field.html.twig and see for yourself
Proposed resolution
Wouldn't it be great if we could simplify this to:
Be easier to read
- Out the same markup, whether there is one item in my field or multiple (so we don't have different CSS/JS depending on how many values we output)
- Have a conditional for styling differently if there is multiple (but this place as a modifier on the .field class, not as an additional .field__items class
Remaining tasks
Rewrite the field.html.twig template
User interface changes
None
API changes
None - my patch is against the starterkit_theme, but I'd like if we could (in D10) have this approach in our other themes too.
Data model changes
None
Release notes snippet
We have simplified the field.html.twig template. The only breaking change here is that we have a .field__items class wrapping the .field__item in all cases now, whether the field outputs multiple items or not. This is to make sure you can write the same CSS for single and multiple items. If you need to style something differently because it has multiple items, there is a .field--has-multiple-items modifier class at the .field level
This is based on a blog post I wrote called Simplified Drupal Field Template (but the same markup rendered)
Comment | File | Size | Author |
---|
Issue fork drupal-3308309
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
markconroy CreditAttribution: markconroy at Annertech for LocalGov Drupal commentedAdding patch.
Comment #3
markconroy CreditAttribution: markconroy at Annertech for LocalGov Drupal commentedComment #5
markconroy CreditAttribution: markconroy at Annertech for LocalGov Drupal commentedSetting back to needs review for the tests to run again.
Comment #6
lauriiiFWIW, the test failures look legit and we should probably address those.
Comment #7
markconroy CreditAttribution: markconroy at Annertech for LocalGov Drupal commented@lauriii Can I presume those test failures are not because of my patch? I can't see any thing in it that would cause those failures, or is there something else you think I need to do to get the tests passing?
Comment #8
Chi CreditAttribution: Chi commentedThat's not specific to this issue but I'd like to indicate that IDEs have problems with syntax highlighting of such code.
Also having non-closed tags in the codition blocks makes it hurder to read and understantd even though it seems less verbose.
Comment #9
markconroy CreditAttribution: markconroy at Annertech for LocalGov Drupal commentedI think the tests might be failing because our mark up for single items is changed now (with forcing the extra .field__items div to always be present), so the test for a single field item can’t check for .
field > .field__item
it needs to check for .field > .field__items > .field__item
Let's figure it out in Prague next week
Comment #10
sarahjean CreditAttribution: sarahjean at Digital Polygon commentedI think this is great, I almost always wind up overriding the default field templates to pare back all the extra cases, not this exact way but similar idea.
Comment #11
wesruv CreditAttribution: wesruv commentedLove this for starter kit, think it could be good for classy, but I wouldn't want this for lowest level field template & stable theme.
I'd prefer the default & stable templates prioritizing output over dev experience (without abandoning it).
Comment #14
tonypaulbarker CreditAttribution: tonypaulbarker at Annertech commentedI think we are looking at
Line 52 of testEntityViewController (core/modules/system/tests/src/Functional/Entity/EntityViewControllerTest.php)
and I think we want to achieve something like
<div class="field field--name-name field--type-string field--label-hidden"><div class="field__items"><div class="field__item">' . $label . '</div></div></div>
But, we seem to have some other issues with the tests locally, so going to see how we go and send work in progress.
Comment #16
bserem CreditAttribution: bserem at Annertech commentedComment #17
tonypaulbarker CreditAttribution: tonypaulbarker at Annertech commented-> Needs review for running tests
Comment #18
bserem CreditAttribution: bserem at Annertech commentedComment #19
markconroy CreditAttribution: markconroy at Annertech for LocalGov Drupal commentedGreat work everyone. Thanks for moving this forward.
Comment #20
andy-blumThe patched version of the twig is definitely easier to read, and there's no cost or risk associated with making this change in starterkits, but I fear it may be too late to get this change into D10.
The string returned from the test is has some ugly indentation, but it's a test and if it passes then I suppose ¯\_(ツ)_/¯
Pending successful tests, RTBC.
Comment #21
bserem CreditAttribution: bserem at Annertech commentedSeveral other areas are affected by this, lots of tests to check (or change those tests to use stark)
Comment #22
tonypaulbarker CreditAttribution: tonypaulbarker at Annertech commentedSome context to the tests.
They do not test anything in starterkit. They should be tested against Stark. They were changed to test against starterkit as a quick workaround when other themes were moved out of core. That is not really related to this issue (and no regression is caused) so it should not hold this up, we should open a new issue to tackle running against Stark.
We should only be concerned about coding standards of the indentation. If it passes coding standards, then I think this is good to go. Otherwise the indentation should be the only remaining thing to address.
This should ship with D10, otherwise we could start to run into backward compatibility issues.
Moving to review for check of coding standards of the indentation.
Comment #23
tonypaulbarker CreditAttribution: tonypaulbarker at Annertech commentedComment #24
andy-blumTests are failing.
Comment #25
tonypaulbarker CreditAttribution: tonypaulbarker at Annertech commentedSorry @andy-blum you're quite right - we've more tests to work through.
Comment #27
kostask CreditAttribution: kostask at Point Blank commented