I have two entities which are in a parent/child relationship. Now I want to add a operations link in the parent's list view so that I can show the child's entity form with the reference to the parent pre-populated.

Ideally, the URL would look something like /admin/myparent/123/mychild/add, where 123 is the ID of the parent entity to which the new child should have a reference. I have the link in place in the operations menu, but I can't figure out how to have my ChildEntityForm load in the parent entity based on the URL parameter. How do I do this in Drupal 8?

I found a couple of questions regarding this here on SO, but they were all about Drupal 7. In addition, the documentation (Using parameters in routes) regarding upcasting url parameters states this:

Note that this method of providing the entity data to a form is not used for entity forms, such as forms to add/edit/delete entity data.

So is there a solution to this, preferably one that would not require installing a third-party module?

  • 1
    See the documentation on how to use parameters in routes in Drupal 8: drupal.org/node/2186285.
    – Wim Leers
    Commented Mar 17, 2016 at 15:25
  • Thanks, I've read that documentation over and over, but I still can't figure out how to get the parent entity into the child form =/ I think the issue is that I'm working with an "add entity form", and as I highlighted in my original question in that case instance parameters don't seem to be used. So I would need some workaround for that situation...
    – Jens Wegar
    Commented Mar 31, 2016 at 11:21

2 Answers 2


The prepopulate module is now able to handle this in the usual way like



This is how i work for done that :

my ajax form call on my parent entity form :

 $form['bouton'] = array(

          '#type' => 'link',

          '#title' => $this->t('Ajouter un produit'),

          '#url' => Url::fromUri($base_url.'/admin/boutique/commandesLignes/add',array('query'=>array('idCommande'=>$entity->get('id')->value))),

          '#attributes' => [

              'class' => ['use-ajax'],

              'data-dialog-type' => 'modal',

              'data-dialog-options' => Json::encode([

                  'width' => 700,




Then in my commandesLignesAdd form (modal open with the link into parent entity), i get the parameter like this :

  public function buildForm(array $form, FormStateInterface $form_state) {
    /* @var $entity \Drupal\boutique\Entity\CommandesLignesEntity */
    $form = parent::buildForm($form, $form_state);
      $entity = $this->entity;



    return $form;

I set a temporary value into my $form_state and i use it into the submit call :

public function submitForm(array &$form, FormStateInterface $form_state)

    parent::submitForm($form, $form_state);

My field reference name is commandes_id, and you need this field on your modal form, i use css for hidden it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.