logo

node template cheat sheet Drupal 9 and Drupal 10

20
August

node template cheat sheet Drupal 9 and Drupal 10
By: Anonymous | Published On: Tue, 08/20/2024 - 22:36

How to get field value of content type on node.html.twig

Get image field value at node.html.twig at Drupal 9 / 10

{{ file_url(node.field_image.entity.fileuri) }}
{{ node.field_image.alt }}

Get node crated date at node.twig.tpl

{{ node.getCreatedTime|date("d/m/Y")}}

Get term name of reference field in node

In following example field_company_name is a term rerence field in a content type so need to render that reference term name at node template

{{ node.field_company_name.entity.name.value}}

To render field value of term reference field

In following example field_company_name is term field in content type and field_pan_no is at field in reference term so we get value of field_pan_no which is exist in taxonomy

{{ node.field_company_name.entity.field_pan_no.value}}

To get value of user reference field

In following example field_refer_user_id is user reference field in content type which reference with user, we get value of field_company_address from user

{{ node.field_refer_user_id.entity.field_company_address.value}}

Render field value with number format, change number format

{{ node.field_price.value|number_format(2, '.') }}

Set value in a variable on node template, in following after division store value in a variable price

{% set price = node.field_price.value/2 %}
 {{ price }} 

Need Help ?