I want set a default value for my entity field reference refer to his parent.

I am on his parent and with a link i load a popup with the form of the child :

enter image description here

My parent is in the URL param value 2 for this example. I need to put this param into the last field on my popup "Commande référente".

The parent is :


The child is (popup) :


The code of CommandesLinesEntity :

$fields['commandes_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Commande réferente'))
    ->setDescription(t("Le numero de commande"))
    ->setSetting('target_type', 'commandes_entity')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'string',
        'weight' => 0,
    ->setDisplayOptions('form', array(
        'type' => 'entity_reference_autocomplete',
        'weight' => 5,
        'settings' => array(
            'match_operator' => 'CONTAINS',
            'size' => '60',
            'autocomplete_type' => 'tags',
            'placeholder' => 'Numero de commande',
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);


For list all children on the parent, i use a controller who return table and i call it into the add form parent :

class CommandesLignesController extends ControllerBase

    private $idCommande;

    public function __construct($idCommande)

    public function content()

        global $base_url;

        $entity = \Drupal::entityManager()->getStorage('commandes_lignes_entity');


        $header = array(
            t('Nom du produit'),
            t('Prix HT'),
            t('Prix TTC'),

        foreach ($commandesLignes as $key => $record) {
            $rows[] = array(


            $rows[]=array('Pas de produits');

        $content['bouton'] = array(

            '#type' => 'link',

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

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

            '#attributes' => [

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

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

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

                    'width' => 700,




        $content['commandes_list'] = array(

            '#type' => 'table',

            '#header' => $header,

            '#rows' => $rows


        return $content;

     * Est utiliser dans CommandesLignesEntity pour la valeur par defaut de la reference commande_id, la session est initialiser dans le construc
    public function getIdCommande()
        return $_SESSION['idCommande'];

This is how i call my children when my add-form load :

   * {@inheritdoc}
  public function buildForm(array $form, FormStateInterface $form_state) {
    /* @var $entity \Drupal\boutique\Entity\CommandesEntity */
    $form = parent::buildForm($form, $form_state);

    $entity = $this->entity;

    if($entity->get('id')->value) {
      $commandesLignes = new CommandesLignesController($entity->get('id')->value);
      $form['commandeLignes'] = $commandesLignes->content();

    return $form;

For now the only way i found for set the default value is to set a session variable when my add-Form parent load, and then when i click on the popup link that ll call my function callback for set the default value.

That work but i have one problem, if user open 2 navigator task and load another add-form, and return on the first task, session variable will be set on the wrong parent.

So is there another way for set a default entity reference value ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.