I can print title of my content-type
using {{ fields.title.content }}
. and it renders like <a href='...'>Title of the content</a>
. I have created a View
(view mode: field) and showing a list of available products on that page. To customize look and feel of the page I copied views-view-fields.html.twig
file from core/themes/stable/template/views
under my theme's template directory and customized it the following way.
<div class="col-md-2 col-sm-2 col-xs-12">
<div class="box-gray-border-only border-radius-5 padd-all-5">
{{ fields.field_product_image.content }}
</div>
</div>
<div class="col-md-10 col-sm-10 col-xs-12">
<h3 class="item-list-title-anchor margin-auto larger">{{ fields.title.content }}</h3>
<p class="margin-t10 small">{{ fields.field_product_summary.content|striptags|convert_encoding('UTF-8', 'HTML-ENTITIES') }}</p>
</div>
<div class="clearfix"></div>
<div class="row">
<div class="col-md-12 col-sm-12 col-sx-12 text-right">
<span>{{ fields.field_category.content }}</span> |
<span><b class="text-red">Price: {{ fields.field_product_price.content }}</b></span>
<a title="{{ fields.content.title }}" class="btn btn-warning" href="{{ path('entity.node.canonical', { 'node' : node.id}) }}">View »</a>
</div>
<div class="clearfix"></div>
</div>
<div class="margin-t20"></div>
<hr />
The title itself is linked to product's page. But I have an additional View button which I want to link to the product page separately as well. In order to accomplish the same I tried to extract the URL
part from the title
with {{ path('entity.node.canonical', { 'node' : node.id}) }}
. But it gives me the following error message.
Symfony\Component\Routing\Exception\InvalidParameterException: Parameter "node" for route "entity.node.canonical" must match "\d+" ("" given) to generate a corresponding URL. in Drupal\Core\Routing\UrlGenerator->doGenerate() (line 193 of core\lib\Drupal\Core\Routing\UrlGenerator.php).
I am referring to this article: https://chromatichq.com/blog/creating-links-within-twig-templates-using-path-and-url
What I did wrong?
This is what I want to achieve:
I used {{ dump(fields.title.content) }}
to see if I the URL
could be accessible, but there was nothing like it.