How to insert variable into href path?

Created on 27 September 2022, about 2 years ago
Updated 10 February 2023, over 1 year ago

I'm using a pattern 'card', which displays cards in a block on the home page. The pattern is based on a ds View Mode of the Content Type. Each card should be contained in an anchor tag so the whole card can be clicked. Destinations of the links should be the default View Mode of each node. Each node's title is used as a url alias, so node/2 has an alias of /honey. The title field is available in my pattern yml.

I'm struggling to write the anchor tag in my twig template.
I first tried the intuitive solution
<a href="/{{title}}">
which produces strange results.

I started googling and realised I've stumbled into a collision of conventions. I've been reading about twig url/path, drupal routes and ui_patterns. It seems like all 3 are coming to the party.

For example
<a href="{{ url('<front>') }}">
works, but putting the variable {{title}} in there breaks it, whatever I try.

I'm a site builder rather than a developer. Can anyone show me how to code the href in my pattern-card.html.twig?

💬 Support request
Status

Fixed

Version

1.3

Component

Documentation

Created by

🇬🇧United Kingdom nigelwhite Marsden

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇷France duaelfr Montpellier, France

    The questions has been answered. Do not hesitate to reopen if needed.

  • 🇫🇷France Grimreaper France 🇫🇷

    Updating issue credits.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed over 1 year ago
  • 🇬🇧United Kingdom nigelwhite Marsden

    Thank you for these ideas. [Coming back to this after a while, so sorry for the delay]

    Two of your ideas worked for me. To summarize for any other site builders who are puzzled -

    • fields you want to format with your pattern must be plain text or link. Even then Drupal will add a load of html markup around your field, which will throw your pattern's twig
    • remove markup from all fields in your display as per #11. At the bottom of /admin/structure/types/manage/your-content-type/display you will find a Pattern Settings tab. Set Field Templates to 'Only content'.
    • remove markup from fields individually by installing nomarkup module as per #12. Each field's settings cog has a 'Remove field markup' option

    For links -

    • in yml your link field is type 'uri'
    • in pattern preview this worked for me: preview: "<a href='https://example.com/'>Link to source</a>"
    • in the twig a simple {{ your-field }} is all you need
    • when adding content in the node add form the url and the link text are what you'd expect
Production build 0.71.5 2024