Skip to main content

Setting up a new local site environment

Setting up a new project in Docker and VS Code.

Using Terminal, go to your Sites directory and create a new directory using the mkdir command.  In this instance I'm going to create a new directory titled ADCE

shortcut command to access the Sites directory

cd ~/Sites

Create the new directory

mkdir ADCE

Go in to the new directory

cd ADCE

Run the devcontainer command

How to programmatically create a custom Drupal 8 block

Blocks are a great way to add pieces or chunks of content to your Drupal site.  Capable of displaying simple text, images, forms or complex logic.  There are plenty of sites around that show you how to add a simple custom block.  However, if you are a developer like me, when you are writing your custom block programmatically it has far more requirements than a simple block.  In this article, I'll show how to:

Loading and accessing Drupal node data - like title, nid and other values

Loading the node

To query the node, I prefer to create a series of functions to mange the process in a class.  However, keeping this simple, this can be achieved through:

$node = \Drupal\node\Entity\Node::load($nid);

Otherwise if you require to load multiple nodes, where the list of node.nids is in an array.

$nodes = \Drupal::entityTypeManager()->getStorage('node');

Adding Tailwind css to Drupal

Tailwind CSS is a highly customisable framework that delivers developers the tools to quickly pull your site together.

 

Initial Configuration

The installation and configuration steps are essentially the same as those outlined within the Tailwind documentation.

Note they need to be performed within your custom theme's directory such as /themes/custom/{theme} for Drupal 8

Changing the author name from username to a first and last name

A bug bear that I have had for a while with Drupal content is how come the author of an article is actually their username.  I have an array of reasons to vent my dislike for this strategy... however, instead I will show you how to change it.  Albeit, programmatically!  Don't stress there actually isn't a huge amount of code to add.  In time I'll create a module so no coding is required.

 

Adding time to read to a Drupal article

Time is essential to all of us... and if you are like me there is never enough of it!  Anyway, the amount of time to read a piece of content can persuade someone whether to read on further... or swipe it away.  When on the verge of flipping an article as the title and /or teaser text hasn't got me across the line, the time to read is a handy reference for me.  This handy feature is becoming more common across any decent article publisher.

Performance testing your Drupal website

Load testing verifies the system performance under the expected peak load.  The peak load needs to set by a series of parameters that you have benchmarked targets.  For example, these parameters could include:

Composer PHP version part 2

As outlined in an earlier article composer php version, when installing the latest version of Drupal the Plesk version and the server version can be different.  In plesk, the version of PHP is 7.3.18, whereas, on the Centos server a check through Terminal (php -v) shows PHP 7.1.33 (cli).

When attempting to install Drupal 9 through composer, the following errors will be shown:

Registering users via REST

Registering a user via REST is an important process particularly when the requests come from a mobile device.

In working through the process you might have come across error message like:

403 Forbidden : "Only anonymous users can register a user."

422 Unprocessable Entity : "A Password cannot be specified. It will be generated on login."

So how did I get these and what to do?

Applying a patch via Terminal

As a process I apply patches locally first, then using git upload the update(s) to a development site on the server.  Once the testing has been completed, the final step to apply the patch to the production site.  

Using Terminal or your preferred shell program, navigate to the correct directory.  Then run this command using the name of the patch file (example.patch):

Subscribe to Drupal