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 :)
Issue fork entity_hierarchy-3242501
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
Comment #2
larowlanThere's code in
entity_hierarchy_form_alterthat is supposed to handle this.And a test in
\Drupal\Tests\entity_hierarchy\Functional\ReorderChildrenFunctionalTest::testAddChildLinksSo perhaps there's something different in your setup.
Comment #3
dat deaf drupaler commentedPopulating 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)
Comment #4
larowlanAh! so its a matter of the widget - thanks @dat deaf drupaler!
Comment #5
dat deaf drupaler commentedYup, that is correct @larowlan.
Marking this as a feature request. See comment #3 for a workaround.
Comment #8
sunlixHey,
I have normalized the widget array shape for the
taget_idkey.Additionally I have added a condition on the widget type, becaue the
selectwidget needs only the entity ID as a field value.The
entity_autocompletewidget needs the full entity object for the#default_value.Adjusted the tests to cover that change, too.
Comment #9
sunlixComment #10
larowlanThanks @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.
Comment #11
sunlixSo 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 :)
Comment #12
larowlanNo need to apologize! thanks for your contribution.
I'll create a new release shortly
Comment #14
larowlanhttps://www.drupal.org/project/entity_hierarchy/releases/3.3.5