I created an entity reference field for a node filtered by a view type "entity reference", but I need to pass an argument in the contextual filter of this view, to filter entities on my field.

I tried passing my argument on the URL in a query parameter (e.g. node/add/page_interieur?dossier=2), but I never get results in my field autocomplete.

Is it possible to do that with this method or another one?

Settings for the contextual filter with the Query parameter option


Settings for the Entity reference field


2 Answers 2


To programmatically pass argument to my field entity reference with link node/add/page_interieur?dossier=512 :

use Drupal\node\Entity\Node;
use Drupal\Core\Form\FormStateInterface;

function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id){

  if ($form_id == 'node_page_interieur_form') {
    $nid  = \Drupal::request()->query->get('dossier');

    if (is_numeric($nid) && isset($form['field_dossier_reference'])) {
      //autocomplete select field reference
      $form['field_dossier_reference']['widget']['#default_value'] = $nid;
      //disable field
      $form['field_dossier_reference']['#disabled'] = TRUE;
      //pass my ID like argument in my field to filter only pages referenced
      $form['field_liste_ref_dossier']['widget']['target_id']['#selection_settings']['view']['arguments'][] = $nid;

    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'science_po_page_interieur_form_submit';

My contextual filter configuration in views :enter image description here

  • Not working any more. Tested with Drupal 8.9.
    – Elin Y.
    Sep 29, 2021 at 13:18

For example.com/path?id=55


For example.com/path/55


For example.com/path/alias


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.