5

I created a Drupal 8 View that creates a block of content associated with a content type. The block has a context filter of "Content: Node ID". I can place the Node ID of different nodes in the Views Preview and receive different content for each Node ID. The Views Block seems to be working well in Views Preview.

I would like to place this Views Block in a region of Block layout. I need the block to obtain the Node ID of the current node so it will display unique content for each different node -- just like in Views Preview. I have not been able to figure out how to associate the current Node ID when running from Block layout. Some blocks have this ability -- for example "Page title" and "Main page content" that ship with Drupal 8. Is it possible to give me Views Block this ability?

I've thought of two alternatives to what I have written above:

  1. write a module that creates the block I need -- obtain the current Node ID and display the Views Block.

  2. integrate the Views Block into the twig file associated with the content type -- when the node is displayed, twig will call Views with the Node ID and return the needed content.

Any suggestions?

7

This feature is already present in form of the contextual filters in views (not limited to D8). To do this:

  1. Add a contextual filter in your view block of "Content: nid"
  2. In "when filter not present" select "provide default value".
  3. Select "Content id from the URL".

And this block will automatically take in the nid from the URL.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.