2

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?

2
  • 1
    See the documentation on how to use parameters in routes in Drupal 8: drupal.org/node/2186285.
    – Wim Leers
    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
    Mar 31, 2016 at 11:21

2 Answers 2

1

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

http://www.example.com/node/add/child?edit[field_parent_ref]=17

0

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;

      $idCommande=\Drupal::request()->query->get('idCommande');

      $form_state->setTemporary(array('idCommandes'=>$idCommande));



    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)
{
    $commandes_id=(int)$form_state->getTemporaryValue('idCommandes');
    $form_state->setValue(array('commandes_id'),array('value'=>$commandes_id));

    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 that you have read and understand our privacy policy and code of conduct.

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