Score:0

How to differentiate the state when a contextual filter is applied?

lu flag

Hello to all the Drupal community. I've recently faced a challenge to visually modify some parts of my view's template for the state when its contextual filter is applied. Some details are as follows:

  • I have a View of the articles previews list. Each article has several tag names (taxonomy terms) applied.
  • This view has a Contextual filter set up to filter the list based on the taxonomy Term (tag name). The Term is provided by adding term_id to URI (like mysite/blog/12). If no Term is provided - it just prints all articles.
  • I have a separate views block with "Featured article" content, which is placed above my Articles list view and is limited to being visible on "mysite/blog" URI. This was made to avoid it from displaying on full articles pages by addressing "mysite/blog/article-name..."

Questions:

  • How can I add some class names to the views TWIG template to indicate that this view has contextual filters applied from URI?
  • How can I show the Featured article block on the "mysite/blog" and "mysite/blog/TID" pages but not for "mysite/blog/article-name..."?
Score:0
ru flag

How can I add some class names to the views TWIG template to indicate that this view has contextual filters applied from URI?

The easiest way to do this is inside the views-view-DISPLAY_TYPE.html.twig templates (e.g. views-view-table or views-view-unformatted, whatever display type you are using,...)

In that template you have access to the view variable, which contains the entire views object of the current page. The contextual filter values are stored in the args variable of that object, so you need something like
{% if view.args %}...your logic here...

How can I show the Featured article block on the "mysite/blog" and "mysite/blog/TID" pages but not for "mysite/blog/article-name..."?

It seems you have placed that block in regions on the global block layout, while you want to show it only on your views page. So the easist way is to add the block in the view page, and not in one of the theme regions. You can place any block, even other views, in the header of a views page in the Views UI.

Themetis avatar
lu flag
Thanks a lot. I will dig into your proposal
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.