Problem/Motivation
When Views rewrites the display of a multi-valued field, it passes each value in that field through the rewrite code. If we wish to use Twig when rewrite a field, we want to option to pass all values to Twig and use Twig to sort out how the final HTML will look. Even though it's semantically correct to render lists with only one item as a list, it clutters the display.
Proposed resolution
Extend the 'Multiple fields settings' form with an option to "Render all items together." The default for this option is FALSE, which keeps things backward compatible.
Remaining tasks
Review possible security issue mentioned in #11.See #2466931: Valid Twig syntax is incorrectly escaped in Views rewrites- Write tests.
API changes
This does not change any APIs.
Comment | File | Size | Author |
---|---|---|---|
#33 | 2055597-33-field-render-multivalue.patch | 5.55 KB | mikeker |
#27 | allow_multi_value_field-2055597-27.patch | 6.66 KB | Nitesh Sethia |
#21 | 2055597-21-field-render-multivalue.patch | 5.55 KB | mikeker |
#17 | allow-multi-value-field-2055597-17.patch | 6.9 KB | jyotisankar |
#15 | 2055597-15.interdiff.txt | 2.55 KB | mikeker |
Comments
Comment #0.0
kaareUpdated issue summary with attached screenshots
Comment #0.1
kaareUpdated issue summary.
Comment #0.2
kaareUpdated issue summary. Removed UI changes header
Comment #1
kaareRenamed option from
'list_single'
to'omit_list_singletons'
which also inverts the logic. It makes sense to enable an exception, because that is what this is. You're adding an exception to creating lists of everything.Comment #2
kaareAnd here is a D8 version, i.e. -dev, where feature requests are supposed to go first. I'm learning :-) Though I'm not sure if this belongs in the 'field system' component as the code resides in the field module.
Settings as seen in D8:
It even works!
Comment #2.0
kaareUpdated issue summary.
Comment #3
jhedstromPatch no longer applies. It may be too late for 8.0, but perhaps allowed for 8.1?
Comment #4
mikeker CreditAttribution: mikeker commentedIf #2342287: Allow Twig in Views token replacement lands, we might be able to do this by providing a token for all the values of a multi-valued field and using Twig to style it as needed.
Pro: avoids clutter in the UI, allows even greater flexibility to site builders (eg: displays such as "item1, item2, and item3"), could add support for multi-value field tokens in contrib while waiting for acceptance into core.
Con: Less discoverable and requires knowledge of how to code Twig to make it work.
Thoughts?
Comment #5
Sivaji_Ganesh_Jojodae CreditAttribution: Sivaji_Ganesh_Jojodae commented+1 for adding this option. Have rerolled patch #2 against 8.0.x.
Comment #7
kaare@mikeker: Intriguing idea. It moves the burden over to the site builder and keeps the views code cleaner and UI uncluttered. I picture the rewritten field code could look something like this pseudo twig code:
This example code could reside in views documentation as an example on how to manage/rewrite/use multi value fields. Would it be an easy fix to provide this as a multiple item in twig?
@sivaji@knackforge.com: Thanks for the re-roll!
Edited: Fixed user names
Comment #8
mikeker CreditAttribution: mikeker commented@kaare, exactly!
One issue: multi-valued fields run the rewrite on each value of that field, not once for the field itself. This makes sense when you're wanting to rewrite each term a page is tagged with, for example.
Using the terms-on-a-page example, the term handler would need to 1) provide a Views token of all the terms in a single item, and b) only execute the render code once for that field. I'm not sure the best way to handle the UI as the site builder would need to control it -- in code we don't know if they want to rewrite each value or rewrite all the values at once. I suppose it would have to be somewhere in the field handler...
(Edit: Duh... I just realized that's exactly where you proposed your change as well! At least we're clearly thinking along the same lines!)
I've got a feeling that this is going to be too much change, too late in the product. And there's no reason it can't be done in contrib.
I've been thinking about this idea for a while now -- I'm happy to fiddle with it and roll a patch, but I don't want to hijack your issue and change its scope. Let me know if you'd prefer this in a separate issue or not. (assuming I can make it work...)
Comment #9
kaareOh, I don't care much about the solution of this problem, as long as it's possible for site builders/themers to do it. Even though … the ability to override the field with a twig template with all individual items available seems far more generic than using
<ul>
forcount > 2
and no html without.If we can argument that this feature doesn't disrupt the 8.0.x target, I think it's a valid candidate. It would bring much flexibility for site builders.
So sure, hijack away.
Comment #10
mikeker CreditAttribution: mikeker commentedOK, hijacking away! :P
The attached patch provides a "Render all items together" option when "Display all values in the same row" is checked. When enabled, multi-valued fields in a Views row will run
renderAltered()
once for each field rather than once for each value in each field. In addition, afoo_multiple
is added so you can render the field using Twig.To solve the initial use case -- rendering a single value (
field_tags
, in this case) as plain HTML and multiple values as a<ul>
-- you would check the "Render all items together" option and use a rewrite similar to the following:The result is:
Comment #11
mikeker CreditAttribution: mikeker commentedThings still to work on:
1. UI cleanup, could use a better explanation of what this new option really does. As was suggested by @kaare, there should be a doc page on d.o that could be linked to from the description.
2. I really want someone that knows Twig and security to tell me if there's a problem with the "unfiltering" I'm doing of admin-entered Twig:
I'm trying to keep the un-encoding as limited as possible, but you never know...
#2 is actually a more generic issue and, if this issue gets postponed it might be worth a separate issue for that. Otherwise something like
{% if foo|length > 1 %}
causes Twig to barf. And that seems like a crucial fix if #2342287: Allow Twig in Views token replacement is going to be worth anything...Comment #12
mikeker CreditAttribution: mikeker commentedSummary update. Hiding screenshots that are no longer valid with the new approach and (in case we return to the former fix) everything except the most recent patch of the previous approach that got a green from the testbot.
Comment #13
mikeker CreditAttribution: mikeker commentedComment #15
mikeker CreditAttribution: mikeker commentedHopefully fixes tests... Let's see what the testbot thinks!
Comment #16
star-szrTagging for reroll.
Comment #17
jyotisankar CreditAttribution: jyotisankar commentedRerolled
Comment #18
star-szrThanks @jyotisankar, a couple things I noticed…
Mode changes snuck in.
Unwanted trailing whitespace snuck in.
Comment #19
star-szrFor me this rebases cleanly following the steps at http://drupal.org/patch/reroll with no conflicts. Maybe give it another shot @jyotisankar?
Comment #20
mikeker CreditAttribution: mikeker commentedWhile there are other eyes looking at this issue, I would love some feedback on what I'm doing as mentioned in #11 -- see item #2 in that comment.
Thanks!
Comment #21
mikeker CreditAttribution: mikeker commentedReroll.
Comment #22
mikeker CreditAttribution: mikeker commentedThe patch in #21 still applies. Forgot to remove the "needs reroll" tag...
Comment #23
mikeker CreditAttribution: mikeker commentedAdded child issue to deal with #11. If we can get that into core, we may be able to handle this in contrib...
Comment #24
mikeker CreditAttribution: mikeker commented... And that other issue is #2466931: Valid Twig syntax is incorrectly escaped in Views rewrites.
Comment #25
mikeker CreditAttribution: mikeker commentedSince this is followup from #2342287: Allow Twig in Views token replacement.
Comment #26
star-szrTagging for reroll.
Comment #27
Nitesh Sethia CreditAttribution: Nitesh Sethia as a volunteer commentedRerolled the patch.
Comment #30
mikeker CreditAttribution: mikeker as a volunteer commented#21 still applies for me. Running it past the testbot to be sure...
#27 seems to add a bunch of unrelated stuff to the Views field schema.
Comment #32
mikeker CreditAttribution: mikeker as a volunteer commentedAck... Nevermind, I was on an experimental branch. Will reroll #21 shortly.
Comment #33
mikeker CreditAttribution: mikeker as a volunteer commentedLooks like the schema definition for
views.field.field
moved fromfield.views.schema.yml
toviews.field.schema.yml
.Yeah, that's not confusing!
Comment #34
star-szrUpdating the issue summary to remove this stuff #2466931: Valid Twig syntax is incorrectly escaped in Views rewrites. Needs work for the test coverage.
Comment #35
mikeker CreditAttribution: mikeker as a volunteer commentedNote: tests for this are potentially blocked on #2346615: Views doesn't have multi-valued field unit tests.
Comment #36
mikeker CreditAttribution: mikeker as a volunteer commentedFollowing an IRC conversation with @dawener, he suggested refactoring
advancedRender
to wrap a newdoAdvancedRender
(or possiblydoAdvancedRenderText
to clarify what we're rendering) and push the code that does the multi-value parsing intoField.php
.That avoids the need to do things like:
Also the existing
if ($this instanceof MultiItemsFieldHandlerInterface)
inFieldPluginBase
would be removed.