I recently needed to add functionality to the Password Policy module so I thought I would outline the steps I took in a blog post. The Password Policy module is used to enforce a stricter password policy on users on a Drupal site. This means that when a user creates or changes their password they must conform to certain rules like the password length, or if it contains upper and lower case characters. There are a set of rules to chose from and they can be fully customised by the site administrators. It's a good module, you should check it out.
When installing composer dependencies those dependencies are downloaded and stored in the 'vendor' directory. There are options available to install this into a different location than the composer.json file, but it's generally found in the same directory. The vendor directory contains quite a bit of code, which is especially the case if your project contains quite a lot of dependencies. More often than not though, this directory will not contain any code that you have actually written. It will contain the necessary third party libraries that allow your code to work correctly.
One gotcha when importing configuration to a Drupal site can be a message that tells you that the configuration you are trying to import is from another site.
Here is the error that can be seen on the Configuration Synchronize page.
The staged configuration cannot be imported, because it originates from a different site than this site. You can only synchronize configuration between cloned instances of this site.
This error happens because the UUID of the site (in the database) is different to that in the configuration (in the code). The UUID in the code is stored in the configuration file system.site.yml. The reason for this check is to ensure that your don't destroy your Drupal site by installing configuration that was created from another site.
Installing a Drupal site from configuration is useful when running tests or if you don't have a copy of the database. You'll get a copy of the Drupal site without any content that will act in the same way as the live site. You can use modules like default content to add content into the mix so your newly installed site acts a little bit more like the live version.
Since there are some prerequisites to get this up and running I thought I would run through what is needed to get this working and how to run it.
A change that was snuck into Drupal version 8.8.0 and wasn't mentioned in the 8.8.0 change notes was a small change to the setting that controls the placement of the configuration directories. The alteration deprecated the $config_directories setting from the settings.php file and move the configuration into the $settings array. You can see the detail behind this change on the Drupal change record.
The fix for this is very easy and only needs a single line of code to change in your settings file.
$config_directories['sync'] = '../config/sync';
Whilst setting up docker on my local development machine the other day I encountered a permission problem. After installing docker I found that I had this permission problem that meant I couldn't run docker using my local user accounts. I was therefore forced to run docker as sudo, which I didn't want to do every time.
This is the error I was getting.
A common technique when creating graphics or visual representations of data is to map a value between two scales. This is useful when working on a set of values and you need to map them to a different set of values in order to show them on a graph.
The maths involved here is essentially figuring out the relationship between the value (v) in our initial scale (x, y) and multiplying this by the maximum range of the second scale (a, b).
- a + (b - a) * ((v - x) / (y - x))
Taking the value of 0.5, which is in the scale 0 to 1. Mapping this to the scale 0 to 100 means we make the following calculations.
0 + (100 − 0) × ((0.5 − 0) ÷ (1 − 0))
This results in the value of 50.
The PHP splat operator (...) has been available in PHP since version 5.6. When it was introduced I made note of it but have never really used it, so I thought it might be interesting to explore it a little.
Internally, the ellipsis operator in PHP is called T_ELLIPSIS, although I have heard a few different names for the operator in the past. This includes names like:
- Unpacking operator.
- Packing operator.
- Three dots operator
- Spread operator
- Splat operator
Personally, I think the splat is the correct name for this operator, so I'll be using that from now on.
A Data Access Object (DAO) is a way of taking data out of a database or API and present it in a unified way across your application. As a design pattern this has uses in standardising how a particular bit of data is passed around, without having to resort to using arrays to accomplish the same job.
I was doing some work with PHP's PDO library the other day when I noticed that I could fetch data out of a database using the fetchAll() method with the PDO::FETCH_CLASS flag. This flag automatically returns the data in an object. This got me thinking more about how this worked, but I couldn't find many articles talking about this. Hence this post.
Setting Things Up
Let's take a class called SpecialText that will store an ID and some text, along with a __toString() method to allow the printing of those values. We will use this class throughout this post to store data from the database.
Spam is a constant problem for any site on the internet that has a publicly available form, but automatically preventing spam can be tricky. The idea is to prevent the automated spam bot from being able to submit data to your site, but not at the detriment of the users. There is a careful balance between preventing spam and prevent real content being submitted by real users. Manually moderating blog comments is usually a good idea, but many websites contain contact forms and user registration forms that are often targeted by spam bots.
Whilst Drupal does have a number of protections against cross site submissions or denial of service attacks and even has build in user and comment moderation. It does, however, need a little bit of help with preventing spam.
Drupal has a number of modules to deal with automated spam and they fall mainly into a number of different categories.