Use Twig to get a url alias in Views

I have a view that lists required courses on a degree detail page for a school.  These courses link back to a listing page, with an anchor to jump to the right place on the page.

I'm using a field based view so I could do some basic manipulations of the output using replacement patterns.  

I need to get the url alias of the listing page, so my first thought was to use the "Link to Taxonomy term" field.  However, I wasn't able to change the link text to be anything but static (I needed it to be the name of the course.  I created a ticket to add the replace patterns functionality here:  https://www.drupal.org/node/2838020

I found a Stack Overflow comment about using twig to convert a string into a url alias: http://drupal.stackexchange.com/a/217415/888

I can create a string as taxonomy_term/TID no problem in a custom text field using replace patterns.  Then use the url() twig function to convert to an alias.

Here's what I ended up with:

<a href="{{ url('entity.taxonomy_term.canonical', {'taxonomy_term':  tid}) }}#{{ nid }}">{{ title }}</a>

 

See more info about that url() function here:  

https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates

Tags
Code Twig