Blog

Render a block programmatically in twig with drupal 8 or 9

In the early days of Drupal 8 we used the EntityTypeManager to render some plugin blocks or other entities. But this changed a bit. Here's a snippet on how to render a block inside your twig files.

Blog

Set media image url with image style token for meta image in drupal 8 & 9

Image url is a must-have for webpages to be properly shareable on social media. With this token snippet you can set a media entity url with image style token.

Blog

Redirect taxonomy terms to filtered view page

A common case when using facet searches is where you want to list some terms and when people click on them you get redirected to a filtered view. This code helps you with that.

Blog

Loop through entity reference in Twig to get id's in drupal 8

Entity references are widely used in drupal to link to other entity objects. Here's how to loop over them in a twig file.

Blog

Check if entity object is of entity type node

<p>There are some ways to make sure your loaded object is in fact an entity of type node. I prefer the following way. In addition I check if the entity types are correct too.</p>

Blog

Translate dates in Twig with drupal 8

I had to output my dates in dutch format, but struggled a bit to do it automatically. I discovered a nice twig built-in feature.

Blog

Add html to a menu links in drupal 8 (twig)

I found a nice way to build complex menu's with twig in Html. Here are a few snippets

Blog

Add views machine name as a class in twig [drupal 8 or 9]

With this views-view.html.twig file in your theme, you add the machine name of the view to your container class.

Blog

How to update a custom entity in drupal 8 once installed: add a field

Custom entities provide great control and flexibility because your entire entity resides in code. This snippet shows how to add a field to your entity, with an update hook. Because once the entity is installed, you should be able to add/remove/edit your fields.

Blog

Create an extra tab on the user log in page

I had a case where an extra tab was necessary on the user pages. This snippets show you how to do it.

Subscribe to