Problem/Motivation
The flag theme does not contains any information on the flaggable view mode. This information allows building conditional logic in the theming layer.
Steps to reproduce
- Render a flag on a node 'full' display.
- Render a flag on a node 'teaser' display.
- Notice there is no information available in the twig file or preprocess methods about the flaggable view mode to distinguish between the two.
Proposed resolution
Add an additional argument 'view mode' to the flag link builder to enable conditional logic in the theming layer.
Remaining tasks
- Write a patch
- Review
- Commit
User interface changes
None.
API changes
The flag.link_builder:build lazy builder takes an additional argument 'view_mode'.
Data model changes
None.
Original report by gagarine
I have a flag in the teaser and full node. Template suggestion do not offer a way to have two different kind of template depending of the view mode of the entity.
I taught about using the flaggable variable aviable in flag.html.twig . Sadly flaggable.view_mode give me nothings. I explored the flaggable variable but didn't find any information about the display mode.
What is the way to do that?
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | 3049155-16.patch | 17.54 KB | idebr |
| #16 | interdiff-14-16.txt | 6.36 KB | idebr |
Issue fork flag-3049155
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
b.khouyI can confirm I've also experienced this issue, I've resolved the situation by adding the following process using
hook_theme_suggestions_alter()Comment #3
idebr commentedAttached patch adds an additional argument 'view mode' to the flag link builder to enable conditional logic in the theming layer.
Comment #5
idebr commentedAttached patch fixes the flag response when using ajax links.
Comment #7
anneke_vde commentedPatch works as designed, I now have a view mode.
Comment #8
idebr commentedAttached patch fixes the flag_link Views field.
Comment #12
hudriI've tried patch #8, and it works on the initial load, but the view mode information is lost after triggering an Ajax reload by (un-)flagging. In my case I first had a
flag--full.html.twigsuggestion on the intial load, but after clicking the link, I got aflag--default.html.twigsuggestion instead.Comment #13
lus commentedWell I got now the possibility to create a new view mode for the Flags, but how do I apply this view mode in my Node entity ? There is no option in the Format.

And if I crate a suggestion in a preprocess, it's not working after Ajax submit as Hudri said.
Comment #14
idebr commented#13 The view mode is added implicitly when the Flag link is rendered, similar to fields. There is no configuration required.
Attached patch adds the following change compared to #8:
Not sure what is going on with the merge request linked to this issue. I suggest using this patch instead.
Comment #16
idebr commentedAttached patch adds the following change compared to #14:
Comment #17
anneke_vde commentedI tested the above patch, the view mode now persists when using AJAX flag links.
Comment #18
tijsdeboeckTested patch #16, works great!
One issue that I encountered was when using Conditional Confirm Form as Linktype (using the flag_conditional_confirm module), that combination throws a fatal error related to the code in flag_conditional_confirm.
Comment #19
claudiu.cristea+1
Thank you for this. It helps to create template suggestions based on the flaggable view mode, allowing to have different theming on different view displays. Very useful! I wonder whether such template suggestions should not be part of the flag module itself but that could go in a followup.
Comment #20
claudiu.cristeaFor each method where we alter the signature by adding the new
$view_modeparameter, we should:Messages should state that the param will be mandatory in the following Flag major version.
Comment #21
kimlop commented@claudiu.cristea could you provide an example of template suggestions? thanks
Comment #22
claudiu.cristea@kimlop
Maybe something like...
Comment #23
anybodyWe've just run into the same issue sadly and need the information about the view mode in the flag template. So it's indeed not an edge-case. Going to push this forward now.
Comment #25
anybody@claudiu.cristea Re #20 I implemented:
✅ Strict typing it to string
✅ Make it optional, defaulting to NULL, otherwise is breaking BC for all sites extending these methods
✅ If NULL is received, we should
✅ Trigger a deprecation error
❓ Log a warning message
✅ Messages should state that the param will be mandatory in the following Flag major version.
🔜 A change notice should be issued.
Could you perhaps take a brief look, if that's what you expected? So we can push things forward here.
I was a bit unsure at which places we should implement the deprecation errors, quite a lot of places.
Comment #26
anybody@idebr maybe you'd also like to take a look at the new MR based on your patch. One test is failing, I'm not yet sure why.
Comment #27
idebr commentedNot sure why the
$view_modeargument should default toNULL? Could just bestring $view_mode = 'default'to match the current behaviourComment #28
anybody@idebr I think I've seen
NULLas default also in core for that. Core then falls back to default if needed, if I'm correct. So that seems right to me!Comment #29
grevil commentedI couldn't find any core code doing that. It is instead probably related to @claudiu.cristea in #20:
I'd agree with that suggestion and seeing that the deprecation and logging part isn't implemented yet, and one test still fails, I'll set this to "Needs work" again. I'll have a go at it.
Comment #30
anybodyYes, of course it's related to #20 but I thought NULL was also used in core for that in some places. Might have been D7 then perhaps, where I had seen that in the past.
Core uses "default" or "full" in some places, for example https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/block...
But I'd also vote to keep it NULL until the fallback is removed, I think it's most clear, if it has no other disadvantages.
That said I'm also totally fine with changing this if it has any benefits.
Comment #31
grevil commentedI adjusted and moved the deprecation notices here: https://git.drupalcode.org/project/flag/-/merge_requests/67/diffs?commit...
All methods end up either calling
getAsLink()orgetAsFlagLink()and since these methods are all public, it only makes sense to throw the deprecation errors there.Comment #32
grevil commentedThat is quite weird. I get the exact same phpunit error locally, when running
Drupal\Tests\flag_follower\Functional\FlagFollowerUITest::testUion the 8.x-4.x dev branch, even if the remote on "main" succeeds: https://git.drupalcode.org/project/flag/-/jobs/568309Comment #34
grevil commentedChange record can be found here: https://www.drupal.org/node/3458551.
Comment #36
grevil commentedAlright, all done!
The last remaining test failure is unrelated, as the test also fails on current 8.x-4.x-dev, as can be seen here: https://git.drupalcode.org/project/flag/-/merge_requests/68.
Please review!
Comment #38
anybodyGreat work @Grevil!
The only thing I found left is:
Can you check that? Of course we shouldn't make unrelated changes / fixes!
Regarding the broken test we can't do anything as of #36. So from my side this is ready for RTBC once the code quality changes have been checked.
Comment #39
grevil commentedSome of the warnings are incorrect, e.g. "Call to method setRouteParameter() on an unknown class Drupal\flag\ActionLink\Url." but others make sense, e.g. using a non injected logger instance.
Comment #40
grevil commentedOn the other hand, the logger will be removed, once the deprecated code gets removed in 5.x. Meaning we would need to introduce a new logger object which will be introduced as deprecated... Meaning third party modules would need to take care of a second deprecation...
So all perfect, please RTBC!
Comment #41
anybodyThank you @Grevil, I agree. Apart from that it might make sense to give the module a code-style cleanup in a separate issue using the modern tools.
Comment #43
berdirAdding new parameters to an interface, even when optional, is an API change. The project is beta, but it's been beta forever, so conflicted about adding this.
One option is to not add it to the interface and just the implementations, that is allowed.
Other thoughts:
* Also changes routes to require additional parameters.
* Not sure about the warning log message. That could result in *many* warnings on a busy site. Just the trigger error might be sufficient?
* theme suggestions based on the flaggable id seems overkill. I understand it's already there, but I don't think we need to add even more variations.
Comment #44
grevil commentedAll routes using the "view_mode" parameter, have a "view_mode" parameter set. I don't think we need to do anything else here?
I talked to our theme developer, and he said that at least the first two additions add a lot of useful benefit!
Adjusted everything else. Please review, last changes!
Comment #45
anybodyThanks @Grevil! Just had a look and LGTM! Back to RTBC from my side.
I also agree that at least these suggestions totally make sense to add benefit to the functionality and make it easier to use them without additional custom code for typical cases like they know it from other entities.
I'm slightly unsure about the return type addition in
src/ActionLink/ActionLinkTypePluginInterface.phpis that fine here?Comment #46
anybodyBTW we're now using this in a project and it works great, thanks all!
Comment #49
ivnish