Drupal 8

Posts, reviews and snippets about Drupal 8.

Drupal 8: Repairing A Broken Multi-Site Configuration Setup

4th December 2018

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 8: Multi-Site Configuration With Configuration Split

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.

philipnorton42 Sun, 12/02/2018 - 13:36

Creating hook_init In Drupal 8

23rd September 2018

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.

Drupal 8: Poking A Hole In The Shield Module

25th August 2018

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 8: Custom Ordering Of Comments

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.

philipnorton42 Sun, 06/10/2018 - 10:38

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

15th May 2018

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 8 : How To Avoid Block Caching

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.

philipnorton42 Sun, 02/11/2018 - 17:21

Drupal 8: Detecting An Anonymous User

28th June 2016

To find out if the current user is anonymous use the following.

\Drupal::currentUser()->isAnonymous();

This can be used as part of an if statement like this:

Drupal 8: Get The Current Language

26th April 2016

To get the currently selected language on a Drupal 8 site you need to run the following.

$language = \Drupal::languageManager()->getCurrentLanguage()->getId();

This will return the 2 character ISO language code for the language. So for English this would be "en".

To get the language name instead of the language code use this.

$language =  \Drupal::languageManager()->getCurrentLanguage()->getName();

Drupal 8: Include A Twig Template

1st April 2016

When creating re-usable theme templates in Drupal 8 it's quite easy to include that template by using the following snippet.

{% include directory ~ '/templates/parts/footer.html.twig' %}

This assumes that your footer.html.twig template file is kept in the same theme as the template you are working on. It's also good practice to add the template part to a directory called 'parts' or 'components' to separate them from the normal Drupal templates.