Blog

Drupal, Typescript, Angular and Midjourney blog content, code snippets and tutorials.

Blog

How to add github repository to composer.json to be used as library in drupal 8 [SOLVED]

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.

Blog

Delete all users of a role in drupal 8

Simple snippet on how to delete all users of a given role.

Blog

Fixing the system.site uuid on config import problem in drupal 8

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.

Blog

Set link field programmatically in drupal 8

Saving a link field is quite different from saving a text value in drupal 8. here's how its done.

Blog

Set geolocation field programmatically in drupal 8 [solved]

Populating geolocation field programmatically in drupal 8 is different from normal field values. This snippet does the trick:

Blog

Node access issues with content moderation

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. 

Blog

How to forced uninstall a (missing) module using drush

Sometimes you forgot to uninstall a module when you removed it. To keep thing clean, uninstall the module using the following drush script.

Blog

Perform math calculation and add result to view programmatically [drupal 8]

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.

Blog

Preview (image) file entities in file overview or views [drupal 8]

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.

Blog

Delete all terms from vocabulary [drupal 8]

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.

Blog

Link to a node in twig [drupal 8]

Twig is the theme engine in drupal 8 sites. This snippet how to link to nodes correctly, without using preprocess functions to do it.

Blog

Set format of date field in twig using drupal 8

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. 

Blog

Drupal 8 trim body text in twig

Sometimes you need a body text that is trimmed. No need to install contrib modules.

Blog

hook_views_pre_render: add javascript to view in drupal 8

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.

Blog

Create a batch process with interaction in drupal 8 [SOLVED ]

Batch processes give you a hard time when using them for the first time. This module example will handle every aspect of it!

Blog

Uri to image style url [drupal 8]

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.

Blog

How to save meta tags to your node programmatically

When importing content, it is really important you also think about meta tags on your import. Here's how to save them to nodes.

Blog

How to programmatically update an entity reference field in drupal 8

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.

Blog

Create paragraphs and attach to node programmatically in drupal 8

Paragraphs give great power to drupal 8. This code will show you how you can add them programmatically, for example when importing stuff.

Blog

Send e-mail with managed file attachment in drupal 8

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.

Blog

Cache block per user in drupal 8

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.

Blog

Drupal 8 swiftmailer html template example

Here's a html e-mail template example for sending html e-mails.

Blog

Fix the "Entities exist of type x and need to be deleted before importing" drupal 8 error

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.

Blog

Get single field value in drupal 8

getting entity field values programmatically is easy, but check this out.

Blog

Send email notifications to webmaster on certain events in drupal 8

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.

Blog

How to send html emails with drupal 8 [SOLVED]

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.

Blog

How to get config entity value in drupal 8

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.

Blog

Drupal 8 messenger api: how to set message examples

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.

Blog

Get list of active languages in drupal 8

Short snippet to get the enabled languages programmatically

Blog

Add a block class programmically in drupal 8

Small snippet to show how to add a class to your block

Blog

How to add javascript to custom form in drupal 8

The way of handling javascript and css is different from drupal 7. This snippet shows how to do it.

Blog

Allow adding roles but disallow administrator role on the user registration form [drupal 8]

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.

Blog

Control block access programmatically in drupal 8

<p>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.</p>

Blog

How to create a settings form to edit and save custom configuration in drupal 8

<p>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.</p>

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

Blog

Show the file description in a custom formatter in drupal 8

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.

Blog

Make a views exposed form field required programmatically [drupal 8]

This is an interesting trick to make fields required in the front-end.

Blog

Check in twig if image field has value [drupal 8]

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.

Blog

Get views row count in twig [drupal 8]

When using twig, sometimes you would like to know whether the view has results or not. This snippet shows you how.

Blog

Get drupal 8 hostname or base url

How do you get the base url of a drupal 8 website programmatically?

Blog

EntityQuery nodes with date field in drupal 8

Little snippet to show how to query nodes with f.e. condition of "1 week ago".

Blog

Date fields showing up in wrong language in drupal 8 [SOLVED]

In drupal 8 you can experience your datefields showing up in English instead of in the language of your website. This fixes it.

Blog

Redirect user on login to admin/content page in correct language

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.

Blog

How to patch htaccess file in composer.json - drupal 8

A composer update of your drupal can override changes to your htaccess file. With this snippet you can patch it correctly.

Blog

How to add a javascript library to composer.json in drupal 8

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.

Blog

Translate page title of custom controller in drupal 8

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.

Blog

How to change labels of date field in views exposed form

The typical 'from' 'to' datefields are not quite good for usability. With the following snippet you can change the labels.

Blog

Get (custom) config settings programmatically in drupal 8

This snippet shows how to get data stored in config.

Blog

Create link with class in drupal 8

Snippet to create a link with a class programmatically.

Blog

Make your theme css available in CKEditor of drupal 8

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.

Blog

How to create a mailchimp subscription with drupal 8 webform and custom handlers [VIDEO]

This code will make you manage subscriptions in mailchimp via a drupal 8 webform.