Disclose flagging view mode in flag theming

Created on 18 April 2019, over 5 years ago
Updated 29 June 2023, over 1 year ago

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

  1. Write a patch
  2. Review
  3. 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?

šŸ“Œ Task
Status

RTBC

Version

4.0

Component

Flag core

Created by

šŸ‡ØšŸ‡­Switzerland gagarine

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • šŸ‡·šŸ‡“Romania claudiu.cristea Arad šŸ‡·šŸ‡“

    +1

    Thank you for this. It helps to create template suggestions based on the flaggable view mode. This helps with theming base on the flaggable view mode. Very useful! I wonder whether such template suggestions should not be part of the flag module itself but that could go in a followup.

  • Status changed to Needs work over 1 year ago
  • šŸ‡·šŸ‡“Romania claudiu.cristea Arad šŸ‡·šŸ‡“
    --- a/modules/flag_count/src/Plugin/ActionLink/CountLink.php
    +++ b/modules/flag_count/src/Plugin/ActionLink/CountLink.php
    @@ -23,9 +23,9 @@ class CountLink extends AJAXactionLink {
       /**
        * {@inheritdoc}
        */
    -  public function getAsFlagLink(FlagInterface $flag, EntityInterface $entity) {
    +  public function getAsFlagLink(FlagInterface $flag, EntityInterface $entity, $view_mode) {
         // Get the render array.
    -    $build = parent::getAsFlagLink($flag, $entity);
    +    $build = parent::getAsFlagLink($flag, $entity, $view_mode);
     
         // Normally, you'd just override flag.html.twig in your site's theme. For
         // this example module, we do something more advanced: Provide a new
    

    For each method where we alter the signature by adding the new $view_mode parameter, we should:

    • Strict typing it to string
    • Make it optional, defaulting to NULL
    • 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.
  • Open in Jenkins ā†’ Open on Drupal.org ā†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    46 pass
  • šŸ‡®šŸ‡¹Italy kimlop

    @claudiu.cristea could you provide an example of template suggestions? thanks

  • šŸ‡·šŸ‡“Romania claudiu.cristea Arad šŸ‡·šŸ‡“

    @kimlop

    Maybe something like...

    function yourmodule_theme_suggestions_flag_count_alter(array &$suggestions, array $variables): void {
      $suggestions[] = 'flag_count__' . $variables['view_mode'];
    }
    
  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    We'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.

  • Pipeline finished with Failed
    5 months ago
    #213691
  • Pipeline finished with Failed
    5 months ago
    Total: 184s
    #213692
  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    @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.

  • Pipeline finished with Failed
    5 months ago
    Total: 165s
    #213765
  • Status changed to Needs review 5 months ago
  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    @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.

  • Pipeline finished with Failed
    5 months ago
    Total: 162s
    #213824
  • šŸ‡³šŸ‡±Netherlands idebr

    Not sure why the $view_mode argument should default to NULL? Could just be string $view_mode = 'default' to match the current behaviour

  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    @idebr I think I've seen NULL as default also in core for that. Core then falls back to default if needed, if I'm correct. So that seems right to me!

  • šŸ‡©šŸ‡ŖGermany Grevil

    [...] I've seen NULL as default also in core for that. Core then falls back to default if needed [...]

    I couldn't find any core code doing that. It is instead probably related to @claudiu.cristea in #20:

    [...]
    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.
    [...]

    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.

  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    Yes, 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.

  • Pipeline finished with Failed
    5 months ago
    Total: 648s
    #213961
  • Status changed to Needs work 5 months ago
  • šŸ‡©šŸ‡ŖGermany Grevil

    I 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() or getAsFlagLink() and since these methods are all public, it only makes sense to throw the deprecation errors there.

  • Pipeline finished with Failed
    5 months ago
    Total: 190s
    #213971
  • Pipeline finished with Canceled
    5 months ago
    #213975
  • Pipeline finished with Canceled
    5 months ago
    Total: 139s
    #213976
  • Pipeline finished with Failed
    5 months ago
    Total: 203s
    #213979
  • šŸ‡©šŸ‡ŖGermany Grevil

    That is quite weird. I get the exact same phpunit error locally, when running Drupal\Tests\flag_follower\Functional\FlagFollowerUITest::testUi on the 8.x-4.x dev branch, even if the remote on "main" succeeds: https://git.drupalcode.org/project/flag/-/jobs/568309

  • Pipeline finished with Failed
    5 months ago
    #213983
  • šŸ‡©šŸ‡ŖGermany Grevil

    Change record can be found here: https://www.drupal.org/node/3458551 ā†’ .

  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    Anybody ā†’ changed the visibility of the branch 3049155-disclose-flagging-view to hidden.

  • Pipeline finished with Failed
    5 months ago
    #214010
  • Pipeline finished with Failed
    5 months ago
    Total: 159s
    #214012
  • Status changed to Needs review 5 months ago
  • šŸ‡©šŸ‡ŖGermany Grevil

    Alright, 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!

  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    Anybody ā†’ changed the visibility of the branch 8.x-4.x to hidden.

  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    Great work @Grevil!

    The only thing I found left is:

    Code Quality scans found 173 new findings and 168 fixed findings.

    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.

  • šŸ‡©šŸ‡ŖGermany Grevil

    Some 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.

  • šŸ‡©šŸ‡ŖGermany Grevil

    On 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!

  • Status changed to RTBC 4 months ago
  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    Thank 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.

  • First commit to issue fork.
  • šŸ‡ØšŸ‡­Switzerland berdir Switzerland

    Adding 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.

  • Pipeline finished with Failed
    3 months ago
    Total: 712s
    #266143
  • Pipeline finished with Success
    3 months ago
    Total: 157s
    #266152
  • šŸ‡©šŸ‡ŖGermany Grevil

    Also changes routes to require additional parameters.

    All routes using the "view_mode" parameter, have a "view_mode" parameter set. I don't think we need to do anything else here?

    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.

    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!

  • Pipeline finished with Success
    3 months ago
    #266161
  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    Thanks @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.php is that fine here?

  • šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

    BTW we're now using this in a project and it works great, thanks all!

  • Pipeline finished with Success
    2 months ago
    Total: 173s
    #282148
  • First commit to issue fork.
  • Status changed to Fixed 21 days ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024