Drupal

Posts about Drupal, the open source content management system.

Drupal Logo

Drupal 8 Messenger Service

7th February 2019 - 3 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.


$messenger = \Drupal::messenger();

This can be used to print messages to the user. For example, to print a standard message with a blue background do the following.


$messenger->addMessage('Message printed to a user.');

This can be done in one line like this.

Drupal Logo

Drupal 8: Repairing A Broken Multi-Site Configuration Setup

4th December 2018 - 12 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 - 20 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 - 5 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.

Drupal Logo

Drupal 8: Poking A Hole In The Shield Module

25th August 2018 - 8 minutes read time

The Shield module prevents access to a Drupal site by putting a Apache authentication system across the entire website. This means that to access the site you need a username and password.

This is useful in a number of different situations, but I use it most for protecting dev and staging sites from access. It's not the most complex authentication system in the world, but it's enough to prevent the embarrassment caused by having staging sites being spidered by search engines.

Drupal Logo

Drupal 8: Custom Ordering Of Comments

10th June 2018 - 9 minutes read time

Drupal 8's comment system has the ability to be threaded so that users can reply directly to other users comments and create threads of conversation. I have always found this difficult to use and difficult to read so I wanted to turn it off when I set up this site. The only issue I had was I could turn off the threading but couldn't alter the ordering of the comments.

The default ordering of comments in Drupal 8 is by thread. This means that even if you turn off threading the comments are always displayed in thread order. The CommentDefaultFormatter class is used by Drupal to display comments and contains this line of code.

Drupal Logo

Drupal 8: Date Search Boosting With Search API And Solr Search

15th May 2018 - 8 minutes read time

The Search API Solr Search module has a bunch of controls for boosting certain fields. This allows you to give more weight (i.e. boost) to the title and less weight to the body, which means that when a search term appears in the title of a page it has more weight than a page that only has the term in the body. This weight value is ultimately used to calculate the score of the page and this directly effects the ordering of results.

Drupal Logo

Setting Up Drush Aliases And Using sql-sync

2nd May 2018 - 9 minutes read time

Drush sql-sync is a very easy way of grabbing a copy of a Drupal database from one site and installing it on another site. The normal application of this task is to copy the database from the production site to your local environment for facilitate local development. There are one or two steps to sort out before you can use the command

This post assumes you have Drush 8 installed on your machine as Drush 9 uses a different syntax for sotring Drush aliases. This also assumes that you have setup SSH key access to all servers you need to get access to.

Setting Up Aliases

The first thing you need to do is set up your Drush aliases, this allows you to connect to the remote Drupal instance from your local machine. Drush aliases are kept in your ~/.drush directory. To see what aliases you have on your system use the command.

Drupal Logo

DrupalCamp London 2018

18th March 2018 - 14 minutes read time

It's been a couple of weeks since DrupalCamp London 2018 so I thought I would try to write up my experiences for the weekend. DrupalCamp London ran from 2nd to the 4th of March and consisted of a CxO day before a 2 day conference.

Getting to the conference this year was a little more troublesome than usual. The snow and frost from that week had caused chaos on the trains (not to mention the roads) and so after many cancellations and delays I eventually made it to London the night before the Saturday/Sunday conference. The snow didn’t just affect me though, a few people couldn’t make it and a few sessions had to be rearranged. Thankfully, everyone made it to the camp in the end.

Drupal Logo

Drupal 8 : How To Avoid Block Caching

11th February 2018 - 6 minutes read time

I was struggling with a problem on a Drupal 8 project that was in development recently where a block used to show information to anonymous users was cached for the first user who saw it. This meant that the special message meant for the first user was then being seen by all subsequent users who visited that page. This only happened when page caching was turned on, but as it's best practice to do that I didn't want to turn that off just to solve one little problem.