Drupal 8 - Use the current node ID (on node edit form) to filter an entity-reference view as a contextual filter I've got this view I just created, which lists all the events &amp;&amp; publications related to a program (field_program, an entity-reference field) on my website (Drupal 8). Call it "view_program_related_contents".

I've got this content type (program), with a entity-reference field (field_promoted_contents). About this field, I want to use a view, because I don't want the webmaster to be able to promote events|publications related to any program but the one he's editing, so I have to filter the results based on the field_program field.

I did set a contextual filter (field_program) on my view. When this filter is not available, I fill it whith a default value : "Content ID from URL".

I can't manage to get my field_promoted_contents field populated with the well-related contents. If I set a static value (node id 1) in the contextual filter, it works, I only have the contents related to this first program. But when I set this "Content ID from URL".. I get no results :(

Do I have to pass a token (such as [node:nid]) as an argument to the view in the entity-reference field configuration ? Is there a module for that ?
This might not be THE answer.
But for my specific need, and because we already are late on this project. 
I found a "quick-fix" solution.. waiting for a more drupalistic one.

Assuming that this entity-reference-1 view display is always called from a node/:nid/edit page, I should always have a HTTP_REFERER on each request to the view (I guess ^^). Well, I check that for insurance.

Inside a hook_views_pre_view hook, I pass the node-id from the HTTP_REFERER to the view, like this :

&lt;?php

use \Drupal\node\Entity\Node;

/**
 * Implements hook_views_pre_view()
 *
 * Alter the contextual filters for my view :
 *
 * When the HTTP_REFERER is a node/:nid/edit pattern and the view-display is an entity-reference
 *  then I fetch the node ID from the URL, I load the node to check the node type
 *  and I pass this nid to the view as an arg
 */
function MYMODULE_program_views_pre_view(ViewExecutable $view, $display_id, array &amp;$args) {
  if ($view-&gt;id() == 'view_program_related_contents' &amp;&amp; $display_id == 'entity_reference_1') {
    if (isset($_SERVER['HTTP_REFERER'])) {
      $referer_url = parse_url($_SERVER['HTTP_REFERER']);
      $path = htmlspecialchars($referer_url['path']);
      if (preg_match('/node\/(\d+)\/edit/', $path, $matches)) {
        $node = Node::load($matches[1]);
        if ($node instanceof Node) {
          if ($node-&gt;getType() == 'program') {
            $args[] = $node-&gt;id();
          }
        }
      }
    }
  }
}


Again, I think I'm missing something in my entity-reference field definition, or my view configuration.. but I don't have enough time to spend on this.

I hope this helps..