2

When I change the input method for an entity reference field from an Autocomplete widget for a Select widget of an admin-side form, I have noticed there are no sorting options available under "Manage form display" and the Select widget is by default sorted by entity ID.

Is there a way to sort the entities in that Select widget by another field (in my case 'title')? I saw a note about this, but that pertains to drupal 7 (and my entity type is custom and not a node extension).

  • 1
    The same process is applicable to Drupal 8 core entity references as it is to Drupal 7 entity references. You can create a view of your entity type with an entity reference display, and tell the field widget to use it under the "Reference Type" configuration. – mradcliffe Sep 16 '16 at 13:55
  • Thanks for the reply @mradcliffe. I'm not sure I'm properly following -- I went to views > add new view, set a view name and set the content type to my entity type, and then within the view set "Fields" to only show title and "Sort" to sort by title and set it to an "Entity reference" instead of "Master" display. I then went back to my form display settings and didn't see it in the Widget dropdown. I'm either misunderstanding something or skipping a step. – saramm1 Sep 16 '16 at 14:48
  • 1
    You need to go to Manage Fields for the Entity Type, click Edit for the respective field. I was wrong. It's not a field widget setting, it's a field setting. Sorry. – mradcliffe Sep 16 '16 at 15:02
4

To enable this functionality in Drupal 8,

  1. Add a view for your entity type.
  2. Create an Entity Reference view display with a field and sort option (See comment from @saramm1 above).
  3. Edit the Field Settings for the entity reference field and change the "Reference Type" to use a view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.