dependency injection

Drupal Logo

Drupal 9: An Introduction To Services And Dependency Injection

15th August 2021 - 21 minutes read time

Drupal 8 and 9 are built upon services, with many parts of the system available through dependency injection, so it's important to understand the concepts. Services are a way to wrap objects and use dependency injection to produce a common interface. They are powerful and are used all over Drupal to do pretty much everything.

They can, however, be a little difficult for newcomers to the system to understand, especially if they are coming from Drupal 7 or other non-object oriented systems. When you look at some Drupal source code you are likely to see objects being created out of apparent thin air. It's a little hard to know where they come from if you aren't used to the how they work.

Drupal Logo

Drupal 9: Integrating Flood Protection Into Forms

13th December 2020 - 10 minutes read time

Drupal's login forms are protected by a protection mechanism that prevents brute force attacks. This means that if an attacker attempts to repeatedly guess a user's password to gain entry to their account they will be blocked before being successful. This system has been a part of Drupal for many years and so is battle tested.

Like all systems in Drupal, the flood system can be adapted to be used on your own forms. Which means you can protect any form that you don't want to be used too much. This will help with authentication forms or any form that might need to process lots of information where you don't want users to submit the form too much.

Before using the flood system on a form you first need to inject it into the form. Here is a basic form setup with the flood service injected into it.

Drupal Logo

Drupal 8: Custom Ordering Of Comments

10th June 2018 - 7 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.

PHP Logo

PHP IP To Location

1st March 2010 - 12 minutes read time

Converting an IP address into some useful location information can be useful if you want to find out where sites are hosted or customise content to users depending on their location.

All this code is freely available over at github.

There are several ways to do this, all of which have their advantages and disadvantages, but sticking with one can cause rewriting a lot of code in the future. So rather than pick one and stick with it I decided to use dependency injection to allow different classes to be used that convert IP addresses to locations in different ways. The first task is to create an abstract class that will be used to construct the rest of the IP location classes. Each class that extends this abstract class will contain a method called getIpLocation() that will convert an IP address into a location, and a method that will update the data source for the location lookup. Rather than lump all of the classes into a single directory I have created a directory called Service, into which all of the different classes that lookup IP addresses will be kept.