Drupal 8

Articles, reviews and snippets about Drupal 8.

Drupal Logo

Drupal 8: Altering Update Dependencies

7th January 2020 - 6 minutes read time

I perform a lot of Drupal updates, and they mostly go very well. The code updates without incident and the database updates apply whatever updates they need without a hitch. Every now and then, however, I will come across a project that hasn't received updates in a while, which means there are a lot of database updates to run through.

Having Drupal run 100+ database updates across modules and core can be a bit problematic. Some modules will require core updates to happen first, and when this happens out of order the database update update fails spectacularly.

Drupal Logo

Drupal 8: Altering Hook Weights

11th December 2019 - 4 minutes read time

Drupal's hook system allows modules to interact with various parts of a Drupal application and is part of the power of the application.

One common issue I have found is altering things that have been added by other modules during the hooks process. For example, a hook might be called that involves gathering data from one or more modules. If you need to intercept this configuration there is no guarantee that your module will be called after the module you are trying to intercept.

Another (more concrete) example is when altering forms. The code below shows a hook_form_alter() being run on all node edit forms on a site.

Drupal Logo

Drupal 8: Prevent User Role Elevation

25th October 2019 - 4 minutes read time
Drupal has a little flaw in its user permission system that allows users to give themselves, or other users, roles that they shouldn't be able to. If the user has the 'administer users' permission this essentially gives them access to alter roles for any user on the system, meaning that they can grand administrator access to any user on the system. The fix to this involves a couple of actions.
Drupal Logo

Drupal 8: Running A Batch Through An AJAX Request

20th September 2019 - 7 minutes read time

This problem came out of a recent project I was working on. I had to perform a bunch of API lookups on behalf of a user that could take a minute or so to complete. The results of the API lookups were cached and so once it was done the site would be very quick, unfortunately the initial API lookup slowed down the page load quite considerably so this created a problem.

Rather than just doing the API loading in the page load process and making the user sit through it I created a batch process to load the API results in a more manageable manner. This created another problem as although the batch runner in Drupal is really good, it is perhaps a little too much just to show to users and expect them to understand what is going on. This led me to think if I could run a batch process via an AJAX callback from the page they were trying to load.

Drupal Logo

Drupal 8: Custom Cache Bins

14th September 2019 - 10 minutes read time

Drupal's cache system is robust, feature full and extensible. It can be used to cache small blocks of rendered output or the results of more complex calculations. The cache can be stored in a for the page request, in a database or in a different cache system.

I recently needed to store a decent amount of data that was pulled from an API. Getting the data from the API took a few seconds so it was important to cache this within the site for quick retrieval. Even just storing the cache in the database was many times quicker than the API so it made sense to cache the results within the site. Having the user sat there waiting for the page to load every time they do anything isn't great.

To this end I created a small Drupal service that would manage this cache for me. Here is the record from the services YAML file within the module.

Drupal Logo

Creating A Language Cascade In Drupal 8

14th March 2019 - 19 minutes read time

From the pages of 'crazy things you can do with Drupal 8' comes this idea that I recently had to implement on a site. This was on a site that had implemented translations, but they needed something a little extra. The central idea was that if you visited a page on the site in a particular language, and that language didn't exist, then to try and give the user a similar language.

If you weren't already aware, Drupal's default behaviour is to return the default language version of a page if the language requested doesn't exist. If the Path Auto module is installed (which is usually the case on most Drupal sites) then Drupal will not be able to translate the path alias correctly and will issue a 404 if the requested translation doesn't exist. Drupal can't take the path for the French page /fr/about-us and find the original node because as far as it's concerned /fr/about-us doesn't exist.

Drupal Logo

Drupal 8 Messenger Service

7th February 2019 - 2 minutes read time

For a long time the drupal_set_message() function has been a mainstay of Drupal development. You can use this function to print a message or an error response to a user. This is useful to do as it can be done almost anywhere before the final page rendering functions and will be printed into the same area of a Drupal page.

This has recently been deprecated in Drupal 8.5.0 and so I was looking around on how to do the same thing in a new way. Drupal now has a messenger service, powered by the class Drupal\Core\Messenger\Messenger. This can be included into your code using the following shortcut.

Drupal Logo

Drupal 8: Repairing A Broken Multi-Site Configuration Setup

4th December 2018 - 10 minutes read time

I recently wrote a post about setting up a multi-site configuration setup using the Configuration Split module. That post was written after I did research into how to set up configuration splits and use them to create multi-site setups in Drupal. One thing I realised when doing that research was that although it's quite easy to get setup with that kind of setup, it's also easy to get it wrong and create a setup that really doesn't work.

Drupal Logo

Drupal 8: Multi-Site Configuration With Configuration Split

2nd December 2018 - 17 minutes read time

Setting up a Drupal 8 site for a multi-site environment with a common configuration isn't too hard, it just requires a little bit of forethought and some planning to get things right. You need to have a default configuration in mind, and then think about how each site can override this configuration in different ways. I have seen it done wrong a few times recently and once you go down the wrong path, getting things back in line again can be a difficult.

The default configuration covers things like content types, vocabularies, fields, views, enabled modules, or anything else that would make up the structure of the site. Each sub-site would override this by adding configuration for theme components, custom block placement, or anything else that is custom to that site.

Drupal Logo

Creating hook_init In Drupal 8

23rd September 2018 - 4 minutes read time

When developing sites in Drupal 7 I found the hook_init() hook was a good way of quickly testing certain things in code. By adding little blocks of code it was possible to build complex SQL queries, inspect internal configuration, or even test complex node interactions.

When I started to develop sites in Drupal 8 I found that I needed a similar mechanism to do similar things. However, the hook_init() hook doesn't exist in Drupal 8 any more so I needed to look at another way of doing this.

In order to do this in Drupal 8 we need to utilise the event subscribers. There are a few different types of event subscribers available, but in order to use them we need to create a little module.

Here is the info.yml file of a module called hook_init.