Problem/Motivation
Follow-up from #2107427: Regression: Language names should display in their native names in the language switcher block.
The language switcher block sorts languages by weight then title, instead of weight then label.
From @penyaskito's comment:
After changing that tests were failing, and the culprit was Language::sort. This calls SortArray::sortByWeightAndTitleKey($a, $b, $weight_key = 'weight', $title_key = 'title') with the default optional parameters. The title key is "title" when it should be "label" or "name". I found that we are not calling that passing the optional params anywhere, and by using uasort there is no way of passing that, so I just cloned it to sortByWeightAndLabelKey.
Proposed resolution
Change sorting to weight then label.
Remaining tasks
- (novice) git instructions for creating patch | Contributor task documentation for creating a patch
- Review patch to check it fixes the issue, the change is properly documented and for coding standards. Make sure patch stays within scope of just this issue. | Contributor task documentaiton for reviewing patch
User interface changes
None.
API changes
None.
Comment | File | Size | Author |
---|---|---|---|
#52 | 2239399-language-sort-by-label-52.patch | 568 bytes | vijaycs85 |
Comments
Comment #1
alimac CreditAttribution: alimac commentedComment #2
alimac CreditAttribution: alimac commentedComment #3
alimac CreditAttribution: alimac commentedComment #4
mashermike CreditAttribution: mashermike commentedYesCT told me to create this patch. It adds a sorting by label instead of title.
Comment #5
penyaskitoComment #6
penyaskitoLooks good.
It's curious that this patch passes without having to modify any tests. The test coverage will be added as soon as #2107427: Regression: Language names should display in their native names in the language switcher block lands, so I would unblock that one RTBCing this one.
Comment #7
YesCT CreditAttribution: YesCT commentedWe need tests on this one.
Also, I'm not sure this is implemented in a sane way.
We are in core, so we can just do this, but how would this be done in a way contrib could do it?
Comment #8
tim.plunkettAdding a new method to the Utility class is wrong here.
If this change had been along the right lines, this is the correct change:
However, #2151223: Add a method to SortArray to sort by weight and title switched from name to title, and this switches to label, but the Language class does *not* have a label or title!
Comment #9
penyaskitoTotally agree with tim.plunkett, I didn't thought of an anonymous function!
Comment #10
penyaskitoComment #11
penyaskitoAdded phpunit tests, and changed the sort method according to @tim.plunkett advice at #8.
Comment #14
penyaskito11: 2239399-11.patch queued for re-testing.
Comment #16
penyaskito11: 2239399-11.patch queued for re-testing.
Comment #17
YesCT CreditAttribution: YesCT commentedan unrelated change in newline. just undoing that so the patch from 8.x is cleaner.
note, if this is committed, it makes #2151223: Add a method to SortArray to sort by weight and title not easy to revert.
Comment #18
YesCT CreditAttribution: YesCT commentedlet's get a patch here that does not use that sortByWeightAndTitleKey()
the tests here are still good.
Comment #19
tim.plunkettNevermind, we're keeping it after all, let's just get this in. Thanks @penyaskito!
Comment #20
YesCT CreditAttribution: YesCT commentedok. super.
Comment #22
Gábor Hojtsy17: 2239399-17.patch queued for re-testing.
Comment #24
penyaskito17: 2239399-17.patch queued for re-testing.
Comment #25
penyaskitoLast one looks random failure, I created #2254011: Drupal\system\Tests\Entity\EntityQueryTest random failure
Comment #26
Gábor HojtsyThe fix looks good. Its confusing that language config entities have a label while language runtime objects have a name. That would be resolved with #2246679: Make Language module's LanguageInterface (to be ConfigurableLanguageInterface) extend Core's LanguageInterface which is still a bit off. So we need this fixed in the meantime.
Comment #28
penyaskitoComment #29
penyaskito17: 2239399-17.patch queued for re-testing.
Comment #31
Gábor Hojtsy17: 2239399-17.patch queued for re-testing.
Comment #33
Gábor HojtsyThis is a blocker for #2107427: Regression: Language names should display in their native names in the language switcher block, would be good to get this going so we can move onto that which is in itself a blocker for more language name display issues.
Comment #34
penyaskitoSpent some time debugging the errors without any luck. BTW locally, without the patch (or with SortArray::sortByWeightAndTitleKey($a, $b, 'weight', 'title');), is always green, but with the current patch at #17 sometimes is green, sometimes I got 5 errors.
Comment #35
penyaskito17: 2239399-17.patch queued for re-testing.
Comment #37
marthinal CreditAttribution: marthinal commentedThere's no "title" as key when ordering the values. This is why the test is always green I think.
Looks like the problem is here:
If this method (sortByKeyString) returns 0, 1 or -1 the test will be green. Maybe a problem with chars when using strnatcasecmp()... not sure. Anyway I would like to know if everything is correct when the result is -1 fror example.
Comment #38
tim.plunkettThis is incorrect. Please remove this change.
Your other change should have been enough.
Comment #40
marthinal CreditAttribution: marthinal commentedFailures at #37 are about Sort Array and this is normal. @tim.plunkett I only wanted to check the test results.
Failures at #17 were about EntityQuery and looks like the problem could be the explained at #37.
With only this change:
return SortArray::sortByWeightAndTitleKey($a, $b, 'weight', 'label');
the test will fail with the same errors (#17). So needs work :)
Comment #41
penyaskitoSorry for not providing feedback, looks like we are all in the same issue but not in the same page.
I asked tstoeckler for help and we have met today for taking a look together. What was clear that there is a random fail, because running the test with patch at #17 repeatedly shows errors, but not consistently:
I'm debugging this tonight and I think I found the cause. IMO the problem is in HEAD, it is just not exposed because we are sorting by "label", but there is no label on languages, so we are comparing "" with "" and that works.
However, the random fails happen when the language name meet concrete conditions. I'm now investigating which conditions would that be.
Comment #42
eugenesia CreditAttribution: eugenesia commentedI'm working on this now at the Drupal London Code Sprint.
Comment #43
YesCT CreditAttribution: YesCT commentedcool. hop in #drupal-contribute and #drupal-i18n on irc.freenode.net :)
Comment #44
eugenesia CreditAttribution: eugenesia commentedThis patch fixes the failed tests in patch #37. It does the following:
\Drupal\Component\Utility\SortArray::sortByWeightAndTitleKey()
as suggested in comment #38.label
property forLanguage
objects, so that they can be tested for sorting by weight and label - the main reason for creating this issue. Patch #37 did not provide alabel
property for testLanguage
objects, which caused the tests to fail.However this brings up a few questions which I hope someone can answer:
providerTestSortLanguages()
created objects of class\Drupal\Core\Language\Language
(the "Core\Language" class) for sorting by weight and label. However, these objects do not have alabel
property upon creation by default, unless a specific label key is passed in. Why not?label
property? If so, what is the point of sorting them by weight and label, since an object might not have a label?\Drupal\language\Entity\Language
class (the "Entity\Language class") has alabel
property. Should this class have been used to create the test objects instead? However the test was for theLanguage
class'ssort()
method. The "Entity\Language" class does not have asort()
method for testing, which means we don't have anything to test.This patch was created with much advice and clarification from @vijaycs85 at Drupal London Code Sprint May 2014. Thanks Vijay!
Comment #45
eugenesia CreditAttribution: eugenesia commentedUnassigning myself as I've created and uploaded the patch in #44.
Comment #46
tstoecklerPatch itself is RTBC.
Just verified, though, that this still results in random failures in EntityQueryTest. Thus marking postponed on #2254011: Drupal\system\Tests\Entity\EntityQueryTest random failure :-/.
Comment #47
YesCT CreditAttribution: YesCT commentedI dont see a label property on core Language/Language
was this assuming language module Entity/Language ?
Comment #48
YesCT CreditAttribution: YesCT commentedeither or both of these might help prevent this confusion.
Comment #49
YesCT CreditAttribution: YesCT commentedThere is a sort on Entity/Language inherited from ConfigEntityBase
Comment #50
YesCT CreditAttribution: YesCT commented#2304651: Core Language sort() should not work, needs to use name instead of default title is in. So we should try and redo this on top of that. This is high on my priority list, but if someone works on it before me that is ok also. Be sure to post on the issue or ping me on irc.
My worry when I looked at this last week was that #47.
Comment #51
YesCT CreditAttribution: YesCT commentedWe did the rename. #2271005: Rename Language module's LanguageInterface to ConfigurableLanguageInterface and Language to ConfigurableLanguage
so let's come back to this.
but sort is not required by the interface. Maybe sort should specified on the (core) LanguageInterface. That seems like a separate issue....
Comment #52
vijaycs85Looks like, this issue become one line patch.
Comment #53
Gábor HojtsyHow is this tested?
Comment #55
vijaycs85ok, we don't have label anymore. it is just 'name'. It means, all part of this issue has been covered by #2271005: Rename Language module's LanguageInterface to ConfigurableLanguageInterface and Language to ConfigurableLanguage. this issue can be closed as duplicate (sadly). Would be great to get the commit credit for the people contributed in this issue in another followup
eugenesia, marthinal, YesCT, penyaskito, mashermike | alimac
LanguageUnitTest::testSortArrayOfLanguages covering it already.
Comment #56
Gábor HojtsyYeah I agree it is sad that the people working on this were not credited :/ On the other hand, #2107427: Regression: Language names should display in their native names in the language switcher block may also be worked on now (unpostponed!), which will be amazing to have in core finally :) Thanks all for contributing here and don't let the process hurdles let you down.