Score:0

Override/remove title of view with contextual filter as a block

br flag

I have a block in a page that is a view with a contextual filter. As a block does not have a path, I have to provide a default value "raw value from URL" as per Contextual filter with a block view

This all works as it should, the view in the block gets filtered per the URL's last/third component as configured in the contextual filter's settings.

I use this block both in an overview mode where it shows all the entities, grouped by a field (when the third path component is missing) and in a more detailed mode where I just want to show the entities without the grouping's title. As the contextual filter already reduces the entity set to only one group, this group's title is redundant and ugly.

However, when using a "default value" the contextual filter's config does not let me override the title.

What I came up with - which seems like too much of a hack - is the following in views-view-grid--{view name}--block.html.twig

{% set parts = url("<current>")|render|render|split('/') %}
{% if parts[5] is empty %}
    {# not a contextually filtered block, so show a title #}
    {% if title %}
      <h2>{{ title }}</h2>
    {% endif %}
{% endif %}

I am just counting path components, which seems a little too vague to me.

Is there any better solution using just the view's (contextual filter) config? Any module to extend the view config options?

Or, alternatively, how can I determine if a view received a contextual filter in twig? I am generally having a hard time finding the available "objects" and their "methods"/parameters in twig as I could not find a good, complete reference. Why do I need to |render|render for example? What parameters does the url() function take? Can I count characters in a string with twig more easily than I did above? Pointers appreciated.

mx flag
You should be able to override the Title when providing a default value (through the interface), but it's under the lower section, the one for when the argument IS there.
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.