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 commentedAdding patch.
Comment #3
markconroy commentedComment #5
markconroy 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 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 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 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__itemit needs to check for .field > .field__items > .field__itemLet's figure it out in Prague next week
Comment #10
sarahjean 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 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
tonypaulbarkerI 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 commentedComment #17
tonypaulbarker-> Needs review for running tests
Comment #18
bserem commentedComment #19
markconroy 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 commentedSeveral other areas are affected by this, lots of tests to check (or change those tests to use stark)
Comment #22
tonypaulbarkerSome 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
tonypaulbarkerComment #24
andy-blumTests are failing.
Comment #25
tonypaulbarkerSorry @andy-blum you're quite right - we've more tests to work through.
Comment #27
kostask commentedComment #31
markconroy commentedComment #32
markconroy commentedJust for clarity, the way
field.html.twigcurrently works is that if it's a multivalue field - even if it only has one item in it - it gets the.field__itemswrapper. If it has only one item, that item is wrapped in adivand if it has more than one item it usesul.The class we are intending to add here
field--multiplewill denote that this is a multivalue field, not how many items are in the field.Perhaps we could add another class
field--has-N-itemsto denote how many items we actually have.Suggestion for classes:
Note: we will need to update the issue summary with new notes for the change record.
===
Thanks to Code Enigma for sponsoring my time to work on this.
Comment #33
vensiresComment #34
bserem commentedComment #36
bserem commentedLB related tests fixed, working on CKEditor ones (the ones we haven't solved in years)
In the meanwhile, I don't really like the naming conventions and some of the classes that we are introducing here
- field--multivalue-field -> field--multiple-value-field, or field--multiple which is what already exists in Core
- field--single-value-field -> as is, or field--single which is what already exists Core
- field--has-multiple-items
- field--has-single-item -> field--has-one-item
- field--items-count-X -> is this really needed? Do we care about something like that or do we just pollute the DOM?
Comment #37
bserem commentedPS, `field_items` class is introduced to single value fields with the work on the branch. Thinking about it too
Comment #38
bserem commentedComment #39
bserem commentedComment #40
vensiresSince it's a long standing issue I also add the "Needs Review Queue Initiative" tag for others to check the solution provided too.
Comment #41
smustgrave commentedBefore reviewing think this needs a rebase, 600+ commits
Will need some screenshots during testing to make sure nothing has broke.
The tests that had to be updated to pass, surprised they are using starterkit_theme maybe follow ups are needed to correct that.
Comment #42
vensiresI went on with the rebase of the commits. I think the tests do need a check from someone who knows what he's doing (currently failing) + also check what @smustgrave said about the use of the starterkit_theme.
Comment #43
bserem commentedI believe we need to change the target branch to 11.1 or something. The commits are awfully wrong in Gitlab after the rebase.
It's been less than 10 commits, we are seeing things already merged in Core.
Comment #44
vensires