Problem/Motivation
(This is the same for 2.x)
As fences is based on field.html.twig overrides, conflicts with themes are possible, if they also overwrite field.html.twig. This typically leads to situations, where fences settings have no effect. In most cases some of the settings are applied, but others are missing the required variables in the overwritten templates.
Fences for example works well with the testing theme "Stark", but if you try with Core default theme "Bartik", it won't work.
You can try via Tugboat here, for example.
Themes working with fences:
- Core: Stark
Themes not working with fences:
- Core: Bartik
- Contrib: Stable
- Contrib: Classy
Themes to be tested:
- Core: Olivero
Top 10 from Contrib Themes overview sorted by installs:
- Bootstrap
- bootstrap_barrio
- Bootstrap4
- Bootstrap5
- Radix
- ZURB Foundation
- Tara
- dxpr_theme
- Vani
- Danland
- AT Theme
Workaround / quickfix:
Until we're able to fix this, the best and most safe solution is to change the custom / contrib theme's field.html.twig according to this module's field.html.twig file contents.
It's highly recommended to manually compare the file contents and make the changes instead just overwriting the templates file, which may cause unwanted differences.
Steps to reproduce
Proposed resolution
Some ideas to improve the situation:
- [x] Add clear information about this and how to fix it manually on the module page
- [x] and in the README.md
- [x] Show a warning in the status report if this issue is detected
Unsure, if there's a (good) logical way to fix this in general. Ideas and help are very welcome!
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork fences-3306130
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
anybodyComment #3
anybodyComment #4
anybodyComment #5
anybodyComment #6
thomas.frobieterSame problem like we already had in fences_blocks, isn't it?
I really don't like the idea to add a field--fences.html.twig here too, but thinking a step further, Fences as a UI driven tool might should work also for novice users - out of the box.
However, this will be a breaking change, so IF we decide to do this, we need to schedule this to the next major release?
Comment #7
anybodyIndeed, at least very closely related.
Comment #10
martins.bruvelisI added a configuration option to allow the override of the field template only if it came from the core or allow to override of the field template for all themes.
@Anybody and @thomas.frobieter would adding Fences module setting to allow to override of the field template for all themes with the default option set to only allowing core theme override avoid a breaking change?
Comment #11
anybody@martins.bruvelis thank you very very much, this looks impressive! :)
I'll need more time to review this in detail, but great work!
What we'll definitely need here are tests. For the option disabled and enable to ensure the expected functionality. Could you add them in the next step please?
Should we also add a line to the status report explaining this option and showing its status? I think that wouldn't be too bad?
Comment #12
martins.bruvelis@Anybody, regarding,
Indeed, it would have been very helpful to see a warning displayed after a Bootstrap5 theme update that added field.html.twig template, resulting in Fences module not working. However, I am not sure how to reliably check if currently enabled non-core theme has a field.html.twig template to show a warning in the status report that an issue is detected. Also, I have not checked if there are any contributed themes, that would not work with Fences module. When I checked if Fences module is working Bartik theme, it looked that Fences is working, contrary to what is stated in the issue description above (maybe the issues with Bartik theme was resolved with one of the other issues patches).
Considering that Fences module checks for field.html.twig template via code
strpos($theme_registry['field']['path'], 'core') === 0it seems that Fences module will only override the field.html.twig template and not any of the more specific field templates, such as, field--entity-reference.html.twig. This seems to allow to override specific fields or group of fields via the respective field--*.html.twig templates, while allowing the base field field.html.twig template to be overridden by Fences module.Regarding,
From the above, the biggest issue is that existing tests do not check that the Fences module would stop working if contributed non-core theme is used. Most likely, because it is not easy to add a dependency on some contributed non-core theme, that could potentially add/remove field twig template from the theme with any of the theme updates. For example, in a recent Bootstrap5 theme update, the field.html.twig template was added, and Fences module stopped working without providing any notification, warning or error messages.
Therefore, could someone clarify if it is a common practice to add test where it is required to install, enable, and check functionality with a contributed non-core theme? If it is not common practice then we could proceed without adding any additional tests, as existing tests arealdy check that it works as expected.
Comment #13
chucksimply commentedMerge request patch works on Drupal 10. Thanks!
Comment #14
foxy-vikvik commented#10 patch working well on Drupal 10
Comment #15
chucksimply commentedComment #16
anybodyThanks all, I still think, this is a very handy addition!
I'd like to ask @Grevil and @thomas.frobieter to review and test this. It should not change existing behavior, but provide a convenient way for non-developers to use fences for overrides.
@Grevil and @thomas.frobieter should check the logics and the general functionality plus ensure BC, so nothing breaks.
@Grevil please think about #12 and check if there are sufficient tests for both cases. I agree we shouldn't add a test-dependency on any third-party theme. It would have to be tested with core themes.
I agree, this is brilliant work and looks really great! Very likely to be merged.
@martins.bruvelis did you check if Olivero perhaps overwrites the field.html.twig? Also the issue summary says, Bartik doesn't work with fences? But that's removed from core in Drupal 10: https://www.drupal.org/node/3304352
@Grevil: After review and feedback please hand over to @thomas.frobieter!
Comment #17
anybody#Needs manual testing for @thomas.frobieter and @Grevil
Comment #19
grevil commentedApologies, for the unnecessary commits, I'll resume the review and clean-up tomorrow!
Comment #20
grevil commentedAlrighty! This works great! :)
The adjustments I did on this issue's branch are attached to this comment as a diff. I think we can also remove the update hook entirely, as config inspector has no problem, updating the module without setting the conf, as it isn't a removal of a config!
Here is the HTML output pre-patch with bootstrap5 enabled:

And here is the HTML output after the patch with bootstrap5 enabled AND our newly added config enabled:

I'll add the tests now.
Comment #21
grevil commentedI put all my hopes and dreams on #2409811: Kernel tests should explicitly install themes and tried adding a theme in Kernel Tests like described in the issue, but unfortunately this doesn't seem to work. 😢
Comment #22
grevil commentedAlright, I did it! 🙂
I even created a test class testing the bootstrap theme! The only problem now, is that every theme comes with their own twig field definitions. MEANING, we can NOT use the same expected output for every theme (since the HTML output is quite different for each theme).
So we should discuss, if manual testing is enough for now, we comment out the newly added test classes and create a follow-up issue for finishing these test classes (since this will take a bunch of time), or if we should wait if we (or someone else) finds the time to finish these tests.
Otherwise I'd say this issue is done!
Comment #23
grevil commentedOk, we internally discussed this issue heavily and came to the conclusion that the current code could be merged, if the newly added setting is properly explained:
This should be explained throughout on the settings page, so that new users exactly know what the consequences of enabling this setting are and when to enable it, but thanks @martins.bruvelis, for providing the code and get this whole thing started!
We moved the test creation to #3368195: Refactor Kernel Tests to explicitly use a Theme, so they don't bloat this issue. Also, they don't really have any connection to the newly added setting, so that's that! 🙂
Comment #24
foxy-vikvik commentedthe #24 patch works for Drupal 10Comment #25
foxy-vikvik commented#26 works correctly
The issue with condition was fixed.
Comment #26
foxy-vikvik commentedComment #27
grevil commented@Foxy-vikvik, please provide any changes to the code in this issue fork's issue branch, so it is visible in the MR, thx!
Comment #28
chris burge commentedIn Drupal 10, both the Stable and the Classy themes were moved to contrib (out of core). Each theme provides a field.html.twig template, so the contrib versions of these themes are affected by this issue, too. (As a quick fix, I wrote a local patch for each themes that simply removed their respective field.html.twig files.)
Comment #29
chris burge commentedComment #30
anybody@Chris Burge and all the others here, could you kindly try / test the MR and tell if it fixes the situation as expected for you?
Then we should try to get this in, instead of adding custom fixes.
Leaving this NW as the final points from #23 have to be added and the patches by @Foxy-vikvik were not helpful outside of the MR. Thanks!
Comment #31
chris burge commentedI tested out MR-22. Testing steps:
<h1>.<div>element.<div>element.<h1>element.Summary - In my testing, the MR works as expected. I think we'll want to clear the render cache when the
fences.settingsconfig is updated in addition to invalidating the theme registry caches with$this->themeRegistry->reset();.Comment #32
anybodyThanks for the detailed feedback and testing! That sounds good.
I agree on that!
Any ideas for tests (see discussion above). Otherwise I'd merge this whenever community says it's reviewed enough.
Comment #33
chris burge commentedRe adding test coverage, we don't need to add any dev dependencies for testing (e.g. the Bartik contrib theme). We can add a test theme (fences/tests/themes/fences_test_theme_b) that has a field.html.twig template. The Twig UI module does this. We'd be looking at a Functional test. It'd probably be a good idea to add a second test theme, too, without a field.html.twig template (fences_test_theme_a).
Proposed testing below (Functional):
Set-up
Test 1 (Core without field.html.twig)
Test 2 (Core with field.html.twig)
Test 3 (Non-core without field.html.twig)
Test 4 (Non-core with field.html.twig)
Steps 2-4 on each test could be factored out into a protected method on the test class. Or.. we could also use a dataProvider to run scenarios through a single test method. My vote would be for the dataProvider approach.
Comment #34
chris burge commentedI don't see how those test failures are related to this MR.
Comment #35
chris burge commentedThe two test failures are unrelated to this MR. Interestingly, they don't occur when I run tests locally:
Comment #36
anybodyWhao @Chris Burge that was a smart idea! Thank you for that and the implementation. As written above, we're not running into this issue, as we have our own base themes, so the priority for us isn't that high on this task. But I'm very thankful the community helps to get that fixed!
I guess the failing test might be caused by the issue fork, we had such cases in the past in other forks.
I left some final comments. Then I think this is ready for RTBC by another reviewer.
Comment #37
chris burge commentedComment #38
anybodyThank you @Chris Burge! Left some comments. We're getting closer :)
Comment #39
chris burge commentedComment #40
anybodyThank you so much @Chris Burge! Looks great. Hope I (or someone else) will have the time for a final code-check. Very busy currently.
In the meantime, what do you think about these last points from the issue summary?
I guess some more information might make sense to better explain the new option and the need for it?
Finally, this seems to need a rebase. But we're getting super close, thanks to your awesome work here!
Comment #41
chris burge commentedRe README.md, there's no README. (Kinda surprised no one has tried to credit farm that yet.) What do you think about opening a new issue for adding a README? We could reference this issue to make sure the new template override behavior is documented in the event the README gets committed after this MR is merged.
Proposed README language re this issue:
Re the status warning, what do you think about this?
hook_requirements().Comment #42
anybody@Chris Burge thank you very much once again. Perfect, I agree with all these points. And sorry, I'm very busy currently and trying to maintain as many modules as possible for the community, so I can't help coding here currently.
Comment #43
chris burge commentedComment #44
chris burge commentedJust pushed fences_requirements(). I'd like to add test coverage in the next few days.
Turns out there is already a issue for the README: #3303084: Update module page and README.md to be up to date.
Comment #45
chris burge commentedWent ahead and wrote test coverage.
Comment #46
anybodyThanks @Chris Burge that looks great! :)
The failing tests seem unreated, but there are 3 code style fixes to do. After that I think it's perfect and ready to go!
Comment #47
chris burge commentedWhile there weren't any coding standards issues with this MR, there were a handful in code:
Pushed a commit to correct them.
Comment #48
anybodyThanks @Chris Burge sorry I thought I had seen a report about the lines in the .install file in the MR above. Maybe I was wrong...
Comment #49
chris burge commented@Anybody - It's all good. The three issues in .install were already there. They're all minor changes. Plus, now there's one less thing to worry about when switching to GitLab CI (which checks coding standards).
Comment #50
anybodyThanks @Chris Burge. So RTBC from my side. Anything else to consider for existing sites?
Otherwise, I think we can merge this into 3.x, what do you think?
Comment #51
chris burge commentedActually.. yes - We're adding a new route, so the cache will need to be rebuilt. Typically, the way I've seen this handled is to add an empty post_update function. Thoughts?
Comment #52
anybody@Chris Burge confirm #51! Just looked that up in another module.
BTW really sad to have no or hard to find documentation at Drupal.org for these things ... :/
Comment #54
anybodyThank you all! Merged into dev now. Let's see if the tests work in main.
Comment #56
mlncn commentedThis issue seems to persist for the Bulma theme: #3485788: Bulma's field.html.twig overrides and is incompatible with Fences module's
Is there anything themes should do or refrain from doing to be compatible with Fences?
Comment #57
mlncn commentedOK that question still stands, as far as best practices for contrib themes to play well with Fences, but the big green box on the module page that links to this issue really needs to be updated to say that one can go to /admin/config/user-interface/fences/settings and enable Override the field template for all themes!
Comment #58
thomas.frobieterYou are right @mlncn, this was missing from the module description. Thanks! I've added it.