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?
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | Capture d’écran du 2022-10-08 19-45-24.png | 28 KB | duaelfr |
| #11 | Capture d’écran du 2022-10-08 19-37-30.png | 19.25 KB | duaelfr |
| #9 | honey2.png | 41.84 KB | nigelwhite |
| #9 | honey1.png | 46.09 KB | nigelwhite |
Comments
Comment #2
nigelwhiteComment #3
nigelwhiteComment #4
nigelwhiteComment #5
pdureau commentedhello @nigelwhite
It is great to have you as an user. I understand you are a site builder rather than a developer, so sorry if my answer is too technical.
Direct answer
I will not advice doing that, recreating the path from other fields. Drupal already know the path and can retrieve and inject it for you, i will propose something later in the message.
Usually, using pattern fields to feed HTML elements attributes, like anchor href attribute, is OK. You just need to be careful you are sending pure textual data and not a renderable array or object.
Where are you retrieving your node title from? A field formatter? if so, use a field formatter like the "Plain text" (string) formatter provided by Core. If I remember right, this formatter will inject only the text value of the node, and it will work as expected.
Better proposal
There are 2 things wrong about my direct answer:
If it is your case, and if you have the ability to change the card CSS, i will inject the link element itself instead of just the URL, and start something like that (not tested proposal):
And then, use the same "Plain text" (string) formatter and check the " Link to the Content" setting. It will produce a HTML link element markup, which will be managed correctly.
Comment #6
pdureau commentedComment #7
pdureau commentedComment #8
pdureau commentedComment #9
nigelwhiteThank you for your answer and explanation. Here are the results...
Here is my pattern twig
Your first suggestion -
I am using DS. The only field formatter provided by core on the title field is "Default". It comes with a h2 wrapper, but I have removed this. The result is
honey1
Result = The link is placed on only the title. It should be on the whole card. The 'link to the content' setting in DS makes no difference here. The title 'Honey' above the card should not even be there. Both links have the correct destination '/honey'
Your second suggestion _
I changed my twig to
So I have put {{link}} instead of {{title}}
The CSS is
[I don't know what this CSS does]
The 'link to the content' setting in DS is on.
Result
honey2
There is a link on the title, which points to the correct content (ie /honey). There is a link on the whole card, which points to the homepage in every case.
Do you know anything else I could try?
Comment #10
pdureau commentedThis CSS was an untested idea (I am not a front dev) and related to Layout Builder. You are using Display Suite instead, which is using the admin theme, so forget everything I said about using a visual page builder.
If this formatter is producing raw text value instead of markup, it is OK.
Maybe it is a pure "CSS in the browser" issue? Do you have an HTML link (injected in image, title or badge) into an HTML link (the wrapper)? I tcan turn the browsers crazy.
Comment #11
duaelfrTo answer your original question, there are multiple ways to solve your issue:
Comment #12
duaelfrQuick update: I just discovered the No Markup module that could be an addition to the first option I described above. Instead of marking all fields as not having markup, you can use this module to do it field by field.
Comment #13
duaelfrThe questions has been answered. Do not hesitate to reopen if needed.
Comment #14
grimreaperUpdating issue credits.
Comment #16
nigelwhiteThank 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 -
For links -
"<a href='https://example.com/'>Link to source</a>"Thank you for a great module