

Then create a template file for our articlelist vew as below in themes/YOURTHE/templates/views folder. So, go to file from classy theme and copy the content.

Here we are creating view templates for views. Here we are going to discuss about how to rewrite results of a views using view templates. I hope this has helped you to clarify the concept a little more.As you know, views are used for listing content by applying various filters with out writing code.Įvery developer has to modify these results in order to fit to their customized themes. This tutorial only scratches the surface of what is possible with contextual filters. If you go to one of your articles, you will see a block displaying all other related publications, but not the one you are reading. As a matter of fact, it is the exact opposite of what we want to achieve.īy clicking exclude, you are basically telling Drupal to display all results of the view, except the one related to the currently displayed article. The view will display just the element related to that particular article. One thing to notice is that Drupal is smart enough to internally figure out the node id, even if you have enabled Pathauto, or have changed the URL manually in the node edit form. The system will take the value of the Article (in this case) ID from the URL and according to that value, it will filter and present the right view. This is where you can test how the contextual filter works. Click When the filter value is NOT available.Blocks do not have URLs to reference, so you have to add this “connection” manually. Click Add and configure contextual filters.Select the Entity (Content) ID as a contextual filter.Display only the filters related to Content.Click ADVANCED to display the right-hand column of Views UI.That is not user-friendly and looks less professional. However, the currently displayed article will also appear within the view results. You will see the block displaying the view the way you have just configured it. Restrict the block only to the Article content type.

Search for the name of your view in the search box.NOTICE: I have also removed the 2 blocks on the Sidebar first region. Scroll down to the Sidebar second region.Click Grid Settings in the FORMAT section.In the FIELDS section click the dropdown arrow and select Rearrange.More on Image styles on a future tutorial. Notice that I created a squared image style (Configuration > Image styles), to display it in the view. Search for the Image field and select it.The goal of this exercise is to create a block with a view displaying articles, related to the currently viewed post. Keep reading if you want to grasp the idea behind contextual filters in Drupal 8!įor the purpose of this tutorial, I have created 11 articles with an image and body text. This whole concept implies certain complexity, so the best way to clarify it is with an example. If the provided argument value is in the URL, the view will contain certain results, if the value is not in the URL, the view will be filtered with other results. The filter acts dynamically based on the URL. Contextual filters in Drupal allow you to add or remove information (content) to the currently displayed view.
