Problem/Motivation

Currently, there's no easy way to generate flag/unflag links in Twig templates. Developers need to inject the flag.link_builder service into their controllers or use service container directly to build these links. This creates unnecessary boilerplate code and makes templates less maintainable.

Proposed resolution

Add a new Twig extension that provides a `flaglink()` function to generate flag/unflag links directly in templates, similar to the existing `flagcount()` function.

Example usage:

{{ flaglink('node', node.id, 'bookmark') }}

Issue fork flag-3486524

Command icon 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

introfini created an issue. See original summary.

introfini’s picture

Title: Add Twig function to generate flag/unflag links » Add Twig function/extension to generate flag/unflag links
Status: Active » Needs review

MR added...

ivnish’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks!

1) Needs to fix pipeline issues https://git.drupalcode.org/issue/flag-3486524/-/pipelines/334145
2) Needs test for new twig function

ivnish’s picture

Version: 8.x-4.x-dev » 5.x-dev

deaom made their first commit to this issue’s fork.

deaom’s picture

Added test for twig extension, leaving to Needs work as phpstan for dependency injection still needs to be updated in the FlagLink.php

deaom’s picture

Status: Needs work » Needs review

The code in constructor of FlagLink twig extensions seems to be some legacy code, so rather then removing it, I've added the ignore next line for the Dependency Injection issue. Ready for review.

  • ivnish committed fa39a2d9 on 5.x authored by introfini
    feat: #3486524 Add Twig function/extension to generate flag/unflag links...
ivnish’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.