viewdrupaldrupal-8drupal-views

Using a Content ID Contextual Filter in a Views Block in Drupal 8


I am attempting to create a View that, instead of showing a list of all nodes of a content type, will show only a single node of a content type based on node ID. In Drupal 7, I worked almost exclusively in Views Content Panes and was able to achieve this based on NID and then setting the Argument Input to From Context: Content ID. How do I get similar results using Blocks in Drupal 8?

I have a view that is correctly configured to show all nodes of a content type. I've tried to add a Contextual Filter: ID; but I cannot figure out how to configure it to get a result that isn't All Results.

Thank you in advance!


Solution

  • When you edit the contextual filter Content ID, you have :

    WHEN THE FILTER VALUE IS NOT AVAILABLE (base view is built without filter, this is the case)

    Check Provide default value to set how filter values should be retrieved, then you can choose a type, for example Content ID from URL, or Query parameter, etc.

    For example with Query parameter you can set the parameter name and the Fallback value. In your case you would set something like nid as the query param, and all or a fixed node ID as fallback value ('all' is the exception value by default that is to disable the filter).

    Given this example, you 'd just add the query ?nid=5 to the request path. It seems you need the block filtered by default though, in this case just set a fixed node ID (eg. 5 instead of all) as fallback value in views admin, then the block will be filtered by default the same way.