Drupal & Angular blog content, code snippets and tutorials.

Overwrite input colors in Tailwind custom forms plugin

Tailwind custom forms is a great plugin to quickly theme your form elements. This snippet guides you through some set-up.

Get image field with image style in preprocess_node()

This technique is used quite a lot to get an image styled url of an image in your twig file.

Get view mode inside preprocess_node()

Little snippet to get the current view mode

How to use configuration as cacheable dependencies in drupal 8 & 9

Cacheable dependencies got introduced in drupal 8 and provides great power over your caching.

Enable partial word search in database search API drupal 8 & 9

I did not expect the setting would be here, so I thought I'd share it.

Count query results of entityQuery in drupal 8 or 9

A typical example of querying is getting the row count. This snippet shows you how.

Add a html5 number field in a drupal 8 or 9 form

The form API in core is great, but without contributed modules it lacks a bit of sweetness for html 5.

Add a html field to a form in drupal 8 & 9

The Form API changed a bit over the years. This is the way add a html field to a form in drupal 8 & 9.

How to upgrade from Drupal 8 to 9 with composer [blog post + screen cast]

This blogposts gives you a summary on how to upgrade to drupal 9 and fix all composer issues.

Get all vocabularies in drupal 8 & 9

This snippet works starting in drupal 8.8 and replaces older deprecated ones.

List all campaign url's witch Mailchimp API in PHP

This snippet will help you to list all campaign urls with Mailchimp API

Hide revision log field on a entity form

When using the node system we have a checkbox on the edit page to deactivate a log field. For custom entities we need to do it ourselves.

List of AccessResult types in drupal 8 & 9

Drupal is using more and more AccessResult objects for access checking. Here is a list of all the access results available.

Set moderation state programmatically in drupal 8

Moderation states help you give entities a state in workflows. Here is how to update your entity programmatically to a published workflow.

Get the moderation state of entity or node programmatically in drupal 8

Worfklows are a powerful tool to add events and states to your entities.

Add a menu item to the drupal toolbar

I like being consistent over every back-end I make. Custom forms with settings need to be available from the toolbar.

How to make core version drupal 9 compatibe in module .info file

Make sure to edit the .info file to make your module compatible with drupal 8 & 9.

Delete users that never logged in in drupal 8

An important clean-up task on bigger websites is to remove unused accounts. This snippet does the work.

Get drupal version with drush

Simple command to get the current drupal version

Amazon lightsail instance down every day [fixed]

I hosted a hobby site on an aws lightsail instance but it went down almost every day. It took me long to find the issue. Here's how I solved it.

Get route of a views page with drupal console

I like drupal console a lot as a helper tool for development. In this snippet I wanted to get the route of a views page. The page is on /news-events.

Set frontpage programmatically in drupal 8 & 9

Snippet to programmatically set the frontpage

Give only custom block access but not block overview in drupal 8

Personally I do not like giving block access to website editors. In this one case, there was a need to edit custom blocks. I granted access to administer blocks, but I blocked access to the overview page with this EventSubscriber.

Get current user role in drupal 8 or 9

Getting the current user role in code is quite easy, here's the snippet.

Query nodes in drupal 8 & 9

A simple query example for drupal 8 and 9.

Apply patch in composer without updating other modules

Patching workflow in drupal is something you need to learn a bit. I stumbled on a few cases where I would have to update one module only and want to prevent from updating the entire system. Here's the snippet

Log messages in drupal 8 & 9

Loggin is very important. These snippets need to be used, be as precise as possible.

Patching workflow in drupal 8 & 9

How do we correctly patch our core and modules in drupal?

Get paragraph type in preprocess_paragraph in drupal 8 & 9

Preprocessing on paragraphs is not used very often. But when needed, here is a snippet that gets you the correct type to add some preprocessing.

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.