Problem/Motivation
The exposed sort identifiers are not configurable in the same way as the exposed filters identifiers. This leaks some internal IDs in the URL and that we can consider part of the UI, even is in the browser address bar, not in the page. For example, with Search API, the user might see such parameter/value in the query string &sort_by=search_api_relevance
, which is not elegant at all.
Proposed resolution
Allow the exposed sort identifiers to be configured.
Remaining tasks
None.
User interface changes
New, textfield element in the exposed sort configuration subform.
API changes
New parameter string $handler_type
in \Drupal\views\Plugin\views\display\DisplayPluginBase::isIdentifierUnique()
signature.
Data model changes
None.
Release notes snippet
When exposing a sort in the exposed form, the site builder is able to configure the identifiers that are exposed in the URL as query parameter values.
Original report
While working on a Rest API via Views, I noticed that exposed Sorts could not have custom identifier likes Filters could. As such, I have created a (probably terrible) patch to add in this functionality.
Please comment on items that need to be changed or fixed and I will be happy to do so!
Comment | File | Size | Author |
---|---|---|---|
#50 | 2897638-50.patch | 38.27 KB | KapilV |
#48 | 2897638-48.patch | 38.27 KB | Giuseppe87 |
#43 | 2897638-43.patch | 39.01 KB | claudiu.cristea |
Issue fork drupal-2897638
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
Comment #2
PaulDinelle CreditAttribution: PaulDinelle at Fenix Solutions Inc commentedComment #4
PaulDinelle CreditAttribution: PaulDinelle at Fenix Solutions Inc commentedRe-rolled patch for 8.5.x-dev.
Comment #5
drclaw CreditAttribution: drclaw at Fuse Interactive commentedThis is great! Code looks good and works as advertised!
Comment #7
LendudeAdding this to get this in line with exposed filters makes sense.
the additional option needs to be added to the config schema, hence the fails. This also means that this would need an upgrade path.
Also, this needs tests.
Comment #12
istavros CreditAttribution: istavros at Netstudio commentedChanged the patch to apply to core 8.8.2
Comment #13
istavros CreditAttribution: istavros at Netstudio commentedFixed patch for core 8.8.2
Comment #14
istavros CreditAttribution: istavros at Netstudio commentedHope I got it right this time. :p
Comment #15
drclaw CreditAttribution: drclaw at Fuse Interactive commentedUpdate for 8.8.4 - Removed the label escaping per #2956722: Exposed sort label is double-escaping special characters (apostrophe)
Comment #17
claudiu.cristeaThis is a new config key and needs a schema. It might be the source of so many failures.
Assigning to try a fix.
Comment #18
claudiu.cristeaHere's a schema fix.
Comment #20
claudiu.cristeaAdded a tested upgrade path.
Comment #21
claudiu.cristeaThis is weird, I cannot replicate locally some of the failures. Still fixed one.
Comment #23
claudiu.cristeaLet's be strict.
This code is almost the same with the one from filter. Let's avoid code duplication.
In fact we need to add the default value also for non-exposed sorts.
Keep assigned to fix the above.
Comment #24
claudiu.cristeaFixed #23, still needs tests.
Comment #25
claudiu.cristeaForgot the
@group
tag for the new test.Comment #26
claudiu.cristeaFixing title, IS.
Comment #27
claudiu.cristeaComment #28
claudiu.cristeaA patch for 8.9.x for whom may concern.
Comment #31
claudiu.cristeaMore default config to be fixed...
Comment #32
claudiu.cristeaComment #33
claudiu.cristeaAdding tests for custom sort identifier functionality.
Comment #34
claudiu.cristeaComment #35
claudiu.cristeaComment #37
claudiu.cristeaIt was a random failure. Back to NR.
Comment #38
LendudeThis looks great.
The one thing I would like to see added to the test coverage is that the 'uniqueness' validation also works cross handler, so we are sure that any collisions between exposed filter identifiers and sort identifiers is also detected. Looking at the code in
\Drupal\views\Plugin\views\display\DisplayPluginBase::isIdentifierUnique
, it should work as it stands now, but since currently this code only needs to work for filters I think it would be good to add test coverage for that.Comment #39
claudiu.cristeaRelated to #8, @Lendude and me, we had a discussion on Slack #contribute channel:
Comment #40
claudiu.cristeaFixed #39. Updated IS & change record.
Comment #42
claudiu.cristeaThinking more on #40. This change is a little risky if a module created a handler, other than filter which adds its query string params. Also it changes the behaviour of the method, We cannot anticipate if the method is used for other reasons in contrib/custom code. So, maybe is safer to keep it as it is and use a custom code in
SortPluginBasethat
only checks for sort handlers.Comment #43
claudiu.cristeaReroll
Comment #44
LendudeDon't think we do this in core?
As pinged on Slack, should we consider not naming the sort identifier 'identifier' in config, so we prevent the logic in isIdentifierUnique() from triggering?
Comment #47
claudiu.cristeaI think #44 is a great idea as long as exposed sort identifier are a different kind of fish than the filter identifiers. I've renamed
identifier
tofield_identifier
as it refers to the field being sorted on.Comment #48
Giuseppe87 CreditAttribution: Giuseppe87 commentedRe-rolled the patch for 9.1.6 - first time I do for a so long patch, I hope I've done right but wouldn't hurt if someone could check it.
Comment #50
KapilV CreditAttribution: KapilV as a volunteer and at Innoraft for Drupal Care, Drupal Association commentedReroll patch for 9.2.x.
Comment #52
claudiu.cristeaFixed https://www.drupal.org/project/drupal/issues/2897638#mr54-note4976
Comment #53
claudiu.cristea@KapilV, the 9.2 (official) change is in https://git.drupalcode.org/project/drupal/-/merge_requests/54 merge request
Comment #54
LendudeHard one to review with all the views getting changed, but tried to focus on the non-config changes, and I think this looks good now. Nice work!
Comment #56
SpokjeMR !54 needs a rebase against
9.3.x
. Only the original creator of the MR can do so.Comment #57
claudiu.cristeaRebased.
Comment #58
Giuseppe87 CreditAttribution: Giuseppe87 commentedHow can I apply the latest version to my project?
I know how to apply a patch with composer, but I don't understand how to do with this merge request.
Comment #59
catchThis looks good to me, but it needs another rebase.
@Giuseppe87 you can use https://git.drupalcode.org/project/drupal/-/merge_requests/54.diff - although to get a stable patch to apply, it's best to download this to a /patches directory in your codebase, and then add the local patch path to composer.
Comment #60
SpokjeRerolled against latest
9.3.x
.Back to RTBC per #54.
Comment #62
catchCommitted/pushed to 9.3.x, thanks!
Tagging for highlights since this is a UX improvement.
Comment #63
SpokjeNow with even more accurate tags! 😇
Comment #65
quietone CreditAttribution: quietone at PreviousNext commentedPublish the CR