Problem/Motivation

The click sort behavior of Flag link field is not working in views.

It fires the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'unknown' in 'order clause

Proposed resolution

Fix the Flag link field views handler so that it sends query against a real table column to make click sorting possible.

Issue fork flag-2943153

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

Oleksiy created an issue. See original summary.

oleksiy’s picture

Status: Active » Needs review
StatusFileSize
new1 KB
andypost’s picture

Issue tags: +Needs tests

Such kind of bugs are testable

joachim’s picture

Status: Needs review » Needs work
+++ b/src/FlaggingViewsData.php
@@ -47,6 +47,7 @@ class FlaggingViewsData extends EntityViewsData {
+      'real field' => 'uid',

Huh?

Why would click-sorting this sort by user ID? I don't see how this makes sense.

This field is not data; it's function. Not being able to sort on it is correct.

oleksiy’s picture

StatusFileSize
new28.93 KB

I have the following using case, where there is a need to sort Checked\Unchecked content. Also, this field is used to change content state.

The user ID is also defined in "Flagged" field (https://cgit.drupalcode.org/flag/tree/src/FlaggingViewsData.php?h=8.x-4....), which is used to show whether the flag is set or not. This is what I need for Link field to make it sortable. I think any column from the "flagging" table can be used for this purpose since the flag exists only if the record is present in the "flagging" table.

joachim’s picture

> I have the following using case, where there is a need to sort Checked\Unchecked content. Also, this field is used to change content state.

Don't we have a flag status field which could be sorted on?

I can see that in your case you've made the flag link texts look like descriptions rather than actions, but for most people's setup, it won't make sense to sort on 'Flag this item'.

oleksiy’s picture

Status: Needs work » Needs review
StatusFileSize
new563 bytes

Don't we have a flag status field which could be sorted on?

We have "Flagged" field which can be used for the sorting.

I got your point. I have attached another patch where only my specific case is handled. For most people's setup, this field will be the function, not data. The "clickSort()" will be executed only if someone enables click sorting for the field, so they will get sorted content insted of the error.

Status: Needs review » Needs work

The last submitted patch, 7: flag-link_is_not_click_sortable-2943153-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oleksiy’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new10.71 KB
new11.29 KB

Tests added.

Status: Needs review » Needs work

The last submitted patch, 9: flag-link_is_not_click_sortable-2943153-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

andypost’s picture

Very probably it fails because of related issue

socketwench’s picture

Hm. I'm not sure it is. I had the same error on #2955701: Add method to get flag in ConfirmFormBase but a retest made it vanish...

andypost’s picture

Yes, it looks like some random fail https://www.drupal.org/pift-ci-job/929080
Very probably kinda "wait-for-ajax" used cos failed only Ajax tests

socketwench’s picture

oleksiy’s picture

Status: Needs work » Needs review

Tests were passed.

morbus iff’s picture

StatusFileSize
new71.21 KB

I need this as well; my flags are "just" checkboxes (or, rather: FontAwesome square/check-squares) in a table, and I want to be able to visually sort by checked or unchecked. Screenshot attached.

morbus iff’s picture

Status: Needs review » Reviewed & tested by the community

This patch is working great for me.

I'm gonna set this to RTBC, given:

  1. There's tests.
  2. The "Flag link" clickSort matches the prior-art of the "Flagged" clickSort.
  3. There's currently nothing in Flag core that PREVENTS a site builder from setting a "Flagging: Flag link" field, in a View, as "sortable" in a View's Table format. If a site builder does check "sortable", it then causes a PHP error / white screen for end-users who click that table header. This patch prevents that error. If this is uncommitable then we, at least, need a different patch that PREVENTS "sortable" from being checked by a site builder.
  4. Given two use cases in this issue, and the long standing #353534: Add Checkboxes as Display Type, there's clearly enough demand for this sort of functionality for the flag links.
berdir’s picture

Status: Reviewed & tested by the community » Needs work

Agree that the uid sort seems a bit weird, but indeed similar to what the other definition does. That said, I'd also be fine with the initial fix, it's consistent then. But no deal breaker.

What is one, unfortunately, is that this needs some updates to be D9 compatible, can't commit a patch that isn't anymore. Hopefully not too much work, new base class, default theme, D9 compatibility in the test module...

cecrs’s picture

Just wanted to add a comment, I had the same use case as @Oleksiy, where I needed the functional flag/unflag link in the view, but wanted to sort by whether they were flagged or unflagged. I was able to solve it by adding both 'Flagging' and 'Flagged' to my view, rewriting 'Flagged' as 'Flagging', and making 'Flagged' sortable in the table. (And excluding 'Flagging' from display)

Flagging/unflagging links work, and the view sorts correctly.

ivnish’s picture

Issue tags: +Needs reroll

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

mahima_mathur23’s picture

Resolved all PHPCS Issues

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

Status: Needs work » Needs review

Rebased branch and updated the test. Changing status to needs review as I did not manually test it, but tests are passing.

ivnish’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll
deaom’s picture

Failed because the older version of phpstan does not have that method it seems. Replaced the method with the waitForElementRemoved as there is no actual element to check on ajax complete and that method also seems to not be available. As I did not want to complicate things added that specific error message to the phpstan.neon to be ignored and that also fails (as the issue is just with previous version). Open to suggestions as locally can't really reproduce. My POV, it's not a big deal, as it's only test and a previous version, so would leave as is.

ivnish’s picture

Status: Needs work » Fixed

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.