I've got this view I just created, which lists all the events && 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 ?

  • When you manually type in the ID on the View edit preview screen, does it work? Test on the Views edit screen first and make sure it works there before you try to troubleshoot the entity reference. Commented Oct 10, 2017 at 15:55
  • Absolutely, it works in the view UI. When I type "1" in the contextual-filter field, it filters my contents with the field_program #1.
    – Julien
    Commented Oct 10, 2017 at 16:04
  • PS: The XHR looks like this when I type in the field-reference field : http://www.my-project.loc/en/entity_reference_autocomplete/node/views/OMRoUtYGplS-duIcL-ZiN1HhmPx8SM3WrLogk-LkilA?q=test .. So I suppose that the view is not aware of the current node ID. I'm missing something in the field config I think, am I ?
    – Julien
    Commented Oct 10, 2017 at 16:10

2 Answers 2


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 :


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 &$args) {
  if ($view->id() == 'view_program_related_contents' && $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->getType() == 'program') {
            $args[] = $node->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..


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?

No, but this will work in core with a patch from Dynamic Views Arguments via Tokens.

If you'd like the depending field to update automatically whenever the dependent field changes, see Add AJAX support to fields using Dynamic Views Arguments via Tokens.

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.