A toolbox like Ionic makes our life as a developer a little bit more easy. This post guides you in how to get the window size dynamically in your components and to set a variable width depending on it.
Finding your way in the UI of ionic can be a bit complicated. This snippet helps to guide you in creating transparent buttons.
Animate.css is my favorite css library to easily add nice open/close or fade effects with css3.
Ionic uses components for layout. This is a gigantic time saver for your web apps. This easy trick makes an ionic item get a transparent background.
A common issue is CORS errors when testing on a local environment on decoupled installations. This will guide you into fixing it.
Service workers are meant to run on production builds over https. But we want to test on localhost.
I stumbled upon some annoying errors setting up Tailwind and Angular. These snippets might fix things.
This configuration setting is easy to get with the following snippet.
Render arrays are great for styling elements to keep consistency across future updates. Today: buttons.
EntityForms are gaining importance in drupal 8 and 9. This is how to make sure you are in one.
Revisions are a great thing for tracking changes between entities. This snippets allows you to name them different in your entity forms.
An often use case for dashboards or software behind a login is to redirect anonymous users to the login page. Like always, I do not want to use contributed modules for this.
Tailwind custom forms is a great plugin to quickly theme your form elements. This snippet guides you through some set-up.
This technique is used quite a lot to get an image styled url of an image in your twig file.
A typical example of querying is getting the row count. This snippet shows you how.
The form API in core is great, but without contributed modules it lacks a bit of sweetness for html 5.
The Form API changed a bit over the years. This is the way add a html field to a form in drupal 8 & 9.
This blogposts gives you a summary on how to upgrade to drupal 9 and fix all composer issues.
This snippet works starting in drupal 8.8 and replaces older deprecated ones.
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.
Drupal is using more and more AccessResult objects for access checking. Here is a list of all the access results available.
Moderation states help you give entities a state in workflows. Here is how to update your entity programmatically to a published workflow.
I like being consistent over every back-end I make. Custom forms with settings need to be available from the toolbar.
An important clean-up task on bigger websites is to remove unused accounts. This snippet does the work.
Simple command to get the current drupal version
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.
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.
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.
Getting the current user role in code is quite easy, here's the snippet.
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
Loggin is very important. These snippets need to be used, be as precise as possible.
How do we correctly patch our core and modules in drupal?
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.
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.
Snippet to dynamically add the year to your page.html.twig file.
This snippet sums op the possibilities of getting the current url path alias.
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.
Full snippet of a working responsive header in Tailwind css
Snipet to make container larger or smaller on certain breakpoints
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.
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.
Metatags can be hard when abandoning the body field in favor of paragraphs. This post gives you a generic solution for the problem.
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.
There are use cases where you do not want drupal to send any mails at all. Here's a snippet that does the trick.
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.
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.
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.
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.
Entity references are widely used in drupal to link to other entity objects. Here's how to loop over them in a twig file.

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.

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.
I found a nice way to build complex menu's with twig in Html. Here are a few snippets
With this views-view.html.twig file in your theme, you add the machine name of the view to your container class.
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.
I had a case where an extra tab was necessary on the user pages. This snippets show you how to do it.
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.
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.
foreach (\Drupal::routeMatch()->getParameters() as $entity) {
  if ($entity instanceof \Drupal\Core\Entity\EntityInterface) {
    // here $entity is your object


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.
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.
This example might help you setting up default installations.
I was building a website with Tailwind and thought it would be nice to share this generic footer html with you.
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.
Caching in drupal 8 is quite nifty, here's a snippet on how to completely disable caching in your controller.
Ik ben onlangs beginnen experimenteren met Tailwind CSS en dit nieuwe framework biedt een hoop mogelijkheden. In twee filmpjes geef ik een introductie.
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.
In this video I discuss block caching in drupal 8 & 9. It is quite radically different from drupal 7. I discuss cache tags, cache context and cache max-age.
A few months ago I released a module that provides an out-of-the-box solution for lazy loading of images in 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:
String translations are always a bit a pain in the ass for drupal. Luckily, in drupal 8 the function is rather easy.
Snippet on how to remove all users with a certain role in drupal.
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.
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.
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
Quick tutorial on how to serve a PDF that downloads automatically in your browser in drupal 8.
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.
When creating seed data, for example in your modules hook_install() script, creating a menulink can be necessary. This snippet shows how.
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.
In drupal 8, each entity will get a UUID assigned automatically. With this snippet you can check if it is a valid one.
In drupal 7 we had some contrib modules using a 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.
The chosen widget is my default option for select widget. This snippet shows you how to add the placeholder in a form alter.
For a few months I'm working in my spare time on a module that can import instagram feeds and turn it into nodes. And on top of that, a nice javascript extension to make it look beautiful out-of-the box.
A thing developers can forget while testing, is to make sure all functionality of your module is stable when used in the context of ajax. Some plugins might stop working after an ajax call on the page. This is how to prevent such thing.
It is a bit strange that the name field (which functions like the node's title field) can be only 50 characters long. The snippet I'd like to share with you, makes it 255 long.
Drupal ships with an ugly 404 page by default. in this tutorial I learn you how to add markup to it.
There is a great shortcut I use a lot when dealing with entity references (the're great, right). I thought I'd share the snippet here.
We all well know the functions to check if a node is published or not, but what about a custom entity?
You create a custom controller and want to show a table that resembles the core back-end tables. This snippet shows you how to do it.
A very recurring thing you would do is to get the current node id of the visited page. Here are two ways of doing it programmatically.
Where in drupal 7 you would typically use something like drupal_get_path_alias(), this is different in drupal 8.
Short snippet that shows how to get url parameters in drupal 8. Where in drupal 7 you would have to do some $_GET or work with args(0), you now have a clean function to do this.
I had to loop over some images in Twig and dynamically adding image styles to them. Without using contrib this seemed a bit hard to do. I found a fix and I share it here with you.
A common issue you would have when using javascript plug-ins is that you do not want them to be added to your version control. You would want to add it to the main composer.json file that has to donwload the library for you. Here is how the solution works.
One thing that bothers me quite a lot is the error when you try to import existing configuration into a new drupal 8 website. I resolved this by adding a custom drush command that gets fired right before config import.
Saving a link field is quite different from saving a text value in drupal 8. here's how its done.
Populating geolocation field programmatically in drupal 8 is different from normal field values. This snippet does the trick:
When using content moderation, the view_unpblished module does not work. But I really needed to get a user role to see all nodes created of this type and have access to it. With the following snippet, I fixed this. 
Sometimes you forgot to uninstall a module when you removed it. To keep thing clean, uninstall the module using the following drush script.
A thing that is very nice about drupal 8 is the flexibility of views In this tutorial, a snippet about how to show a 'total' count of results of some number fields of a table.
I encountered a problem while working on a big migration. The client wanted to reuse files and images, so I added entity browser functionality. The problem however was that views does not provide a preview image for files. Therefore I created a small module.
When importing tags, or when in development, you would sometimes need to delete all terms from a vocabulary. This snippet does the trick for you.
Twig is the theme engine in drupal 8 sites. This snippet how to link to nodes correctly, without using preprocess functions to do it.
Twig comes with quite some handy functionalities. Date formatting is an example. In this snippet an example of how to show the created date in a drupal 8 time format. 
Sometimes you need a body text that is trimmed. No need to install contrib modules.
Use global theme javascript only in a few occasions. You should make sure javascript is only used on the right places. This snippet shows you how to load javascript when a specific view is loaded only.
Batch processes give you a hard time when using them for the first time. This module example will handle every aspect of it!
A much needed functionality is how you get the url of an image style form the uri (f.e. in a node object). This snippets helps you accomplish this.
When importing content, it is really important you also think about meta tags on your import. Here's how to save them to nodes.
Drupal 8 is becoming more and more API-first based. This means a lot of imports and exports of data will be done. A thing you might face here, is that the updating of entity reference fields is a bit tricky. Here I'll show you how it is done.
Paragraphs give great power to drupal 8. This code will show you how you can add them programmatically, for example when importing stuff.
Sending files attached in e-mails is a necessary feature for a lot of applications. This snippet shows you how to attach a managed file f.e. an invoice to your custom e-mail.
I'll share with you some code on how to cache blocks per user. The secret lies in using the right cache context on the build() function of your block.
Here's a html e-mail template example for sending html e-mails.
Configuration management is great. But you'll encounter some difficulties at first when importing existing config to a new site. A typical error is the "Entities exist of type" error. Here's how to fix them.
getting entity field values programmatically is easy, but check this out.
Today I have a small helper module to share with you. A small pain in the ass is that the sending of e-mails requires a bit too much code. More specifically sometimes you would like to send simple alerts to webmasters. This helper module helps you with it.
One of the biggest flaws in drupal is the disability to do something easy as sending html emails out-of-the-box. In drupal 8 it is already slightly better, but you would need to know some things.
Config entities are new in drupal 8. The differ a bit from content entities. Here a snippet on how to get the value of a field programmatically.
Drupal set message was deprecated in Drupal 8.5 and will be removed before Drupal 9. Time for a roundup of this new API and how to use it.
The way of handling javascript and css is different from drupal 7. This snippet shows how to do it.
When you give users the permission to administer users, there is a security implication in that users with this permission can add the role 'administrator' role to any user.

Block access is a powerful way to show or hide content in some contexts. Instead of using contrib modules, this is very easy in small pieces of custom code. The following code prevents access to a block based on a subscription field in the user object.

The following code shows how to create a custom form in the drupal back-end and how to save custom config defined in your custom module. Powerful stuff and very flexible.

You can update custom config with a settings form you build. 
In a custom module *custom_mailing* add a file called *custom_mailing.routing.yml* add the following route:

Custom formatters are great to get precise control over your field output. But I ran into something difficult when trying to output the description of a file field.
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.
When using twig, sometimes you would like to know whether the view has results or not. This snippet shows you how.
How do you get the base url of a drupal 8 website programmatically?
Little snippet to show how to query nodes with f.e. condition of "1 week ago".
In drupal 8 you can experience your datefields showing up in English instead of in the language of your website. This fixes it.
By default you get to see a strange screen saying how long your are a member. It is good practice to auto-redirect users on login to the /admin/content page. This snippet does the trick.
A composer update of your drupal can override changes to your htaccess file. With this snippet you can patch it correctly.
It is seen as a bad practice to add complete libraries to your theme in drupal 8. You should add these as packages to your composer file.
The page callback system in drupal 8 is considerably different from drupal 7. This snippet shows you how to show a title depending on your language.
The typical 'from' 'to' datefields are not quite good for usability. With the following snippet you can change the labels.
Snippet to create a link with a class programmatically.
When editing notes, an often mentioned problem of webmasters is the lack of preview of their text. With this snipped you can embed the css of your theme in CKEditor, which makes it much more visible towards your webmasters.
Drupal 8 will standard give the term name as page title on /taxonomy/term/term-foo. I want to alter this so it says "Items tagged with Term foo". 
How can you query for nodes, terms etc of a drupal 7 website from your drupal 8 website? 
A frustrating thing for webmasters is often that they cannot see the eventual lay-out of text styling in their CKEditor. This snippet fixes this issue.
This module explains how you can add custom scripts inside your controllers. Something that is useful for things like e-commerce and advertising.
This little tip adds just that extra good feeling for your webmasters. And a click less to the "Add content" page.
The following snippet makes you format a date field inside a twig file.
You often need to get the current language in your Drupal 8 custom modules or theme. Here's how to get it.
Sometimes you want to import specific config files, out of your main config library. Here's a drush snippet which does exactly that.
When a drupal site has different domains for its language, difficulties can arise when developing locally. With this snippet, you override your language settings, so prefixes are used instead of domains.
Always try to use the core functions for linking inside custom formatters or modules. The following snippet allows html inside the link text.
I personally like using the drupal menu block module. It allows for quick creation of submenu's. The following patch fixes a bug that keeps showing regions, even when there are no subitems in the menu block.
$date = strtotime($importObject->created_at); // which is a date format
$node->set('created', $date);
// $node->save(); etc


When saving nodes programmatically, you probably want to set the body to accept html. it is important to set the input format, so html is accepted.
This is quite a nice feature to run database operations on your website. Found some wrong implementations so I thought I'd share it here.

From time to time it comes in quite handy to import configuration from other websites into yours. You site uuid is unique though, with the following snippet you get yours.

drush config-get "system.site" uuid

To set the uuid in drush:

drush cset system.site uuid 9b13adbf-0da5-4937-a731-xxxxxxxxxxx -y

A typical setup is one where you would redirect all non www and http requests to https://www.domainname.com. I prefer to keep my .htaccess file in version control also. With the following snippet, I get it the way I want it.

The following snippet will return your langcode f.e. 'en', 'nl', etc. Use the following code:

// Get current language code
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();


I started using an automated script for my drupal installation, which will generate an admin user. But, after installation I have to edit the password.
I like a good user interface for webmasters, so a clear menu path is necessary. For the admin toolbar dropdowns, I usually create subitems programmatically. 
While there are many ways to debug your errors, sometimes just seeing the full errors in your screen works best.
I recently had an issue with ckeditor inside pop-ups (modals) in drupal 8. This snippet is the solution to it.
I recently found out a bug I wasn't aware of. My default text format wasn't saved on a body field. I attached a snippet to fix this.
I've had some difficulties loading a list of translated terms. More in particular I didn't want to show the untranslated terms, but ONLY the ones that were already translated in the current language
A snippet with submitForm to redirect your form to another page and pass a variable.
With this snippet, you will be able to render a list of tags in drupal 8.
By default, users with the permission of 'administer users' cannot assign roles on the user registration form. With this snippet you can, but make sure to hide the 'administrator'-role for your users.
I encountered this bug in a Display suite 2-column lay-out where I wanted the left region to disappear when empty. 
If you like to use the search api or a custom Views page for your search result I personally like to use the default search block and redirect it to my results page.
When creating custom blocks, sometimes you need to make sure the block is never cached f.e. when including dynamic links. 

In this case I created a custom entity called 'submenu'. In my custom module a used HOOK_LINK_ALTER to make the submenu (which contains a textfield) visible on hover. I used the code below to render my custom entity content:

In some cases you want to embed a view inside your custom module. The views_embed_view() function is the way to go. With this little snippet, you can render any view.
With this simple snippet, menu items are saved as expanded when you save your node. In some cases, this is a necessary functionality.
When using lazy loading of images, I encountered an important issue. Social sharing engines like Facebook can't read placeholder images. In this drupal 8 tutorial we'll fix this issue by creating a small custom module handling image tokens.
Ever looked at Pinterest or Google images and wondered how to achieve this nice effect of loading in images? One of the things that frustrated me building drupal websites is the lack of nice front-end libraries. Fancyload solves the problem!