How to Use the Ternary Operator in Twig in Drupal 10

In Drupal 10, you can use the ternary operator in the same way as in regular Twig code. Here's an example of how you can use the ternary operator to create an array of CSS class names based on some conditions:

Get url path of referenced node image in Twig and Drupal 10

This snippet will definitely save you some time searching for the proper structure, it loops over entity referenced nodes and sets a background image from that node

How to add direct share links to your Drupal page in page.html.twig

The following snippets can be used to get direct share links to Facebook, Twitter and LinkedIn in your page.html.twig

Get a link field url and value in Twig for both internal and external links

Drupal's link field is quite generic. You can both add internal paths as external links. Here is how get the correct values in twig.

How to know if a user is logged in in page.html.twig in Drupal

This is quite an easy one. You get the following variables available in page.html.twig:

How to get entity reference value and link to it in a paragraph

I had this use case where I had to show a label and link to another content type from within my paragraph. Because the theming was specific, I had to get the label and link in twig

How to do a for loop in Twig PHP

This is the way to do a "for" loop. We are going to loop over an array of Node objects of type 'sector'

Five ways of using the path twig function for route links in drupal 8, 9 or 10

Linking to routes in Twig has become much easier since the release of some updates in Drupal 8. Here are 5 snippets that help when linking to internal routes.

Check in twig if image field has value [drupal 8]

In a node teaser or full view mode it is for front-end purposes needed to know if an image is available. This snippet takes care of it.

Get views row count in twig [drupal 8]

When using twig, sometimes you would like to know whether the view has results or not. This snippet shows you how.

Subscribe to Twig