Drupal & Angular blog content, code snippets and tutorials.

Add body classes in drupal 8 & 9

There are two ways of adding body classes. The first is to add them in twig, the second is to provide additional classes in mytheme.theme.

Add the year to your page.html.twig file

Snippet to dynamically add the year to your page.html.twig file.

Get the current url path alias in drupal 8 & 9

This snippet sums op the possibilities of getting the current url path alias.

Disable css of a hubspot form

HubSpot is a marketing automation tool that offers forms to embed on your website. Here's how to disable the css that comes with it.

Tailwind responsive header menu example

Full snippet of a working responsive header in Tailwind css

Custom container width in Tailwind css

Snipet to make container larger or smaller on certain breakpoints

Get is_front frontpage variable in node.html.twig twig file

This is quite an easy one. I want to know in my node.html.twig file if i'm on the homepage of the site. This snippet does the trick.

Get logo path of custom theme in page.html.twig in drupal 8 or 9

The site logo moved to a block in drupal 8 but with this snippet you are still able to get the custom logo path, as defined in your settings file.

Scrape first available text from your node and save them as meta description in drupal 8

Metatags can be hard when abandoning the body field in favor of paragraphs. This post gives you a generic solution for the problem.

Set first paragraph text as description of metatag

Most websites abandon the body field in favor of paragraphs. One flaw this has, is that no longer a summary is provided for your (auto-generated) meta description. This snippet fixes this issue.

Prevent drupal from sending e-mails [drupal 8 or 9]

There are use cases where you do not want drupal to send any mails at all. Here's a snippet that does the trick.

Display a config value in your theme page.html.twig file in drupal 8 or 9

Often we create a custom settings form with stuff like social media url's or addresses. With this snippet you make the variables available in your page.twig.html file.

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.

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.

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.

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.

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>

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.

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

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.

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.

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.

Redirect form after registration in drupal 8

Sometimes it is needed for a user to be redirected after registration. The user is not logged in yet, and has to go to a landing page with more instructions on verifying his e-mail.

Trim metatag description to 140 characters in drupal 8

By default drupal will take 600 characters to show your description. But you need to know that this setting is used to show in the metatags as well.

Get node from url in drupal 8

<pre>
<code class="language-php">foreach (\Drupal::routeMatch()-&gt;getParameters() as $entity) {
if ($entity instanceof \Drupal\Core\Entity\EntityInterface) {
// here $entity is your object
}
}</code></pre>

<p> </p>

Create forum topic programmatically in drupal 8

The forum module is a core module that was rewritten for drupal 8. In this snippet I show you how to create forum topics programmatically.

Clear specific cache in drupal 8

Drupal's default caching mechanism is to clear "all" cache. This is rather inefficiënt. There are options to clear specific caches only, here's how.

A default .gitignore file for drupal 8

This example might help you setting up default installations.

Tailwind css responsive footer full example

I was building a website with Tailwind and thought it would be nice to share this generic footer html with you.

Validate numeric url parameter for controller using regex in drupal 8

The routing system that drupal 8 offers is flexible in terms of validation. It is possible to validate your route against some typical regex lines.

Disable caching on a drupal 8 page controller

Caching in drupal 8 is quite nifty, here's a snippet on how to completely disable caching in your controller.

Tailwind css introductie (code + video) [dutch]

Ik ben onlangs beginnen experimenteren met Tailwind CSS en dit nieuwe framework biedt een hoop mogelijkheden. In twee filmpjes geef ik een introductie.

How to "composer require" Github package

Sometime you would want to require a module or library from Github instead some packagist or drupal.org. Here's how you would do this.

Caching in drupal 8 & 9 explained [video & code]

In this video I discuss block caching in drupal 8 &amp; 9. It is quite radically different from drupal 7. I discuss cache tags, cache context and cache max-age.

Nid to url in twig [Drupal 8]

What if you have to link to a specific url in in twig, but you only have the nid? Moreover, it is good practice to use the id and not a hardcored url. Here's what you should use:

drupal 8: image field to image style programmatically

Helper snippet to render images with an image style in drupal 8

Create string translations programmatically in drupal 8

String translations are always a bit a pain in the ass for drupal. Luckily, in drupal 8 the function is rather easy.

Delete all users of a role in drupal 8

Snippet on how to remove all users with a certain role in drupal.

Delete all menu links of a menu programmatically in drupal 8

I play a lot with importing and exporting content lately. When I remove content, the created menu items need to be removed automatically. Here's a snippet on how to delete all links in a menu.

Create a menu link with html markup in drupal 8 (menu plugin tutorial)

Goodbye preprocess functions! Welcome to drupal 8. We use a nice plug-in to get control over our menu item and to show html inside the title. In this case we show a counter of the amount of messages a user has.

Format created date of custom entity in drupal 8 [Solved]

Some parts of custom entities seem different from nodes, but mostly they aren't. Small snippet for showing a created date of a custom entity in a controller

Fix "The provided host name is not valid for this server" error of drupal 8

When installing drupal 8, or moving to production one could see this error. The fix is extremely simple. 

Serve a pdf download in controller in drupal 8

Quick tutorial on how to serve a PDF that downloads automatically in your browser in drupal 8.

Change the page title of the user edit form in drupal 8 with a RouteSubscriber

Sometimes simple tasks like changing a page title of a system page can be hard. Luckily, drupal 8 provides clean ways to do that. In this tutorial some info about RouteSubscribers.

How to create menulink programmatically in drupal 8 [SOLVED]

When creating seed data, for example in your modules hook_install() script, creating a menulink can be necessary. This snippet shows how.

Use html markup in form labels in drupal 8

Getting the output like you want it to be an be quite a challenge in drupal. This snippet shows you how you can add markup to your form labels in drupal 8.

Validate a UUID in drupal 8 (check if uuid is valid)

In drupal 8, each entity will get a UUID assigned automatically. With this snippet you can check if it is a valid one.

Drupal 8 check if user is logged in

Check if a user is logged in in drupal 8

Create or add a menu item without a link in drupal 8

In drupal 7 we had some contrib modules using a <nolink> tag to make subtitles in our menu. In drupal 8 I try to avoid contrib modules on my menu items. Here's a hint on how to get it to work.</nolink>

Set placeholder for Chosen widget in drupal 8 form alter

The chosen widget is my default option for select widget. This snippet shows you how to add the placeholder in a form alter.