Enable prepopulating the reference field with select display form widget - currently only works with autocomplete widget.

Issue information

From user: vermario

Related to !20 (merged)

Contribution record

Fork management

Problem/Motivation

When visiting the "children" form of a node that can be assigned children, a button is provided that can be used to add a new child page. The button is associated with a specially crafted URL which is supposed to preset the entity reference field to the currently viewed page.

The link follows this schema:

`node/add/[content_type]?field_name=id`

<?php
public function getAddChildUrl(EntityTypeInterface $entityType, ContentEntityInterface $parent, $bundle, $fieldName) {
    return
Url::fromRoute('node.add', [
     
'node_type' => $bundle,
    ], [
     
'query' => [
       
$fieldName => $parent->id(),
      ],
    ]);
  }
?>

In drupal9, clicking on this link will _not_ set the default value for the field, leaving the user to look into the field for the parent that they want to associate with the new child entity that they are creating.

Steps to reproduce

Set up entity reference hierarchy, visit the "children" page, click the button to add a new child page.

Proposed resolution

One possible way would be to add the Prepopulate module as a dependency: https://www.drupal.org/project/prepopulate and then modify the url query to:

"edit[$fieldName][widget][0][target_id]" => $parent->id(), 

At least in my local setup this makes it work as intended, but do let me know if I am missing something obvious :)

Edited by drupalbot
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information