Setting active links (or breadcrumbs) within twig

By mherchel, 18 May, 2022

You can use Drupal twig’s path function to do logic to check what page you’re on (and set CSS clasess or breadcrumb text )

The logic will look something like this:

active-path.twig
            
            {% if path('<current>') == path('view.my_interests.page_1') %}
    {% set breadcrumb_text = 'For You' %}
  {% elseif path('<current>') == path('view.topics.page_1') %}
    {% set breadcrumb_text = 'Prospect Interests' %}
  {% elseif path('<current>') == path('view.news_by_prospect.page_1') %}
    {% set breadcrumb_text = 'People' %}
  {% else %}
    {% set breadcrumb_text = 'Back' %}
{% endif %}