I've added two flags in my system and created a view with links to flag/unflag content more easily.

Flag 1: Content Type A, B
Flag 2: Content Type A

After adding relationship so that I can add the flag links fields to the output the flag 2-link for content of type B is rendered falsely - and of course not working.

Issue fork flag-3305405

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

Rudi Teschner created an issue. See original summary.

rudi teschner’s picture

Adding an additional check before rendering the links solves the problem for me, dunno if it is supposed to happen earlier though.

rudi teschner’s picture

Title: Links rendered even though content does not have flag » Links rendered even though content is not allowed for flag
rudi teschner’s picture

Forgot to consider $entity_type_id. Without it could result in a field type is unknown exception for other entity types like users.

ivnish’s picture

Status: Active » Needs work
Issue tags: +Needs reroll
jaydev bhatt’s picture

Assigned: Unassigned » jaydev bhatt

i'm checking this issue.

jaydev bhatt’s picture

Assigned: jaydev bhatt » Unassigned
Status: Needs work » Needs review
StatusFileSize
new63.67 KB
new61.34 KB
new1.37 KB

I was able to replicate this issue using the above description. What’s happening is that if one flag is attached to two content types and another flag is assigned to only one content type, the latter flag still generates a false link. When this link appears in the View, it renders but does not actually flag the content.

Steps to Replicate
1. Set Up Flags:
Create two flags in your Drupal site:
Flag A: Configure it to apply to content types ContentTypeA and ContentTypeB.
Flag B: Configure it to apply only to content type ContentTypeA.
2. Create Content Types:
Ensure you have at least two content types:
ContentTypeA
ContentTypeB
3. Create Content:
Create at least one node of each content type:
Node 1: Content type ContentTypeA.
Node 2: Content type ContentTypeB.
4. Add Flag Links to a View:
Create a View that displays nodes of both content types (ContentTypeA and ContentTypeB).
Add fields for both Flag A and Flag B links to the View.
5. Render the View:
Render the View and observe the flag links:
For Node 1 (ContentTypeA):
Both Flag A and Flag B links should appear and work correctly.
For Node 2 (ContentTypeB):
Only Flag A should appear and work. Flag B should not appear or should be disabled.
6. Identify the Issue:
If the Flag B link appears for Node 2 (ContentTypeB) (even though it should not), this is the issue you’re trying to replicate.

the patch above work but update attaching my version for the patch ...
here is the explanation for the changes in the patch ::

The code has been updated to improve its functionality and maintainability. Instead of using $entity->get('type')->getString(), we now use $entity->bundle() to make the code work for any entity type, not just nodes. Early returns have been added for invalid cases, such as missing entities or flags, to make the logic clearer. The return type has been standardized to an empty array ([]) for consistency with Drupal's rendering system. Additionally, the $view_mode parameter has been included in the getAsFlagLink() call to ensure the flag link respects the view mode. These changes make the code more generic, reusable, and aligned with Drupal best practices.

with the patch that i'm attaching it will generate the flag link as per the content type if the flag is not attached to any content type it will not generate the link itself.

Also attaching the before and after screenshots for the issue for the reference
Moving this to needs Review
for the further verification of the attached patch.

sdhruvi5142’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new205.46 KB
new414 KB

Hi, Applied "3305405_1.patch" successfully and it is working as expected.

Testing Steps:
Thanks @jaydev bhatt for giving such detailed steps. I've followed all the above mentioned steps and observed the changes there.

Testing Result:
After applying the patch, Links aren't rendering when content is not allowed for flag. Attached screenshot for reference. Moving this to RTBC+1

Status - PASS

ivnish’s picture

Status: Reviewed & tested by the community » Needs work

Needs reroll patch to MR

lavanyatalwar’s picture

Assigned: Unassigned » lavanyatalwar

Working on it

lavanyatalwar’s picture

Hi @ivnish, Created the MR.
Kindly review :)

lavanyatalwar’s picture

Assigned: lavanyatalwar » Unassigned
Status: Needs work » Needs review
ivnish’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll

@lavanyatalwar thanks!

MR has 2 problems:

1) phpcs issues
2) tests failed

jaydev bhatt’s picture

Assigned: Unassigned » jaydev bhatt

Working on it.

jaydev bhatt’s picture

Assigned: jaydev bhatt » Unassigned
Status: Needs work » Reviewed & tested by the community

Fix the issues with the PHPUNIT test failing.
The issue here was by default the if there is nothing selected in the flag while creating the content(node) flag it applies to all the content type present in the system. and hence the user follower flag was also not working for the same reason fixed the LinkGeneration code to apply to all the content type if the no content type is selected and fixed the PHPCS issues as well for this.

The MR is ready to merge.
Moving this to RTBC.

ivnish’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

We need a tests to demonstrate that link doesn't render in not allowed entities

rudi teschner’s picture

Short info: Patch 3305405_1.patch from #7 might not work if patch from issue 3139250 is also applied because both change the same function

ivnish’s picture

Version: 8.x-4.0-beta3 » 5.x-dev

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

deaom’s picture

Status: Needs work » Needs review

Added test, ready for review.

ivnish’s picture

anaconda777’s picture

this patch #7 wont apply to latest dev or 5.0.3. (was able to patch the 4.0 beta)

deaom’s picture

That patch in #7 is probably out dated. Create a patch from MR as MR has the latest changes. At the end of the MR link add .patch so you get Patch from MR and that creates a patch from MR.

  • ivnish committed 45a09ca8 on 5.x authored by lavanyatalwar
    fix: #3305405 Links rendered even though content is not allowed for flag...
ivnish’s picture

Status: Needs review » Fixed
Issue tags: -Needs manual testing

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.