logo

Render Field Variable In Node Twig Template (Node.html.twig) Drupal 8

03
June

Render Field Variable In Node Twig Template (Node.html.twig) Drupal 8
By: Anonymous | Published On: Thu, 06/03/2021 - 19:19

Render Field Variable In Node Twig Template (Node.html.twig) Drupal 8

Show Node Created Date

If you want to show node created date on template you can use code as below

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

You change date format as your requirement

Display Title of node

{{ node.title.value }}

Print node url

{{ url }}

Show Term Reference Field

To display reference term name the you can use code as below

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

In example "company_name" is term reference type field in node and its reference with a term so It will be display that reference term name

Display Custom Field Value of Reference Term

If you want to display field value which in reference term

{{ node.field_company_name.entity.field_gst_no.value}}

In the above example reference term field exist in node as "field_company_name" and "field_gst_no" is a text field which exist in term detail field so its return value of "field_gst_no" from term.

Display Title of Node Reference Field

If you want to show node reference field title

{{ node.field_refer_workshop_id.entity.title.value }}

In the above example a node reference field "field_refer_workshop_id" so its render title of other node which is linked with reference field.

Display Value in Decimal Number Format of Numeric Field

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

In the above example "field_workshop_price" is integer field which display value in decimal format, after decimal its shows 2 digit. you can change number digit after decimal as your requirement.

Display Field Value In Lower Case in Node Template

{{ node.field_city.value|lower }}

In the example field name as "field_city" in content type so it show city name in lower case.

 

Get field value if type is multiple type, In following body field is multiple type field where will post multiple value in their content.

			 
{% for item in node.body %}
	{{ item.value|raw }}
{% endfor %}	

In the above example using loop will print multiple value

 

In following example field_course_ref is a field which reference with other node, field_course_type is a field of the reference content type which is a term so we get term name

{{ node.field_course_ref.entity.field_course_type.entity.name.value }}

How to get the field_image path with Twig in Drupal 8

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

Display alt text of image

{{ node.field_image.alt }}

Need Help ?