Problem/Motivation

We have a multi value Dynamic Entity Reference field that can reference both nodes and group entities. However, we want the second value to always be a group entity reference. However by default the type is set to "content", ie nodes.

We've been able to successfully change the type of the second value to be a group via a form alter (see code below). However, we lose any filtering of the values as set in the field settings (limiting groups using a views entity reference view -- see screenshot). We also tried simply changing the "type" select with Javascript if that would trigger the necessary changes but with no success.

FYI Manually changing to content and back to group manually makes the views entity reference filter work as expected.

Just wondering what I'm missing to make this work?


<?php
function custom_module_form_node_game_form_alter(&$form, $form_state, $form_id) {
  $form['field_teams']['widget'][1]['target_type']['#default_value'] = 'group';
  $form['field_teams']['widget'][1]['target_id']['#target_type'] = 'group';
  $form['field_teams']['widget'][1]['target_id']['#selection_handler'] = 'default:group';
  unset($form['field_teams']['widget'][1]['target_id']['#selection_settings']);
}
?>

Apologies if this has been answered before. I did search the issue queue with no success. Tried on version 1.x and 2.x. Can't test 3.x or higher since my site isn't quite PHP8 ready.

Comments

rhache created an issue. See original summary.

rhache’s picture

Issue summary: View changes
rhache’s picture

Issue summary: View changes
jibran’s picture

Status: Active » Postponed (maintainer needs more info)

Can you please upload the views YAML if possible? Can I have the autocomplete URLs for the DER fields when you change them using form alter? Maybe have a look at the network tab and see which endpoint it is hitting before and after changing it manually.

rhache’s picture

Here's the autocomplete input field when type changed via form alter:

<input data-drupal-selector="edit-field-teams-1-target-id" class="form-autocomplete form-text ui-autocomplete-input" data-autocomplete-path="/entity_reference_autocomplete/group/default%3Agroup/3XgIhuGSIFRmxKRPej-bzZgoB6ocMTWGBScAJlMFUgs" type="text" id="edit-field-teams-1-target-id" name="field_teams[1][target_id]" value="" size="40" maxlength="1024" placeholder="" data-once="autocomplete" autocomplete="off">

And when type is change manually:

<input data-drupal-selector="edit-field-teams-1-target-id" class="form-autocomplete form-text ui-autocomplete-input" data-autocomplete-path="/entity_reference_autocomplete/group/views/dJeG_gd2LuJaIM_4fhGfVBJdnbuYyeCpIZB0M84NLe4" type="text" id="edit-field-teams-1-target-id" name="field_teams[1][target_id]" value="" size="40" maxlength="1024" placeholder="" data-once="autocomplete" autocomplete="off">

I can see how one points to /entity_reference_autocomplete/group/default...(incorrect) /entity_reference_autocomplete/group/views/... (correct).

This is the views entity reference file:

uuid: 46a34778-07f0-4b0d-a75f-829bdac13906
langcode: en
status: true
dependencies:
  config:
    - group.type.female_team
    - group.type.team
  module:
    - group
    - taxonomy
id: active_teams
label: 'Active Teams'
module: views
description: 'View for teams in current active year'
tag: admin
base_table: groups_field_data
base_field: id
display:
  default:
    id: default
    display_title: Master
    display_plugin: default
    position: 0
    display_options:
      fields:
        label:
          id: label
          table: groups_field_data
          field: label
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: null
          entity_field: label
          plugin_id: 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
          click_sort_column: value
          type: string
          settings:
            link_to_entity: false
          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
      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: none
        options: {  }
      cache:
        type: tag
        options: {  }
      empty: {  }
      sorts:
        weight:
          id: weight
          table: taxonomy_term_field_data
          field: weight
          relationship: field_level
          group_type: group
          admin_label: ''
          entity_type: taxonomy_term
          entity_field: weight
          plugin_id: standard
          order: ASC
          expose:
            label: ''
            field_identifier: weight
          exposed: false
        field_team_comp_value:
          id: field_team_comp_value
          table: group__field_team_comp
          field: field_team_comp_value
          relationship: none
          group_type: group
          admin_label: ''
          plugin_id: standard
          order: DESC
          expose:
            label: ''
            field_identifier: field_team_comp_value
          exposed: false
        label:
          id: label
          table: groups_field_data
          field: label
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: group
          entity_field: label
          plugin_id: standard
          order: ASC
          expose:
            label: ''
            field_identifier: label
          exposed: false
      arguments: {  }
      filters:
        type:
          id: type
          table: groups_field_data
          field: type
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: group
          entity_field: type
          plugin_id: bundle
          operator: in
          value:
            female_team: female_team
            team: team
          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
            reduce: false
          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_hockey_year_active_value:
          id: field_hockey_year_active_value
          table: taxonomy_term__field_hockey_year_active
          field: field_hockey_year_active_value
          relationship: field_year
          group_type: group
          admin_label: ''
          plugin_id: boolean
          operator: '='
          value: '1'
          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: {  }
        field_team_hide_value:
          id: field_team_hide_value
          table: group__field_team_hide
          field: field_team_hide_value
          relationship: none
          group_type: group
          admin_label: ''
          plugin_id: boolean
          operator: '='
          value: '0'
          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: {  }
      style:
        type: default
        options:
          grouping: {  }
          row_class: ''
          default_row_class: true
          uses_fields: false
      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:
        field_year:
          id: field_year
          table: group__field_year
          field: field_year
          relationship: none
          group_type: group
          admin_label: Year
          plugin_id: standard
          required: true
        field_level:
          id: field_level
          table: group__field_level
          field: field_level
          relationship: none
          group_type: group
          admin_label: Level
          plugin_id: standard
          required: false
      header: {  }
      footer: {  }
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
      tags: {  }
  entity_reference_1:
    id: entity_reference_1
    display_title: All
    display_plugin: entity_reference
    position: 1
    display_options:
      pager:
        type: none
        options:
          offset: 0
      style:
        type: entity_reference
        options:
          search_fields:
            label: label
      display_description: ''
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
      tags: {  }