Problem/Motivation
I noticed this problem while working on #3089525: Sort options should correspond to bundles selected for entity reference field settings.
Using the Standard profile,
- Visit
/admin/structure/types/manage/article/fields - Click the "Add field" button.
- Select Content under the Reference group.
- Enter a label, then "Save and continue".
- On the next screen, click the "Save field settings" button.
- In the "Reference type" section, select at least one content type. This will expose the "Sort by" select list.
- Select a "Sort by" field. The "Sort direction" select list should appear.
- De-select all content types.
- The "Sort by" option is hidden, but the "Sort direction" option is still there.
We have to be careful that the fix for this problem does not break reference fields for entity types, like User, that do not have bundles.
Proposed resolution
TBD
Remaining tasks
User interface changes
When no bundles are selected, the "Sort direction" option is hidden as well as the "Sort by" option.
Current behavior:

API changes
None
Data model changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #57 | After applying patch #54 .png | 21.82 KB | janmejaig |
| #55 | before patch #54 .png | 29.32 KB | janmejaig |
| #54 | 3158651-54.patch | 5.3 KB | sarvjeetsingh |
| #2 | 3158651-2-test-only.patch | 1.4 KB | benjifisher |
| sort-direction-visible.png | 19.74 KB | benjifisher |
Comments
Comment #2
benjifisherHere is a failing test. I will set the status to NR, and the testbot should set it back to NW.
Comment #4
benjifisherThe patch in #2 failed in the expected way. So far, so good! now we just have to fix the bug. ;)
In
core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php, I see the following:I am not sure where the configuration is set, but presumably from
$form_statewhen the form is submitted. (Also, there is an AJAX submission when the Content type checkboxes are changed.)Comment #5
benjifisherOops, I did not mean to set the status to NR. It is still NW.
Comment #6
mayurjadhav commentedComment #7
himanshu_sindhwani commentedI tried to fix the issue with a small change in conditions. Please review.
Comment #9
himanshu_sindhwani commentedChanging the test case according to the fix in patch.
Comment #11
Kumar Kundan commentedComment #12
paulocsAlso if we apply patch #9, it will not be able to choose "Sort direction" if it is used a User field reference.
Comment #13
paulocsCreated a new patch but it needs to re-write the tests.
Comment #14
paulocsPatch with the test.
Comment #15
paulocsNew patch with code standard fix.
Comment #18
mayurjadhav commentedUpdated the patch for failed test for #7, Kindly review.
Comment #19
Kumar Kundan commented#11 assigned by me to work on this issue. @mayurjadhav & @paulocs you hijacked the issue. I request you to follow the drupal issue etiquette & show your respect to other contributers.
Comment #20
Kumar Kundan commentedComment #21
paulocsSorry @Kumar Kundan, I thought it would be no problem to work in the issue.
Now about the issue, as I said in comment #12, if we apply patch #18, it is not possible to choose a "Sort direction" if the field is a user reference.
Try to fix the tests from patch #15 @mayurjadhav.
Cheers, Paulo.
Comment #22
benjifisher@Kumar Kundan:
Thanks for the link to the page on issue etiquette. Item 8 on that page is
If you want others to follow the guidance on that page, then please set a good example and leave such a comment when assigning an issue to yourself.
When I created this issue, I intended to work on it myself, but I respected the assignments in #6 and #11.
In the issue summary, I wrote,
and @paulocs points out that the patches so far did not pay attention to that.
I am assigning this issue to myself:
Comment #23
benjifisherHere is a new test-only patch, along with an interdiff comparing it to the one in #2. It makes two changes to the tests:
After de-selecting all bundles and checking that the sort order is not visible, re-select them so that the rest of the test will continue as before.
Test that User references can be sorted, and that the sort direction can be selected, as discussed in my previous comment.
Comment #24
benjifisherComment #25
benjifisherComment #26
paulocsAs @benjifisher allowed, I unassigned the issue and I attached a new patch with a possible solution for the issue.
Lets see what the tests bots will say.
Cheers, Paulo.
Comment #28
benjifisherIf you want to use
#statesto control visibility, then$this->assertNoFieldByName('settings[handler_settings][sort][direction]');to test visibility.Maybe (2) is not a problem. It might default to ascending.
Are you set up to run functional javascript tests locally? The feedback loop is painfully slow if you have to wait for the testbot to check your work.
Since
$form['target_bundles']has'#ajax' => TRUE, the form is rebuilt whenever you change the selected bundles. I spent some time on Saturday trying to figure out how to get$configuration['sort']['field']to update properly. It seems thatvalidateConfigurationForm()is called, and I tried updating$this->configurationor$form_statethere, but I could not get it to work. I know that$this->configurationis set from the constructor, but I am not sure what values are passed to the constructor.Comment #29
paulocsHey @benjifisher!
I did a new patch for it and I change also the tests. As I'm still learning how to create tests, I don't if I wright the best test code but I could manage it.
Please review it.
Cheers, Paulo.
Comment #31
paulocsNew patch with the right tests.
Comment #32
benjifisher@paulocs:
Is there a reason you use
$page->find()instead of$page->findField()here?I think my patches defined
$sort_directionusingfindField(). It makes the test easier to read if we do similar tasks the same way. Also, CSS classes are more likely to change than field names, so your version is more likely to need updates in the future.Same snippet: use the
isVisible()method. The same argument for consistency applies. It is also more robust: thestyleattribute might end up being something likedisplay: none; font-size: larger;. It would still be hidden, but your$isDisplayedwould be set toTRUE.Same snippet: under Functions and variables in the Drupal coding standards, it says, "Be consistent; do not mix camelCase and snake_case variable naming inside a file.".
Use
findField()andisVisible()elsewhere as well. I will not list all the places they can be used.Why make this change? It makes your patch harder to review, so only change it if there is a reason to.
I am nervous about the general approach, adding an input field to the form but hiding it in some cases. Does that mean some values will get processed that should not be there? Maybe it is the right thing to do: the form has probably been around for a long time, and the
#statesproperty was not always available. The fact that it passes tests is a good sign. Sometimes it is right to be bold rather than cautious.Comment #33
paulocsHello @benjifisher,
I really appreciate your feedback! It makes me know more about Drupal and tests :)
As you pointed I made a new patch with some fixings:
1) I used $page->find() because I was searching the select field directly. I didn't know I could get the div field by name and use the isVisible() function. I fixed it in my new patch.
2) Now the patch is using the isVisible() function to verify the field visibility so even if the field style changes, the tests will not fail.
3) I fixed the drupal code standards!
4) New patch uses findField() and isVisible().
5) About what you pointed in topic 5, I'm still using
$assert_session->assertWaitOnAjaxRequest();because the tests will fail if I use$assert_session->waitForField('settings[handler_settings][sort][direction]');. I suppose that is because of the #states property.6) I think it is no problem to use the #states property as you said and the tests didn't fail.
The interdiff file name is interdiff-32-33 but suppose to be interdiff-31-33.
Thanks!
Paulo.
Comment #34
thalles#33 work fine to me!

Before:
After patch in #33

Comment #35
quietone commented@thalles, thank you for manually testing the patch and making the screen shots. It will help reviews (and comitters) if you add the screens shots to the issues summary and noting the number of the patch you tested. Thanks.
The issue summary shows that the proposed resolution is TBD - that needs to be completed.
I applied the patch and found that a deprecated method is being used. Since it will be NW for that I also have a query about long lines.
These are hard to read on one line, can we make them multi-line?
assertFieldByName is deprecated.
Comment #36
abhisekmazumdarUpdating the last patch.
Comment #37
abhisekmazumdarHi @quietone
Thanks for you feedback.
I see the use of
assertFieldByNamein many places In the filecore/modules/field/tests/src/FunctionalJavascript/EntityReference/EntityReferenceAdminTest.phpand many other places too, so it will better if the clean up work is done other issue. As of now I have just replaced the code which are realated to the #33 patch.Comment #38
himanshu_sindhwani commentedFew observations:
remove trailing space here.
This is not broken to multi lines.
Comment #39
abhisekmazumdarOops My bad. Update the patch correctly. Thanks @himanshu_sindhwani
Comment #40
himanshu_sindhwani commentedIts good to add a comma after value '_none'.
Please correct the indentation here and also add comma after the value TRUE.
Comment #41
Vidushi Mehta commentedAdded above mentioned correction.
Comment #42
paulocsI think now it is everything okay. Patch #41 looks good.
Set to RTBC.
Cheers, Paulo.
Comment #49
paulocsI don't know why the tests are running on patch #24 if it not the last patch attached.
I will hide patch #24.
Comment #51
sarvjeetsingh commentedAdded a comma after the last array element.
Comment #53
tanubansal commented#51 is not working on 9.1
Please check
Comment #54
sarvjeetsingh commentedI have re-rolled the patch in #51.
core/modules/field/tests/src/FunctionalJavascript/EntityReference/EntityReferenceAdminTest.phphad conflicts, see https://www.drupal.org/project/drupal/issues/3139406please review
Comment #55
janmejaig commentedI have verified the above ticket and found that after applying patch #54 on Drupal 9.1.x it is working as expected.Please find the attachment for the same.
Please find the steps below to test the issue :
Expected : When no bundles are selected, the "Sort direction" option is hidden as well as the "Sort by" option.
Comment #56
paulocsComment #57
janmejaig commentedComment #61
catchCommitted/pushed to 9.2.x and cherry-picked to 9.1.x, thanks!
Comment #62
jibran#3132676: Use progressive disclosure on sort direction element of entity reference fields seems like a duplicate of this.
Comment #69
jibranBringing the tags and credits over from duplicate issue #3132676: Use progressive disclosure on sort direction element of entity reference fields.
Comment #70
benjifisherI guess I am the one who created the duplicate issue. Thanks for connecting them, @jibran!
Comment #71
xjm