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.

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.

DrupalCamp London 2019

4th March 2019 - 8 minutes read time
On the weekend of 2nd and 3rd March I went down to London to attend DrupalCamp London 2019. This weekend I went with 9 other colleagues from Access, which is almost certainly our biggest company outing to a camp.

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.

PHP:CSI - Random Increment Insert

26th January 2019 - 7 minutes read time

A while ago I was working on some changes to a website and came across a block of code that made me stare blankly at my screen. The website I was working on was a custom build website, created by another developer at the company I was working with at the time. I have never done a PHP:CSI on this site before but remember being so amazed at what I found at the time that I made a note of it for future reference. I have pondered recently how to approach the analysis of the code.

The code I found was in a method that inserted an item into a database table. For some reason that I can't fathom the developer had opted to not use auto increment ID's and has instead developed a method that essentially randomly decided on an ID number for the item.

I can't paste the entire block of code here, but I can include the region of code that made me scratch my head in bewilderment.

Image Colorising In PHP

14th January 2019 - 3 minutes read time

Colorising images is fairly simple to accomplish, especially using PHP's GD library. All we need to do is load an image, create a blank image of the same size in a particular color and then merge the two images together.

In fact, we can do this entirely with the imagecopymerge() function, but creating a function to wrap all of this makes sense as well.

The following function takes an image resource (as created by imagecreatefrompng()), the red, green, and blue values of the color, and the percentage to overlay the color on top of the image. The percentage can be set to 0 for no effect and 100 to fully replace the image with the given color.

Drawling A Line With Pixels In PHP

13th January 2019 - 8 minutes read time

From the drawers of "I didn't realise how complicated that was" I was wondering the other day how to draw a line using just pixels. This turned out to be more complicated than I thought.

Normally in PHP you would use the imageline() function to draw a line between two points. The following block of code creates and image and draws a white line from the coordinates 50x,50y to 200x,150y.

Checking Domain TTL Values

4th January 2019 - 3 minutes read time

Part of the process of putting a new site live can be moving DNS entries around. Prior to doing this it's a really good idea to sort out the Time To Live (TTL) of the DNS record so that when you do change DNS entries you aren't waiting around for a day for the DNS to sort itself out. Most DNS registrars will allow you to set your TTL down to a minute or so.

It's also very important to check the status of your DNS records to ensure that they have the correct TTL, usually a day before (and day of) the move.

You can check the TTL value of your A record with the host command. Change the value of the -t (type) flag to aaaa or cname to inspect different types of records.

host -a -t a www.hashbangcode.com

This will produce the following output. The TTL of the domain below is '125'.

Read Contents Of SSL Cert From The Command Line

4th January 2019 - 2 minutes read time

Whilst it is possible to view the contents of an SSL cert from within most modern browsers I occasionally find the need to use the command line to find out the same information. I find this useful when renewing certificates as browsers can occasionally cache certificates for longer than expected, causing false results.

The following command connects to the server, downloads the SSL certificate from port 443 and then uses the openssl tool to extract the information from the certificate into a readable format.

echo | openssl s_client -showcerts -servername example.com -connect example.com:443 2>/dev/null | openssl x509 -inform pem -noout -text

This produces the following output.

Color Sorting In PHP: Part 4

2nd January 2019 - 6 minutes read time

Following on from by previous post about sorting colors I decided to take another step towards sorting colors by segmenting the data to create a further dimension to the multi-dimensional array.

The array is already split into segments based on the hue of the color, but we can further split this by separating out saturation and value into separate arrays within hue. To do this we set the saturation or value to be a constant and push them into separate arrays.

The new code looks like this. Please excuse the duplication of code here. This is just a simple example to show how the array is put together.