I want to make a table of content block where I link to the grouping field titles on a view. The problem is that the grouping field is only a h3 without any class or id. Is there a way to nest the h3 in a div with an unique ID so I can create an anchor link for it?
I think this may be a template modification task.
This is the current views-view-unformatted.html.twig file:
{% if title %}
<h3>{{ title }}</h3>
{% endif %}
{% for row in rows %}
{%
set row_classes = [
default_row_class ? 'views-row',
]
%}
<div{{ row.attributes.addClass(row_classes) }}>
{{ row.content }}
</div>
{% endfor %}
Switch:
<h3>{{ title }}</h3>
With:
<h3 id="{{ title | clean_class }}">{{ title }}</h3>
This would require your titles to all be unique to ensure unique ID's. There may be something better to use than "title" for the ID as well in this case.
Also, I would wonder if using an "attachment" in the view might give better options and control over a block for the table of contents...
-
Thanks! This helped me a lot. For people looking for naming conventions on custom views templates in your custom theme check out redcrackle.com/blog/drupal-8/theme-views-templates – Jessica IX May 15 '18 at 0:07