Use 'Reference method' approach to allow 'Entity reference' view for selection

Created on 30 March 2022, about 3 years ago
Updated 5 September 2023, over 1 year ago

Problem/Motivation

The user gets a overwhelming amount of results for nodes and can't identify which node they want (eg similar titled content in the same content type).

If cores' ability (from an entity reference field) to provide the 'Reference method' and allowing a View (type: entity reference) could be implemented , the view can expose other useful information (instead of just the entities' Title), and lets the user be comfortable they've chosen the right piece of content!

Eg: We often make custom entity reference views when we have thousands of nodes, and concatenate other metadata to the output results in the autocomplete list. Example is '[title] | [tags] | [MMM-YY published]' so 'Examples of evaluation | Approach | Feb-21'

Proposed resolution

Add the option to use an Entity reference view and choose the appropriate view when configuring a entity embed button

User interface changes

See above proposal and images

Feature request
Status

Active

Version

1.0

Component

User interface

Created by

🇳🇿New Zealand edmonkey

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom james.williams

    I agree, this would be nice. For anyone looking to achieve it via custom code though, pop something like this in a custom module:

    
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\embed\EmbedButtonInterface;
    
    /**
     * Implements hook_form_FORM_ID_alter().
     */
    function MYMODULE_form_entity_embed_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
      $embed_button = $form_state->get('embed_button');
      if ($embed_button && $embed_button instanceof EmbedButtonInterface && $embed_button->id() === 'MY_BUTTON') {
        if ($form_state->get('step') === 'select') {
          $form['entity_id']['#selection_handler'] = 'views';
          $form['entity_id']['#selection_settings'] = [
            'view' => [
              'view_name' => 'MY_VIEW_NAME',
              'display_name' => 'entity_reference_1',
              'arguments' => [],
            ],
          ];
        }
      }
    }
    

    (Replace the various bits in capital letters.)

Production build 0.71.5 2024