Drupal 9: Get List Of Content Types

3rd May 2021 - 4 minutes read time

Getting a list of content types out of a Drupal 9 site is useful in a few situations. Mostly, I find that when creating a service of some kind that I will also create an administration form for that service to allow it to be restricted to certain content types. This list can be saved to configuration so that when the service is run it only effects certain content types, based on the saved configuration.

To get a an array containing a list of the content types on a Drupal site you can use the following code.

Drupal 9: Auto Tweeting From A Drupal Site When Content Is Published

26th April 2021 - 18 minutes read time

Normally, when creating Tweets from Drupal 8 I use the Social Post Twitter module. This module is part of the Drupal Social Initiative and has been my go-to module when I've needed to read or send Tweets from a Drupal site. Since the release of Drupal 9, however, these modules have not been receiving the support needed and as of writing this article there is no easy way to install them on a Drupal 9 site. I've looked into the issue queues and can't see why the delay is there.

The Social Post Twitter module does have a lot of features that I didn't need for what I was looking for, which was to send Tweets when items of content are created. I decided to see how difficult it would be to send Tweets from a Drupal site as an item of content is published.

Drupal 9: How To Tell If A Page Is First Published

19th April 2021 - 5 minutes read time

I was writing some code on a Drupal site that detects if a page is being published and I realised that this state isn't as clear cut as you might expect.  Drupal stores the published state of a page as the 'status', with 0 being unpublished and 1 being published. With revisions being turned on by default since Drupal 8 it is possible to see past states of the page when saving the page. The issue is that there is nothing in the current state of the page stating that this is the first time it is being published.

As an example of this in action, and where I cam across this, we can use the hook_ENTITY_TYPE_update() hooks to detect if a page is published as it gets updated like this.

Godot: The Open Source Game Engine

11th April 2021 - 7 minutes read time

I have been watching a lot of game development coding tutorials over the last couple of years. This includes things like the excellent Game Developers Conference (GDC), Sebastian Lague and his wonderful coding adventures series, and Yahtzee's dev diary series to name a few.

It's difficult to not see developers use Unity when demonstrating their game developing online. Some developers will directly reference it, whilst others will simply look at the code they are writing. Unity, though, didn't appeal to me as it comes with some licensing costs. You only need to pay those licensing costs if you release a game, but as a hobby developer who advocates free and open source development Unity just didn't sit well with me.

Drupal 9: Some Strategies For Developing Update Hooks

4th April 2021 - 15 minutes read time

Drupal's update hook system is a powerful way of updating your site to introduce things that wouldn't be handled using the configuration system.

Modules will use update hooks to bring sites that have the old version of the module in line with the latest additions to the module. For example, if a new field is added to a table that the module uses then an update hook will be needed to add that field to all sites that are current using the old version. This update hook will be in addition to the install hook that would install the table with the added field in the first place.

There are a number of different reasons why you would want to use update hooks on your own site. Normally being stored in either install profiles or custom modules they would be run on deployment in order to update your dev/stage/production site with changes without having to manually apply them. This is a useful way to do one of the following actions.

Turn Off Fn Mode In Ubuntu Linux

2nd April 2021 - 4 minutes read time

I was typing on my Keychron K2 keyboard today and realised that I hadn't used the function keys at all. Not that I hadn't tried a few times, it's just that the function keys were linked to the media keys for the laptop I was using. When I pressed the F2 key it would increase the screen brightness, which was fine, but I realised that I had missed using the function keys for a while.

I was using Ubuntu 20.04 and there appeared to be no option to turn this off in the settings. After a little research I found a solution to the problem.

Using Shadow DOM

23rd March 2021 - 19 minutes read time
When I first heard the term "Shadow DOM" I had to laugh at the name. I had visions of a cloak and dagger HTML element that would only be seen on the dark web. Or some sort of nefarious shadow site that was the after effect of a hack. In reality, this is quite a useful bit of technology that has a number of applications, although it does need a fairly modern browser to be able to use it. Shadow DOM is part of the web components strategy, which includes custom elements and HTML templates. These give developers the power to create custom tools and experiences that are encapsulated away from other parts of the code.  In this article I will go though what the shadow DOM is, how to use it, and in what situations it comes in handy.

Simple Horizontal Segmented Bar Chart With CSS

13th March 2021 - 9 minutes read time

Bar charts are powerful ways to show the relationships between different data items. If the data you want to show is discrete then a simple horizontal segmented bar chart is a good idea. You can easily change a collection of numbers into a related set of attributes.

To display this bar chart you don't need a large JavaScript library or an backend charting system, you just need a few lines of markup and some styles. Here is all of the markup needed to generate the bar chart. This consists of a wrapper element and four inner elements that make up the data of the bar chart. Note that the width of each element is pre-calculated to be 25%. I'll address the maths involved in this later in the post.

Creating A Game With PHP Part 4: Side Scrolling Shooter

10th March 2021 - 12 minutes read time

As another step up from the game of snake I created in my last post I decided to try my hand at creating a side scrolling shooter. Just like my other posts, this is an ASCII based game played on the command line.

A side scrolling shooter, if you didn't already know, moves a scene from right to left across the screen with enemies moving with the scene towards the player's ship, which is on the left hand side of the scene. The player can fire bullets towards the enemies so remove them from the scene.

In order to create a side scrolling shooter we need to define a few elements.

Seeding Random Numbers in PHP

9th March 2021 - 7 minutes read time

Computers are not that great at creating random numbers as the methods they use are deterministic. That is, they start from a number (called a seed) and apply maths to that number to generate pseudorandom numbers. Most random numbers generated by computers programs use a seed provided by the system, which is generally the current time. If you can guess the initial value then you can start to work out what the random sequence of numbers generated is. Most random numbers, are therefore not suitable for encryption.