Filter options ignoring sort criteria of entity reference display

Created on 14 September 2023, 10 months ago
Updated 26 September 2023, 9 months ago

(Issue also present in 8.x-1.5.)

Summary: My entity reference includes relationships that are necessary for the sort criteria. However, these criteria are ignored when the filter options are displayed.

I have a website with nodes arranged hierarchically via the menu. If I create a view of such nodes, I can mirror the menu structure in an Entity Reference display by:

  • creating a relationship between each node and its menu link (via Menu Entity Index )
  • creating a relationship between each node and its parent's menu link (via Menu Entity Index )
  • creating a relationship between the parent menu link and the parent node (via Menu Entity Index )
  • formatting the display as using TreeHelper (Adjacency model), with the node ID as the Main field and the parent ID as the Parent field (via Views Tree )
  • sorting by node weight then parent weight

The preview shows the rows sorted correctly. However, when I use this display as the filter source for another view, the sort criteria are effectively ignored.

The view (with irrelevant displays removed) is as follows…

uuid: 75af3e80-cdbc-44b0-a5b5-e6511384c14b
langcode: en
status: true
dependencies:
  config:
    - node.type.page
  module:
    - menu_entity_index
    - menu_link_content
    - node
    - user
    - views_alias_filter
    - views_conditional
    - views_field_compare
    - views_tree
id: test
label: Test
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
  default:
    id: default
    display_title: Default
    display_plugin: default
    position: 0
    display_options:
      fields:
        nid:
          id: nid
          table: node_field_data
          field: nid
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: node
          entity_field: nid
          plugin_id: field
          label: ID
          exclude: true
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: number_integer
          settings:
            thousand_separator: ''
            prefix_suffix: true
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
        level:
          id: level
          table: menu_entity_index
          field: level
          relationship: entity_id
          group_type: group
          admin_label: ''
          plugin_id: standard
          label: Level
          exclude: true
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
        title:
          id: title
          table: node_field_data
          field: title
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: node
          entity_field: title
          plugin_id: field
          label: ''
          exclude: true
          alter:
            alter_text: true
            text: '{{ views_conditional_field }} {{ title }}'
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: false
            ellipsis: false
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          settings:
            link_to_entity: true
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
        nid_1:
          id: nid_1
          table: node_field_data
          field: nid
          relationship: node_id
          group_type: group
          admin_label: ''
          entity_type: node
          entity_field: nid
          plugin_id: field
          label: 'Parent ID'
          exclude: true
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: number_integer
          settings:
            thousand_separator: ''
            prefix_suffix: true
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
        views_conditional_field:
          id: views_conditional_field
          table: views_conditional
          field: views_conditional_field
          relationship: none
          group_type: group
          admin_label: ''
          plugin_id: views_conditional_field
          label: ''
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          if: level
          condition: eq
          equalto: '3'
          then: '— {{ title }}'
          then_translate: 0
          or: '{{ title }}'
          or_translate: 0
          strip_tags: 0
      pager:
        type: none
        options:
          offset: 0
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      access:
        type: perm
        options:
          perm: 'access content'
      cache:
        type: tag
        options: {  }
      empty: {  }
      sorts:
        weight:
          id: weight
          table: menu_link_content_data
          field: weight
          relationship: entity_id
          group_type: group
          admin_label: ''
          entity_type: menu_link_content
          entity_field: weight
          plugin_id: standard
          order: ASC
          expose:
            label: ''
            field_identifier: ''
          exposed: false
        weight_1:
          id: weight_1
          table: menu_link_content_data
          field: weight
          relationship: parent_id
          group_type: group
          admin_label: ''
          entity_type: menu_link_content
          entity_field: weight
          plugin_id: standard
          order: ASC
          expose:
            label: ''
            field_identifier: ''
          exposed: false
      arguments:
        nid:
          id: nid
          table: node_field_data
          field: nid
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: node
          entity_field: nid
          plugin_id: node_nid
          default_action: 'not found'
          exception:
            value: all
            title_enable: false
            title: All
          title_enable: false
          title: ''
          default_argument_type: fixed
          default_argument_options:
            argument: ''
          default_argument_skip_url: false
          summary_options:
            base_path: ''
            count: true
            override: false
            items_per_page: 25
          summary:
            sort_order: asc
            number_of_records: 0
            format: default_summary
          specify_validation: false
          validate:
            type: none
            fail: 'not found'
          validate_options: {  }
          break_phrase: true
          not: false
      filters:
        status:
          id: status
          table: node_field_data
          field: status
          entity_type: node
          entity_field: status
          plugin_id: boolean
          value: '1'
          group: 1
          expose:
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
        type:
          id: type
          table: node_field_data
          field: type
          entity_type: node
          entity_field: type
          plugin_id: bundle
          value:
            page: page
          group: 1
          expose:
            operator_limit_selection: false
            operator_list: {  }
        url_alias_filter:
          id: url_alias_filter
          table: node_field_data
          field: url_alias_filter
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: node
          plugin_id: url_alias_filter
          operator: starts
          value: /resources/
          group: 1
          exposed: false
          expose:
            operator_id: ''
            label: ''
            description: ''
            use_operator: false
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
            identifier: ''
            required: false
            remember: false
            multiple: false
            remember_roles:
              authenticated: authenticated
            placeholder: ''
          is_grouped: false
          group_info:
            label: ''
            description: ''
            identifier: ''
            optional: true
            widget: select
            multiple: false
            remember: false
            default_group: All
            default_group_multiple: {  }
            group_items: {  }
        field_comparison:
          id: field_comparison
          table: views
          field: field_comparison
          relationship: none
          group_type: group
          admin_label: ''
          plugin_id: field_comparison
          operator: '!='
          value: ''
          group: 1
          exposed: false
          expose:
            operator_id: ''
            label: ''
            description: ''
            use_operator: false
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
            identifier: ''
            required: false
            remember: false
            multiple: false
            remember_roles:
              authenticated: authenticated
          is_grouped: false
          group_info:
            label: ''
            description: ''
            identifier: ''
            optional: true
            widget: select
            multiple: false
            remember: false
            default_group: All
            default_group_multiple: {  }
            group_items: {  }
          left_field: nid
          right_field: nid_1
      filter_groups:
        operator: AND
        groups:
          1: AND
      style:
        type: default
        options:
          grouping: {  }
          row_class: ''
          default_row_class: true
      row:
        type: fields
        options:
          default_field_elements: true
          inline: {  }
          separator: ''
          hide_empty: false
      query:
        type: views_query
        options:
          query_comment: ''
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_tags: {  }
      relationships:
        entity_id:
          id: entity_id
          table: menu_entity_index
          field: entity_id
          relationship: none
          group_type: group
          admin_label: 'Menu link'
          plugin_id: standard
          required: true
        parent_id:
          id: parent_id
          table: menu_entity_index
          field: parent_id
          relationship: none
          group_type: group
          admin_label: 'Parent link'
          plugin_id: standard
          required: true
        node_id:
          id: node_id
          table: menu_entity_index
          field: node_id
          relationship: parent_id
          group_type: group
          admin_label: Parent
          plugin_id: standard
          required: true
      header: {  }
      footer: {  }
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - 'user.node_grants:view'
        - user.permissions
      tags: {  }
  …
  entity_reference_1:
    id: entity_reference_1
    display_title: 'All (ER)'
    display_plugin: entity_reference
    position: 1
    display_options:
      pager:
        type: none
        options:
          offset: 0
      arguments: {  }
      style:
        type: tree_entity_reference_selection
        options:
          row_class: ''
          default_row_class: true
          type: ul
          main_field: nid
          parent_field: nid_1
          collapsible_tree: '0'
          search_fields:
            title: title
            nid: 0
            nid_1: 0
      row:
        type: entity_reference
        options:
          default_field_elements: true
          inline: {  }
          separator: ''
          hide_empty: false
      defaults:
        fields: true
        arguments: false
      display_description: ''
      display_extenders:
        views_merge_rows:
          merge_rows: false
          field_config: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - 'user.node_grants:view'
        - user.permissions
      tags: {  }
🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇬🇧United Kingdom RobbM

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @RobbM
  • Status changed to Postponed: needs info 9 months ago
  • 🇬🇪Georgia maximkashuba Batumi

    robbm,

    Can't reproduce the bug with Menu Entity Index module (sorting with the module views relations to menu items).

    The results for the entity entityreference filter in a view are the same as for origin/source entity reference view and sorted in the same order.

  • 🇬🇧United Kingdom RobbM

    Thanks for investigating. I thought I'd found that it was any relationship, but perhaps it was the inclusion of views_tree…

    I'll try to put together the simplest set of steps to reproduce the issue repeatedly.

  • Status changed to Active 9 months ago
  • 🇬🇧United Kingdom RobbM

    Steps to reproduce:

    1. mkdir EntityReferenceFilterTestProject
    2. composer create-project "drupal/recommended-project:~9" EntityReferenceFilterTestProject/repo
    3. cd EntityReferenceFilterTestProject/repo
    4. sed -i 's/"stable"/"dev"/' composer.json # Or for macOS: sed -i '' 's/"stable"/"dev"/' composer.json
    5. composer require drupal/entityreference_filter drupal/menu_entity_index drupal/views_tree
    6. Install Drupal…
    7. Enable modules: Menu Entity Index, Views Reference Filter, Views Tree.
    8. Create Basic pages with titles as follows – adding each to the Main navigation menu:
      1. Cats (probably nid 1)
      2. Dogs (probably nid 2)
      3. Snails (probably nid 3)
      4. Sphinx (probably nid 4)
      5. Persian (probably nid 5)
      6. Moggy (probably nid 6)
      7. Husky (probably nid 7)
      8. Chow Chow (probably nid 8)
      9. Akita (probably nid 9)
      10. Mutt (probably nid 10)
    9. Arrange in Main navigation menu as follows:
      1. Home
      2. Cats
        1. Persian
        2. Sphinx
        3. Moggy
      3. Dogs
        1. Akita
        2. Chow Chow
        3. Husky
        4. Mutt
      4. Snails
    10. Configure Menu Entity Index to track Main navigation and Content.
    11. Create view of Content, e.g. "Classification":
      1. Change Pager to Display all items.
      2. Add Relationship to Menu link ID, with an Administrative title of "Menu link". Require this relationship.
      3. Add Relationship to Parent menu link ID, with an Administrative title of "Parent link". Do not require this relationship.
      4. Add Relationship to Content (Menu Entity Index), with an Administrative title of "Parent node". Use relationship "Parent link". Do not require this relationship.
      5. Add field for ID (Content) with no relationship. Move this to the top of the fields list.
      6. Add field for Weight (Custom menu link) with a relationship to Menu link.
      7. Add fields for ID (Content) and Title (Content) – each with a relationship to Parent node.
      8. Add field for Weight (Custom menu link) with a relationship to Parent link.
      9. Add Entity Reference display.
      10. Change Format to TreeHelper (Adjacency model), setting:
        1. Search fields to Content: Title.
        2. Main field to (first) Content: ID.
        3. Parent field to (second) Content: ID.
      11. Remove sort criteria.
      12. Add sort criteria:
        1.  Weight with a relationship to Menu link.
        2.  Weight with a relationship to Parent link.


        Note that the sort order in the preview now echoes the menu order.

      13. Exclude all fields apart from Content: Title.
    12. Add Reference, Content field to Basic pages, e.g. "Compatible":
      1. Limit to a single value.
      2. Select the view created above for the Reference method.
      3. Change the field from Autocomplete to Select list for the Form display.
    13. Create or edit a Basic page.

      Note that sort order in the drop-down for the field created above is not the same as previously seen in the preview, i.e. it no longer echoes the menu order. (Not exactly the original issue, but closely related.)

    14. Create view of Content, e.g. "Animals":
      1. Change Pager to Display all items.
      2. Add Page display with a path, e.g. "animals".
      3. Add entity reference filter for the field created above, e.g. "Content: Compatible (field_compatible) (entityreference filter)", and expose it.


      Note that sort order in the drop-down for the filter is not the same as previously seen in the preview, i.e. it no longer echoes the menu order.

  • 🇬🇧United Kingdom RobbM

    In hindsight, the drop-down might be respecting the sort criteria, just not the Views Tree "grouping". Will double check and come back…

Production build 0.69.0 2024