Skip to main content

Using Twig {% extends %}

In Twig, the {% extends %} tag is used to inherit and extend the contents of another template. In the case you provided:

Using paragraph.getParentEntity() in Drupal Twig templates

In Drupal Twig templates, you can set a variable like paragraph_parent to the parent entity of a paragraph using the paragraph.getParentEntity() method. This is a common approach when you want to access the parent entity (e.g., a node) that contains the paragraph.  Here's how you can do it:

Twig\Error\SyntaxError: Unknown "filter" tag

Twig error

Twig\Error\SyntaxError: Unknown "filter" tag. in Twig\Parser->subparse() (line 6 of themes/custom/{theme}/templates/views/views-view-field--products--field_product_type.html.twig).

Line 6 of the views-view-field--products--field_product_type.html.twig file is 

{% filter upper|escape('html') %}

The actual Twig file code

Creating a node alias path in a twig file

Do you want to know how to add a node alias in a twig file?  Rather than do the set up in a custom module or using your theme file.

To be able to create a node alias in a twig file you need to know the node.nid value.  Obviously without it you have nothing to reference from.  In this example, the node.nid value is extracted during a loop.  A fairly standard loop such as

Displaying a content type that has a paragraph within a paragraph in a twig file

I want to create a content type that has a paragraph.  Easy enough.

What happens when I want to extend this concept and have a paragraph within the first paragraph?  Importantly, then display the second paragraph in a twig file.

How to create a content type that holds a paragraph within a paragraph and make it display in twig files?

 

How do I generate URL by term_id (tid) or node_id (nid)

When you have an entity ID value such as node ID (nid) or taxonomy term ID, how can you create the alias in a twig file?  In other words, instead of /taxonomy/term/{tid} I would like to get the alias I have defined for this, eg: /some/path/to/my/term.

In trying to find an answer you might have queried your search criteria using something like

twig canonical taxonomy path

twig canonical entity path

twig canonical node path

 

How do I get a page’s current URL in Twig for Drupal 9

Having access to a site’s URL in your templates can come in handy for many different use cases, such as determining if your on the admin/people page as apposed to admin/reports/dblog or admin/content.  These three pages might seem to bear on common relationship, however there is at least one.  Users.

I edited the user in the admin/people page so rather than showing just username, know shows:

twig add to array [merge, append, push]

Working in Twig, I had to add classes to a pre-existing array.  Whilst I've grown used to the patterns in PHP, these cannot be applied in Twig.  Instead I had to merge the new array elements.

Non associative arrays

Working from a base level, I'll begin as if the original array hasn't been set.  The merge filter expects as first argument an array whose content will be merged with the assigned variable:

Subscribe to twig