Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Updated: Comment #N
Problem/Motivation
Avoid multiple one off implementations for a common case of sorting by weight and title.
Proposed resolution
Add a SortArray::sortByWeightAndTitleKey method.
Remaining tasks
- (done) patch committed was #4.
- Draft a change record | Contributor task document for writing a change record
User interface changes
None
API changes
None, addition of helper method.
Comment | File | Size | Author |
---|---|---|---|
#20 | drupal8.language-sort.20.patch | 895 bytes | sun |
#12 | sort-2151223-12.patch | 4.74 KB | tim.plunkett |
#4 | interdiff-2151223-8.txt | 2.69 KB | damiankloip |
#4 | 2151223-8.patch | 4.71 KB | damiankloip |
Comments
Comment #1
damiankloip CreditAttribution: damiankloip commentedHere is a start.
Comment #2
damiankloip CreditAttribution: damiankloip commentedOne potential issue: Alot of sort implementations are action on class properties, not arrays. So to make the maximum reuse out of this, maybe we need methods that can work on object properties too?
Comment #3
dawehnerAwesome test coverage. I wonder whether we can use it somewhere already in the patch.
Comment #4
damiankloip CreditAttribution: damiankloip commentedWhat about something like this?
Comment #6
tim.plunkett4: 2151223-8.patch queued for re-testing.
Comment #7
pkunwar CreditAttribution: pkunwar commented2151223-8.patch seems to be good.
Comment #8
pkunwar CreditAttribution: pkunwar commentedComment #9
xjm4: 2151223-8.patch queued for re-testing.
Comment #10
catchCommitted/pushed to 8.x, thanks!
Worth adding a change notice for the addition.
Comment #11
tim.plunkettWhy did we switch from an object with a name property to an array with a title key?
Comment #12
tim.plunkettOut of curiosity, testing a revert to see what happens.
Comment #13
xjmLet's wait on the change record until we fix this. :) We can also just create a new draft here and publish it when we commit the fixed patch.
Comment #14
YesCT CreditAttribution: YesCT commentedI think head is silently failing because of missing tests? So this is needs work to get a patch with tests.
Comment #15
damiankloip CreditAttribution: damiankloip commentedYeah, seems like we might as well revert this? Not sure why I thought we should add this. It's not that useful really, if you have different keys you need to use.
Comment #16
penyaskitoI looked at this and I don't know what is the next step.
ArraySort::sortByWeightAndTitleKey it is useful for avoiding duplication, just maybe rename it to sortByWeightAndLabel key and use label by default (I thought there was an issue for that, but cannot find it right now).
In #2239399: Languages should be sorted by label instead of title we are using an anonymous function in sort() for calling this with additional params, so we could use similar patterns in other places.
#11: I don't know, but it allows to reorder arrays and objects in the same way, and reuse sortByKeyInt et al. We could check if this function is needed for arrays anymore and remove the array casting, which probably is a bad thing in terms of performance.
So, as far as I got, @ŧodos:
1. Write a change notice for commit at #10.
2. Check #11, in a follow-up.
3. Rename sortByWeightAndTitleKey() to sortByWeightAndLabelKey(), in a follow-up (which maybe already exists).
Am I right? Still confused.
Comment #17
tim.plunkettOther than language, which is broken, there are no other usages of this in core.
I don't think sortByWeightAndTitleKey is actually that reusable.
Even if we had another use case for it, what if it wanted to sort first by Title and *then* by weight?
I think Language::sort should just use SortArray::sortByKeyInt and SortArray::sortByKeyString in the anonymous function.
Reverting this would let us use the test coverage from the other issue, and I think that's enough.
Comment #18
sunBecause the class is named SortArray...?
Comment #19
YesCT CreditAttribution: YesCT commentedI agree with reverting this.
Comment #20
sun@tim.plunkett kindly explained the bug/regression to me in IRC.
However, I don't think that really requires a full revert of this patch. The method is useful, but admittedly, it isn't obvious how you can leverage it. The trick is to wrap it into a mini closure, so you can pass custom arguments:
I'm very confident that I'm actively using this method in another issue/patch/branch, but I wasn't able to find it.
Comment #21
YesCT CreditAttribution: YesCT commentedthat patch is #2239399: Languages should be sorted by label instead of title. but it has test coverage.
Comment #22
tim.plunkettOkay, so this is just back to needing a change notice, and we can follow-up in the language issue.
Comment #23
YesCT CreditAttribution: YesCT commentedok.
Comment #24
YesCT CreditAttribution: YesCT commentedclarified in issue summary remaining tasks.
Comment #27
xjmComment #28
catchTagging novice for the change record.
Comment #29
catchThis is a (very) minor API addition, not an API change at all, so I don't really think it's worth adding a change notice for.
Comment #31
YesCT CreditAttribution: YesCT commentedThis is still troubling...