Problem/Motivation

When using the Table style in Views, Link fields are not click-sortable.

Steps to reproduce

  1. Add a Link field to any content type.
  2. Edit the standard Content admin view.
  3. Add the Link field to the view. Note that there is a setting for the field indicating the "Column used for click sorting".
  4. Open the Table format's Settings dialog.
  5. Note that under the Sortable column in the Settings dialog there is no checkbox for the new Link field.

Proposed resolution

Implement hook_field_views_data_alter in the Link module to set "click sortable" to TRUE for field instances.

Remaining tasks

User interface changes

This will provide a checkbox in Views table style/format settings to make Link fields sortable.

Before:
The table format's settings dialog with no option to make a Link field click-sortable.

After:
The table format's settings dialog with a checkbox option to make a Link field click-sortable.

API changes

None.

Data model changes

None.

Release notes snippet

Issue fork drupal-3239705

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

liquidcms created an issue. See original summary.

liquidcms’s picture

The Style inconsistency is even more confusing to people just starting with Views when there is a section in the field config (where that message shows up) called Style.

liquidcms’s picture

Version: 9.2.x-dev » 9.3.x-dev
Status: Active » Needs review
StatusFileSize
new0 bytes

Not sure if this is the correct way to fix it but seems to line up with other fields (although doesn't seem like a lot of consistency with these). This patch replaces the link.views.inc file which possibly just got lost somewhere along the way. It uses hook_field_views_data_alter to set all link fields as "click sortable".

This patch only fixes the click sortable part of this issue. I didn't touch all the wording issues that exist.

Also, i would assume there are tests required here but no idea at what level these are done. Shouldn't there be a global test for testing click sortableness for all fields? Which there must not be or this bug would not have persisted for so long.

liquidcms’s picture

hmm.. nope.. apparently my patch command created an empty file.. investigating..

liquidcms’s picture

this one looks better.

norman.lol’s picture

Status: Needs review » Needs work

Seems it just needs a minor coding standards fix (two lines at end of file while one expected).

liquidcms’s picture

Thanks for the tip. Cleaned up patch is attached here.

Was hoping for comments regarding the approach taken here though and maybe even on the requirement for tests (as in is this a requirement for this core module or should it be handled at a higher level - like maybe for views tests).

norman.lol’s picture

Status: Needs work » Needs review
chetanbharambe’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new483.79 KB
new555.34 KB

Verified and tested patch #7.
Patch applied successfully and looks good to me.

Testing Steps:
# Goto: Appearance -> Apply Seven Theme
# Goto: Structure -> /admin/structure/types/manage/article/fields
# Add "Link" field
# Save it
# Goto: /admin/structure/views/view/content
# Click on "Settings" under Format
# User will able to see added "test link"
# Check the results

Expected Results:
# After applying the patch, the User is able to see the "Sortable Checkbox" for added test link.

Actual Results:
# Currently, the User is not able to see the "Sortable Checkbox" for added test link.

Please refer attached screenshots for the same.
Looks good to me.
Can be a move to RTBC.

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests, +Needs issue summary update

I think this will need a test, adding tag. Also the issue summary needs an update, it should state the proposed resolution and the remaining tasks,

liquidcms’s picture

Issue summary: View changes
vikashsoni’s picture

StatusFileSize
new37.29 KB
new67.84 KB

Applied Patch #47 working fine
After patch we can sort link field
Thanks for the patch
for ref sharing screenshot ...

bnjmnm’s picture

Removing credit for unnecessary screenshots in #12. Screenshots were already provided in #9. This would be best confirmed by tests instead of screenshots anyway (hence the "needs tests" tag in #10)

liquidcms’s picture

Although I have been on d.org for 15 years, honestly not familiar with guidelines for being credited on issues. As useful as verifying a fix is, is simply uploading a screenshot (such as chetanbharambe did) all that is required to receive credit for fixing a problem? Clearly I could have a lot more credit on things had i taken that approach all these years.. lol

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

murilohp’s picture

Status: Needs work » Needs review
StatusFileSize
new2.6 KB
new3.51 KB
new2.44 KB

Hey everyone! I tried my best to create a new test for this issue, I'm uploading three files, the first one is a test-only patch, hoping this fail to validate the issue, the second file is the patch itself and the third one is an interdiff.

I hope you can validate this, and if you have any questions, please let me know.

Thanks!

The last submitted patch, 16: test-only-3239705.patch, failed testing. View results

murilohp’s picture

@quietone, I'll remove the tags, thanks to @liquidcms for updating the issue summary, and about the tests, I'll keep this to needs review to see what you guys think about #16.

Thanks!

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work

After applying the patch I'm not seeing the sortable checkbox next to a link field in the view.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

robbt’s picture

I attempted to apply this patch using composer and the file wasn't created.

When I manually created the file based upon the patch I could indeed click sortable and sort the table based upon the link.

But one issue I noticed is that it sorts based upon the URL rather than the display name which probably isn't ideal for everyone.

I'm also not sure if the best way to accomplish this fix is through using a hook_alter for views rather than fixing the links module directly, but that's going to require more research on my part to determine how other fields tell views they are sortable.

akhil babu’s picture

StatusFileSize
new60.65 KB
new52.66 KB

Applied patch from #16 and it works in both drupal 10 and 11 versions.

https://www.drupal.org/files/issues/2023-12-14/link%20sorting.gif

@robbit, Link field can be sorted based on title by changing Column used for click sorting from URL to title.
https://www.drupal.org/files/issues/2023-12-14/link%20field%20sorting.png

I also checked core to see how other fields have enabled click sorting
Modules such as statistics, dblog etc use hook_views_data() to define fields and add 'click sortable' parameter from there.

Node module uses views_data handler "views_data" = "Drupal\node\NodeViewsData" for node entity and 'click sortable' added to fields from NodeViewsData.

taxonomy uses hook_views_data_alter() to add some fields and click sorting is added from there. So I think current implementation would be fine.

Used by Style: Table to determine the actual column to click sort the field on. The default is usually fine.

This is the default description added to click sort column field.

    // No need to ask the user anything if the field has only one column.
    if (count($field->getColumns()) == 1) {
      $form['click_sort_column'] = [
        '#type' => 'value',
        '#value' => $column_names[0] ?? '',
      ];
    }
    else {
      $form['click_sort_column'] = [
        '#type' => 'select',
        '#title' => $this->t('Column used for click sorting'),
        '#options' => array_combine($column_names, $column_names),
        '#default_value' => $this->options['click_sort_column'],
        '#description' => $this->t('Used by Style: Table to determine the actual column to click sort the field on. The default is usually fine.'),
      ];
    }

Maybe we can exclude "Used by Style:" and add only "Table to determine the actual column to click sort the field on. The default is usually fine" as the description?

akhil babu’s picture

Moving to needs review for #24. If description change is not needed, issue can be moved to RTBC

akhil babu’s picture

Status: Needs work » Needs review
robbt’s picture

So I was able to get this to patch to apply. I realized I needed to set patch level to 2 as described here - https://www.drupal.org/docs/develop/using-composer/manage-dependencies#s... in order to apply a patch to core using composer-patches.

With that working I do think it makes sense to alter the description to say Used by Format: Table rather than Used by Style: Table which I think was from a previous version of Views.

I've created a new patch based upon 11.x that makes this change as well, so if anyone can test and review it then I think we can change it to RTBC as @akhil_babu suggests. My change merely updates the description and is based otherwise entirely on the patch on #16

robbt’s picture

The patch in #27 was failing drupal coding standard tests so I've added a new one that should at least pass the issue it was failing on (a lack of a <?php tag.

Otherwise if this passes the automated tests it should be ready for review.

smustgrave’s picture

Status: Needs review » Needs work

From what I can tell the tests are check that the checkbox is there but doesn't test the sort?

Think we could also extend the tests to include the different column sort.

robbt’s picture

I think that testing the sort is a good idea. I haven't written any tests for a Drupal core contribution yet but I'm willing to give it a shot.
In looking at the code it appears that web/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php has a test for testing click sorting that could be used as the basis for a new test of this functionality.

Also I see web/core/modules/views/tests/src/FunctionalJavascript/ClickSortingAJAXTest.php which tests the same thing via AJAX.

In theory I think it would make sense to also test the functionality of sorting based upon the title (if it is enabled) and also based upon the uri (the default). I'm not sure how to mock this test up exactly but I'll be digging into other tests and reading the documentation to try to figure it out but any help or pointers in the right direction would be appreciated.

From a UI/UX perspective I would think that if a link field uses a title/display that would be the default sort chosen since that is what usually displays but that would require further changes to this code and probably views (beyond updating the description). I also don't know what sorting by the options would mean and if this even makes sense to include.

robbt’s picture

I'm here at DrupalCon Atlanta in the mentored contribute @partyka and we looked under the core directory and don't see any other fields that are have their 'click sortable' tested. I believe that whether a field gets sorted or not is actually handled by Views code and so adding the ability to sort links shouldn't require a test of the sort. Going to push the code and update this to a MR.

robbt’s picture

This is currently not working because it uses the deprecated .views.inc approach that was removed from D11. It needs to be refactored to use ModuleViewsData.php to extend EntityViewsData in order to set click sortable to true. Although typically click sortable is enabled by default.

robbt’s picture

It looks like the appropriate thing to do for D11 compatibility is to change this to a views hook - see the commit https://git.drupalcode.org/project/drupal/-/commit/1da8aa097417905a9772d... and this issue https://www.drupal.org/project/drupal/issues/3489415

Essentially just need to rewrite the .views.inc to be compatible with the specific hook type.

Like for instance https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/taxon... replaced https://git.drupalcode.org/project/drupal/-/blob/10.5.x/core/modules/tax...

dcam changed the visibility of the branch 11.x to hidden.

dcam’s picture

Category: Bug report » Feature request
Issue summary: View changes
Status: Needs work » Needs review

I'm reclassifying this as a feature request. It may seem like a bug because of "Column used for click sorting" setting, but Views adds that setting anytime a field has multiple columns. The inclusion of that setting element for fields that can't be click-sorted may be a bug and a follow-up issue could be opened for it. But in the end, this issue is asking for a feature to be added that wasn't present before now rather than fixing something that was broken.

I converted the patch from #28 to an MR, making several modifications along the way. Mainly, the changes were upgraded to the most current APIs, including the change to a Hook class as noted in #33. I also removed the text change in EntityField. Not only do I think it's out-of-scope for this issue, a change to a translatable string in a separate module may make it more difficult to get this change in. If you want to see the text changed, then open a follow-up issue.

From what I can tell the tests are check that the checkbox is there but doesn't test the sort?

Think we could also extend the tests to include the different column sort.

Personally, this feels like a "Shouldn't we trust the Views API to be tested and do its job?" kind of thing. I'm not sure writing a Functional or FunctionalJavascript test to test the functionality of a separate module adds much of value to this one. Hence, I'm setting this back to Needs Review.

dcam’s picture

Title: Link fields are not sortable in Views. » Link fields are not click-sortable in Views tables
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Test coverage can be found here https://git.drupalcode.org/issue/drupal-3239705/-/jobs/7436803

I did verify this on a standard profile install
Added a test_link field to the Article content type
Used the admin content view for testing and yup can't sort by link
Applying MR I can now check the box

Left some comments in the MR.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.