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

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

vermario created an issue. See original summary.

larowlan’s picture

There's code in entity_hierarchy_form_alter that is supposed to handle this.

And a test in \Drupal\Tests\entity_hierarchy\Functional\ReorderChildrenFunctionalTest::testAddChildLinks

So perhaps there's something different in your setup.

dat deaf drupaler’s picture

Populating the referenced field from children page works fine for me on a clean D9 environment when the 'field_name' form display widget is set to 'Autocomplete'.

Else node/add/<content_type>?field_name=<nid> schema does not work with 'Select' widget as form display for this 'field_name' field, unfortunately.

However, with the Prepopulate module enabled, you can populate a 'Select' form widget field using the schema:
/node/add/<content_type>?edit[field_name][widget][0][target_id]=<nid>
to prepopulate the very same field ;)

Edit: updated for the sake of field name consistencies (y)

larowlan’s picture

Issue tags: +Needs tests

Ah! so its a matter of the widget - thanks @dat deaf drupaler!

dat deaf drupaler’s picture

Title: Reference field is not populated when clicking on the add new entity link in the children form page » Enable prepopulating the reference field with select display form widget - currently only works with autocomplete widget.
Category: Bug report » Feature request
Issue tags: -Needs tests

Yup, that is correct @larowlan.

Marking this as a feature request. See comment #3 for a workaround.

sunlix made their first commit to this issue’s fork.

sunlix’s picture

Hey,

I have normalized the widget array shape for the taget_id key.
Additionally I have added a condition on the widget type, becaue the select widget needs only the entity ID as a field value.
The entity_autocomplete widget needs the full entity object for the #default_value.

Adjusted the tests to cover that change, too.

sunlix’s picture

Status: Active » Needs review
larowlan’s picture

Status: Needs review » Needs work

Thanks @sunlix - looks good, I would like to keep the (broken) nested target_id if possible, just to minimize the chance of breaking people's existing form alters.

sunlix’s picture

Status: Needs work » Needs review

So that should work. Test are running again. Touched fewer code.
Hopefully you agree now :) I am sorry I was tired and confused by some test failures.
At some point there was no really brain power involved :D good night :)

larowlan’s picture

Status: Needs review » Fixed

Hopefully you agree now :) I am sorry I was tired and confused by some test failures.

No need to apologize! thanks for your contribution.

I'll create a new release shortly

  • larowlan committed f44546e1 on 3.x authored by sunlix
    Issue #3242501 by sunlix, larowlan, dat deaf drupaler, vermario: Enable...
larowlan’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.