Over in #1333000: Remove all wrappers around title and content we discussed having an optional "field wrapper" that adds a wrapping HTML element around the field's title and values.
The issue is that currently fences supports the 90% use-case where a field either has no title or no need to wrap a title and a field value together. But it might be nice to easily allow that use case where you need to position (via CSS) a field value and its title together.
We might be able to do this with the #theme_wrappers render attribute. Needs investigation.
Obviously, we don't want to always use <div>
. We want it to be configurable just like the current wrapper around the field values.
Required steps
Write additional wrapper HTML patch with schema and settings form additions. Including tests.Write update hook to disable additional wrapper tag by default for existing installations to not change markup. Also ensure that by tests.- Write ADDITIONAL FieldOutputTest tests with wrapper, don't change the existing to ensure backwards-compatibility!
- Prepare issue summary text for 3.x release (to make important changes visible for admins) and include information about field.html.twig update which might be adapted to custom theme overrides
- Review by community
- Review maintainers and commit
Issue summary proposal
TODO
Comment | File | Size | Author |
---|---|---|---|
#91 | 1343578-91.patch | 8.61 KB | Anybody |
#68 | interdiff-54-68.txt | 17.5 KB | Anybody |
#68 | fences_8.x-3.x_add_items_wrapper__3018654-based__1343578-KEPT-TESTS-68.patch | 6.03 KB | Anybody |
#30 | interdiff.txt | 1.11 KB | adamwhite |
#30 | add_optional_wrapper-1343578-30.patch | 4.2 KB | adamwhite |
Issue fork fences-1343578
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 #1
JohnAlbinComment #2
figureone CreditAttribution: figureone commentedI'd like to second this request. I have a few fields that need to be absolutely positioned on the page (e.g., a field for sidebar content), and having the field label dissociated from the field content makes it impossible to move them both together.
Alternatively, an option to move the field label inside the fences wrapper for the field content would work well for my use case, too.
Comment #3
JohnAlbinI just realized how this could be implemented on the back-end, I think.
Drupal renderable arrays can contain a #theme_wrappers key with a list of theme hooks to wrap the item.
If Drupal allows us to add #theme_wrappers during hook_preprocess_field(), then we are set. Though I'm uncertain how to handle theme hook suggestions. Maybe embed them in the renderable array too? Needs testing. And a UI.
Comment #4
jnettikThis would be heavily used on a site I'm working on.
Comment #5
afoster CreditAttribution: afoster commentedI've just run into a spin off of the above use case I thought would be worth mentioning. It would be useful to have the ability to wrap the field title and the field value with their own unique tags.
I'm creating a machine parts list with each part as a node with different properties like max/min temp, burst pressure, as fields. Ideally we'll wrap a group of related fields in DL. In this case it would be great or the field title to in a DT and then field value wrapped the DD.
Example
Comment #6
JohnAlbin@afoster: Since you are talking about 2 separate fields being put into a wrapping <dl>, it's actually not the same issue at all.
You should be either be:
If you have additional questions, please open a separate support request. Thanks!
Comment #7
trawekp-1 CreditAttribution: trawekp-1 commentedI think hook_field_attach_view_alter() is what we need.
This code adds wrapper to every field but I'm not sure if this covers all occurences of displaying a field. I've tested node page and default formatter for field in Views and both works.
Adding simple checkbox to field settings form and adding the #theme_wrappers property to field only when that checkbox is checked should be quite simple.
Comment #8
alanburke CreditAttribution: alanburke commentedThe code is only needed if the label is displayed inline, and it that case, it is necessary.
I can't come up with a way to put the label inline without it.
The second class added here is overkill really.
The SCSS needed
Comment #9
jeremiahtre.in CreditAttribution: jeremiahtre.in commentedThis sounds interesting. I desire what appears to be the same thing:
Example:
Comment #10
doublejosh CreditAttribution: doublejosh commentedReally seems like a bad idea to remove wrappers on multi-value fields. Bummed that it works like that as-is.
Comment #11
BrightBoldAs others have mentioned, that is not the only use case for this feature. In addition to the above examples of absolute positioning and multi-value fields, sometimes you may want to style the field label differently for certain fields, or group a form element and its label together for positioning or styling purposes. Since they're all currently h3s with no class or other way to associate it with the field, this becomes impossible. So this feature would be useful in many situations.
Comment #12
duntuk CreditAttribution: duntuk commentedI second the request... Label and Value should definitely have a wrapper--or optionally selectable.
The only available option I see with Fences to achieve this (without rewriting things), is to wrap each field in it's own 'group' which would be way too cumbersome.
Comment #13
GiorgosKsurely needed, I thought it was there all along, just realized on multivalue fields
EDIT: #7 works very good
Comment #14
mducharme CreditAttribution: mducharme commentedWhat's the current thinking on this issue? There seem to be some options on the table but little movement. While the patch in #7 is a good stop-gap, I'd like to suggest two additional wrapper options per field and move the fences UI into a its own fieldset. See attached UI screenshot with suggested defaults.
I'm not married to the extra Field wrapper since this can be accomplished with Field Collections but I think a label wrapper is needed.
Thoughts?
Comment #15
trawekp-1 CreditAttribution: trawekp-1 commentedThis would be great but I'm wondering if those settings should be alterable on Manage Display tab or not. Sometimes you want to display field wrapped into a H2, sometimes not.
Maybe a default settings on Field settings page and specific settings on Display settings page (per view mode)?
Comment #16
mducharme CreditAttribution: mducharme commented@trawekp The beauty of fences is that field settings don't change across view modes. If you need per view mode settings then Display Suite may be more suitable.
Comment #17
trawekp-1 CreditAttribution: trawekp-1 commented@mducharme: Isn't field a data? I think that assigning a markup tag to a field (data) is not what we should expect as beatiful. You can never be sure where the field is going to be displayed or in what context. I'm still not conviced...
Display Suite is something I'd rather not use.
Comment #18
mducharme CreditAttribution: mducharme commentedI understand your point. View mode overrides would solve that (or fences for defaults + display suite for overrides now). I'd like see this label markup issue resolved first though. Perhaps a new feature request to provide view mode overrides is in order.
** EDIT **
Feature request already exists for display mode overrides: http://drupal.org/node/1888224
Comment #19
trawekp-1 CreditAttribution: trawekp-1 commentedGlad to hear that :] I can write a patch that would add a label settings to the field in a couple of days. It depends on free time I'd have.
Comment #20
trawekp-1 CreditAttribution: trawekp-1 commentedAccording to #14 it is impossible to create patch for this. Every template file needs rewriting.
In case of providing markup for label, wrapper and value there must be change in template files. Displaying label would have to be removed from templates and put into a theme function. This is quite big change, everyone who has written his templates would have to refactor them.
Comment #21
trawekp-1 CreditAttribution: trawekp-1 commentedHere is the patch that adds a optional wrapper but with no markup specific settings. It means there is only div available.
** EDIT **
This patch loses markup settings for all fields but the patch is simpler.
Comment #22
mducharme CreditAttribution: mducharme commentedI'll take a look at this. I've also created a patch that adds a label handler and it works well but is messy. I'll post it up when I have a chance for you to see what I've done for comparison and see if there is a good path forward.
Comment #23
marcvangendIs there any progress on this?
I would love to be able to output a definition list, for instance. Wrapping multiple fields in a single
dl
(as in #5) is out of scope for fences, I understand. But for a single field, I guess this should be possible:Currently, I'm overriding the dl-template in my theme to achieve this:
Comment #24
MrPaulDriver CreditAttribution: MrPaulDriver commentedIs the patch #21 likely to be committed?
Comment #25
HaiNguyen007 CreditAttribution: HaiNguyen007 commentedRe-roll patch #21 for latest dev
Comment #26
HaiNguyen007 CreditAttribution: HaiNguyen007 commentedBut this line of code produce a notice:
Notice: Undefined variable: suggestion in include() (line 239 of /path/to/fences/fences.admin.inc).
And I can't understand why? Please help.
Comment #27
marcvangendThis usually means that
$suggestion['wrapper']
is not set, so casting it to a boolean (evaluating it to be true or false) throws a notice. Try this instead:Comment #28
HaiNguyen007 CreditAttribution: HaiNguyen007 commentedThanks @marcvangend, I follow your suggestion & notice disappear :)
Here updated patch
Comment #29
HaiNguyen007 CreditAttribution: HaiNguyen007 commentedComment #30
adamwhite CreditAttribution: adamwhite at JMR Logics commentedI had to re-work the patch from #28 slightly.
Like 198 of fences.module was using an undefined constant as the array key ('value' needed to be in single quotes) and was throwing a PHP notice.
Furthermore, the earlier patch was placing the field API code to add the checkbox outside of the _fences_form_field_ui_field_edit_form_alter function, not within it.
I also tweaked the label on the checkbox itself for clarity.
Comment #31
JohnAlbinComment #32
MrPaulDriver CreditAttribution: MrPaulDriver commentedNeither of the patches at #28 & #30 will apply to 7.x-1.2 or the current 7.x-2.x-dev
Reverting back to 7.x-1.0 and using the patch at #21
Comment #33
jglynn CreditAttribution: jglynn commentedCan this get committed pls? Fences is quite a pain to use without the option to wrap the label and content together, as there is no way to position the label.
Comment #34
MrPaulDriver CreditAttribution: MrPaulDriver commentedI would really would like to see this committed.
It would be of no detriment to those that don't need it, but it adds much utility for those that do.
5 years is a long time to wait. Please
Comment #35
Chris Matthews CreditAttribution: Chris Matthews commentedI ran into this issue and would very much like to see this fix get into 7.x-1.x as well.
Comment #36
thomas.frobieterAlso needed in the D8 release. Extremly problematic as we are currently also not able to override the field.html.twig to fix it on our own (https://www.drupal.org/project/fences/issues/2572397).
Comment #38
AnybodyOk let's take this to Drupal 8 and get it running...
Attached you can find a patch written together with @thomas.frobieter with a working wrapper implementation for Drupal 8. No tests included yet to have a manual review first. If everything is OK for you, we should
Write / change tests
Backport to Drupal 7
We have wrapped the field items stead of the whole field which makes a lot more sense to us... Feedback is welcome.
Comment #40
AnybodyAttached you can find a cleaner approach which corrects the class names to fit the BEM (http://getbem.com/naming/) and Drupal Core standard.
This *might be a separate issue* but would make sense to commit this together with the wrapper change in Drupal 8. Perhaps it would justify a new 8.x-3.x release because it fixes (but also changes) old, wrong markup.
Comment #41
AnybodyComment #43
thomas.frobieterWe should consider to also add twig {block}'s around the single wrappers & values, so we are able to override just parts of the field template file.
Comment #44
Anybody@thomas.frobieter that's a great Idea. Would you mind to create a new patch accordingly? I guess it may be part of this issue and doesn't have to be separated.
Comment #45
thomas.frobieterThe new patch adds the Twig blocks and an empty check for the new wrapper settings.
Comment #46
Anybody@thomas.frobieter. Patch from #45 seems to be broken. Can not be applied against latest dev.
Comment #47
AnybodyReworked patch from #45 and merged changes with #40. We still need to adjust the tests.
Comment #48
AnybodyOk this is still quite relevant. Because the patch for 8.x-2.x from #47 conflicts with the major issue in #3018654: Fences seems to be ignored by Layout Builder I'll recreate it after that one has been committed and then we should push things forward.
Comment #49
AnybodyOk, here we go. Attached is the patch to review after major #3018654: Fences seems to be ignored by Layout Builder has been fixed and committed! Until that the tests will fail.
I named the patch 8.x-3.x because this should definitely be a new branch because the code structure changes to be more flexible and cleaned up.
Comment #50
Anybody-- broken --
Comment #51
AnybodyReroll from #49 against latest 8.x-2.x-dev after https://www.drupal.org/files/issues/2019-07-12/3018654-16.patch applied.
I'd be happy to receive your reviews, I think we've improved the structure a lot.
Comment #52
AnybodyOk let's start testing now where #3018654: Fences seems to be ignored by Layout Builder has been fixed.
Comment #54
AnybodyFixed tests, the IntegrationTest is still a bit unclear because I don't know which Theme it uses (missing knowledge and infrastructure on my side) - I tested with Spark which has no "clearfix" on while the old test had...
If someone could help, that would be perfect.
Comment #55
Sam152 CreditAttribution: Sam152 at PreviousNext commentedRequiring these keys to be set indicates there'll be an issue with config created in previous installations. For BC reasons we need the module to work in exactly the same way before and after these settings were introduced, so we need some defensive checks in case they are missing and default to the pre-patch behavior.
The best way to demonstrate BC like this is to leave all of the existing test cases as they are and only introduce new tests for new functionality.
Comment #56
AnybodyHi @Sam152,
I completely understand your point, but from my perspective we have to introduce major changes (release from old burdens) to the markup structure which can't all be migrated from the old ones. Perhaps I'm wrong and it's even possible by moving configuration around, but on the first sight I guess it's not and I think the better structure should be our major focus, not the migration path between the major releases. Existing sites should be able to keep 8.x-2.x and should step by step upgrade to 8.x-3.x.
Anyway I agree that we should try to find ways to migrate old configuration and markup structures as good as possible, but this issue definitely needs a new major 8.x-3.x release. Keeping the old tests and see what is possible is a very good strategy, but perhaps that should include a config update / migration step. The code and variables should still express what they really do, even if it makes the migration harder.
We didn't even have a stable release yet so I think that should be possible, shouldn't it? See it like Drupal 7 to Drupal 8 major upgrade.
Comment #57
Sam152 CreditAttribution: Sam152 at PreviousNext commentedBackwards compatibility shouldn't be too hard here right? We can default to simply hiding the field item wrapper markup if no element has been explicitly selected.
Strictly speaking, we'd need both an update hook to adjust config and to also support non-updated config. That's unfortunately a consequence of: #1677258: Configuration system does not account for API version compatibility between modules.
Comment #58
AnybodyThis is not forgotten. We're still using this in all our projects and it works great. We just didn't have the time to finalize the patch and upgrade path yet.
@Sam152: If you'd be interested, I'd be interested in Co-Maintainership for a 3.x branch. But I'll reply here as soon as we have the time to proceed and finalize things. Sorry.
Comment #59
benjifisherWhat needs to be done on this issue? Is it just an update hook or two? Do we need to handle updates from 7.x versions as well as earlier 8.x versions?
Comment #60
AnybodyHi @benjifisher,
thank you for your reply and my delay. I'm very busy currently. Yes, what has to be done is what Sam152 wrote in #57:
plus
Furthermore the patch has to be rerolled, I guess. I'll do that as soon as possible. If someone is willing to help to push things forward, I'd be happy.
If anyone is using this patch until reroll, simply use
"drupal/fences": "2.x-dev#ed9aed6",
in composer to switch back to the alpha2 status.Comment #61
AnybodySorry, this is on my priority list, but I'm still very busy. I'll create a new patch to create a 3.x branch.
2.x should be kept for users who don't need the new features and 3.x should become new development branch, but not yet suggested branch until it's widely tested.
And yes there should be an upgrade path from 2.x to 3.x, preserving markup by migrated settings if possible. New installations receive the new default structure, I'd suggest.
If there is a Drupal 7 upgrade path to 2.x everything will work with the 3.x incremental update, because we should keep all hook_updates since older versions and simply add new ones to 3.x.
If there is no upgrade path yet, adding Drupal 7 upgrade path would be a separate, unrelated issue.
What do you think?
Comment #62
AnybodyOk let's reroll this against latest .dev and align tests.
Comment #63
AnybodySorry, here's the interdiff.
Comment #65
Anybody@Sam152: Re #55/#57: I have 3 options in mind and would like to hear which you like most or if you see a better alternative:
a) Add fences_field_items_wrapper_tag and set it to "none" by default. The disadvantage is that you'd have to enable it on every single instance, where fences is used to get very helpful markup for themers, which is even standard for many themes and core bartik for example. The reason would be BC, but it means a lot of manual changes for every instance now and in the future, if you need it.
b) Create a new 3.x version and keep the 2.x version. In 3.x we add and enable field__items as new default, like many themes do. If people switch to 3.x and want the 2.x behaviour without field__items, this would mean a lot of manual change now and in the future (like (a), just the other way around).
c) Create a settings page for fences to set the four default tags globally and add a "Global default setting" option to each of the four selects so you can configure the defaults centrally. Settings default to 2.x standard.
(c) is my personal favourite because you can control the field tags defaults globally and only have to change the value if required. Me make no expensive assumptions here what the user requires. Between (a) and (b) I'd choose (b) because 2.x is kept and 3.x introduces better markup for the future.
A 3.x version should be created anyway for these new features, I think. To reflect the important change and tell website owners about the new option which is often relevant for themers. Furthermore we could allow testing the feature prior to stable release and introduce semver btw?
I'll have a look at the patch errors soon, was just a first shot. If anyone has some time, please provide feedback about a/b/c and help to fix the patch without changing existing IntegrationTests and FieldOutputTests, just add new ones. I'll revert that part too, as @Sam152 is right in #55 to ensure BC this way.
Comment #66
AnybodyKeeping the old tests. Tests for new functionality will be added with next patch.
Comment #68
AnybodyNew patch with update hook proposal, please have a look, what you think:
Comment #70
AnybodyDoes someone know if the testbot will run update hooks? In this case I named the hook 8301 because I presumed a 3.x version for this feature. The failing tests are related to the missing keys for the new items.
Patch #68 applies cleanly against latest dev and rc-1 FYI
Comment #71
Anybody@Sam152 & @benjifisher, could you perhaps have a look at #65 to #70 and give some feedback what you think?
I'm now back on this issue and will use issue forks for further implementation to make it easier to review the changes for all of us.
Thanks a lot :)
Comment #73
AnybodyPushed #68 to Issue Fork branch 1343578-add-optional-wrapper:
https://git.drupalcode.org/issue/fences-1343578/-/tree/1343578-add-optio...
Added a (currently WIP) Merge Request: https://git.drupalcode.org/project/fences/-/merge_requests/2
You can track the MR changes as patch live here: https://git.drupalcode.org/project/fences/-/merge_requests/2.patch
Comment #77
AnybodyThis issue now has Tugboat live preview for the MR! Yay! :) That should make it a lot easier to test and discuss proposed changes.
Comment #78
AnybodyComment #79
AnybodyHopefully this triggers tests on the Issue fork MR!2...
EDIT: Sadly not. @Sam152 could you perhaps enable tests for issues / patches as written in https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa...
Comment #80
AnybodyRemoved the BC as the update hook allows us to do this non-breaking. I'll also add the further steps to issue summary.
Comment #81
Sam152 CreditAttribution: Sam152 at PreviousNext commentedI mentioned this before, but the safest way of adding keys to config schema is to both support updates via a hook and to have BC and defaults:
The update hooks don't cover config that may have been exported into a module or profile
./config/(install|optional)
. There is no system to update these exported objects, that don't exist in active configuration.Comment #83
Grevil CreditAttribution: Grevil as a volunteer and at DROWL.de commentedThank you @Sam152,
I added further checks to fall back gracefully, if the configuration is not existing due to the reasons given in #81. I guess it's feature-complete now?
Could you please have a look and tell if any further changes are required or this is ready for RTBC?
Comment #84
AnybodyUnassigning for review. In my eyes this is ready. Please test and provide feedback. Still think a 3.x semver release for this would make sense to be 100% safe...
Comment #85
AnybodyAs there's a bunch of work in this issue and we still think this is very useful and a huge improvement to the module, I'd like to ask the maintainers of this module for further feedback and steps.
I wrote @Sam152 a PM some weeks ago, but didn't receive a reply, guess he's busy. So I'd like to ask the maintainers, if they're planning to proceed here in the near future.
Otherwise, we'll have to create a fences fork, which I don't like to do, but patching this in for more than three years also isn't a good final solution.
Comment #86
AnybodyGreat work once again @Grevil! RTBC and as written in #84 I'd vote to create a 3.x release for this change.
Comment #87
AnybodyComment #88
AnybodyNeeds rebase after the latest changes.
Comment #89
Grevil CreditAttribution: Grevil as a volunteer and at DROWL.de commentedPlease review carefully! As this was quite a big rebase!, also I haven't applied the patch from https://www.drupal.org/project/fences/issues/1781940#comment-13649024, maybe someone with twig experience can tell me, if the current twig file will add the extra spaces or not.
Comment #90
Grevil CreditAttribution: Grevil as a volunteer and at DROWL.de commentedTests need a rewrite, as of these changes!
Comment #91
AnybodyCurrent state as patch. Still working on the whitespace issues -.-
Comment #92
Grevil CreditAttribution: Grevil as a volunteer and at DROWL.de commentedComment #93
AnybodyOk finally I think we have a nice intendation now. That was pain... ;) Let's see what the testbot says.
Comment #94
AnybodyOkay the failing tests indeed point to an issue, but as the test output containing HTML is stripped, here's the result with HTML to be easier to compare:
What's happening and what's the cause (to be fixed):
Class
field__items
is missing onfield_tag
where it was before:<{{ field_tag|default('div') }}{{ attributes.addClass(classes, 'field__items') }}>
(Before)<{{ field_tag|default('div') }}{{ attributes.addClass(classes) }}>
(After)as it was moved down to:
Core also sets field__items on the field_tag if there are multiple items, see https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/themes/class... for example
or
https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/themes/olive...
Perhaps we should also implement the
multiple
check to be as close as possible to the core results?So if
display_items_wrapper_tag
is TRUE the field__items class should be at the wrapperIf
display_items_wrapper_tag
is FALSE, it should be atfield_tag
like in 2.x and in core.So we don't have to make a breaking change.
That could be done in PHP or in twig, but we should also consider people may have overwritten the twig file, so I guess one of both options is better. I think it might be the twig solution.
Let's discuss this with @thomas.frobieter and look for the missing additional test for now.
So back to NW.
Furthermore, I'm wondering where our additional tests with wrappers are gone... Hope we'll find them and I didn't just forget to push the changes when I wrote them.
Comment #95
Grevil CreditAttribution: Grevil as a volunteer and at DROWL.de commentedWhen this issue is resolved, we should also add an example screenshot for the items field wrapper on the module page, and add an items field wrapper description in #3303663: Add descriptions for the field settings.
Comment #96
thomas.frobieterAdded
field__items
if wrapper is unused to keep backwards compatible.Comment #97
thomas.frobieterComment #98
Anybody@thomas.frobieter you forgot to push
Comment #99
Grevil CreditAttribution: Grevil as a volunteer and at DROWL.de commentedI guess the implementation in this issue fixes the issue found in #3303655: [2.x INFO] Setting Field label "none" hides the label value (BC). Let's rebase this to see if the tests go green then!
Comment #100
Grevil CreditAttribution: Grevil as a volunteer and at DROWL.de commentedRetriggered tests!
Comment #101
Grevil CreditAttribution: Grevil as a volunteer and at DROWL.de commentedYay! Great work, thanks all here, let's merge this into 3.x and create a 3.0.0-alpha0!
Comment #103
Grevil CreditAttribution: Grevil as a volunteer and at DROWL.de commentedComment #104
AnybodyThank you all soooo much, this is amazing! :)