0

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..."?

1 Answer 1

0

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.

1
  • Thanks a lot. I will dig into your proposal
    – Themetis
    Commented Nov 16, 2022 at 9:59

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.