For the node type article I want to display a block view of map markers generated from a content type called sted (places). For the content type article I have made a entity reference field (multiple values allowed) called kartreferanse (map reference) where you can pick multiple nodes of the type sted (places). So far so good. I have also made a view in a block that displays markers for all the content types sted (places) using a Geolocation - CommonMap. Now I want to pass the reference field to the view to show only the places picked in the field kartreferanse (map reference) for the current node. I have tried using contextual filters (field_kartreferanse: Content) Content: ID with no luck, and/or tried building a relationship using field_kartreferanse: Content in the view.

The question

How can i pass the ID of the current node (article) to the view (in a block) and use the entity reference field in the article to show right node IDs in the map block view? Do I also have to make some filter-criteria for this to work? (field_kartreferanse: Content) Innhold: ID (= [Content: Nid]) (Later I will have to make this view work in a multilingual site.)

enter image description here

(Drupal 8, local install / mamp, geo locationfield, views 8.1.10)


2 Answers 2


The easiest way to use a view in a block (or anything that uses a build array) is as a render element:

$node = \Drupal::routeMatch()->getParameter('node');

$current_nid = $node->id();

$view_arguments = [$current_nid];

 $build['news'] = [
   '#type' => 'view',
   '#name' => 'myviewname',
   '#display_id' => 'block_1',
   '#arguments' => $view_arguments,
   '#weight' => 30,

return $build;

The first argument passed in will be used as your content ID.

If you want to do it all through the UI, you set your contextual filter to pull from the content id.

set views contextual filter to pull in content id

then if you place a block based on the view, it will pull in from the current node.

placing block version of view through CMS

  • Thanks. I am used to D7 and pretty new to the Drupal 8 structure. Is this going in some article, view or block template (twig)? Or do I have to make a render element plugin? Does this mean that there is no "views UI-way" of doing it?
    – eye-wonder
    Commented Oct 4, 2016 at 7:27
  • Yes, you can set it up so that it pulls from the current node, and then place the block through the CMS, see updated answer.
    – oknate
    Commented Oct 4, 2016 at 14:40
  • Yes. That is what I was initially trying to do. I tried the "Content ID from URL" and view in a block, but it did not give any results. It might be a combination of settings (Contextual filters, Relationships, Filter Criteria) that is not right. I think I will try to update D8 Core. Views is showing weird behavior some times.
    – eye-wonder
    Commented Oct 24, 2016 at 8:57
  • "contextual filter: Display all results for the specified field" gives a result (all places). As soon as I choose "Provide default value" "content ID from URL" or even a fixed value, I get no results. If I tick of "Require this relationship" under Relationships, I also get no results.
    – eye-wonder
    Commented Oct 24, 2016 at 9:10
  • I accepted your answer, even tho I have not tried your reder element solution. The UI-way was a bit tricky so I made a step-by-step guide in a separate answer ( drupal.stackexchange.com/questions/216688/… ). Thanks
    – eye-wonder
    Commented Oct 26, 2016 at 11:57

Finally i made it.

First you make a content type for the items (in this case sted / place). Then you make or edit the content type where the relevant items is going to be displayed (in this case article). For the articles you add a reference field. The Type of item to reference is content and the content type to reference is your items content type. (I chose unlimited values for the field).

Create a view that shows content of the type your items in a block and choose the styling you want (e.g. grid, unformatted list, custom style...). The preview of the view now show all your items. Edit one of the articles so that it reference one/some of the items (not all). Add the view block to be displayed with the articles.

Edit the view to show only the relevant items:

(advanced) Relationships add: Content using field_your_refernce_field - (Relate each Content with a field_your_refernce_field set to the content.).

(advanced) Contextual filters add: CONTENT ID. Edit the Contextual filter (CONTENT ID) When the filter value is NOT available Provide default value Content ID from URL and (above) "Relationship:" choose field_your_refernce_field

(For the view preview add the ID of your test-article into the Preview with contextual filters: to see the result.)

enter image description here

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.