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

Created on 11 October 2021, over 3 years ago
Updated 30 March 2023, over 2 years ago

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`

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 :)

Feature request
Status

Fixed

Version

3.0

Component

Code (module)

Created by

🇫🇮Finland vermario

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.

Production build 0.71.5 2024