.

What Drupal Development Tools are Worth Using? 7 Handy Solutions

When creating websites on Drupal, as developers, we should try to make our job easier. Managing modules, users, generating code – all these processes can be automated and performed with single commands. In this article, we'll take a look at the tools available and discuss them, giving specific examples of use.

1. Drupal Console

Drupal Console is a powerful Command Line Interface. It's used to generate boilerplate code and maintain and debug Drupal. The latest version of this tool is v1.9.8, released on 28 November 2021.

To add Drupal Console to our project, all we need to do is use one command:

composer require drupal/console:~1.0 \
--prefer-dist \
--optimize-autoloader

After that, we can use various commands provided by Drupal Console. We provide some examples below.

Module generation:

drupal generate:module  \
  --module="modulename"  \
  --machine-name="modulename"  \
  --module-path="/modules/custom"  \
  --description="My Awesome Module"  \
  --core="8.x"  \
  --package="Custom"  \
  --module-file  \
  --composer  \
  --test  \
  --twigtemplate

Entity generation:

drupal generate:entity:content  \
  --module="modulename"  \
  --entity-class="DefaultEntity"  \
  --entity-name="default_entity"  \
  --base-path="/admin/structure"  \
  --label="Default entity"  \
  --is-translatable  \
  --revisionable
  --has-forms

Service generation:

drupal generate:service  \
  --module="modulename"  \
  --name="modulename.default"  \
  --class="DefaultService"  \
  --interface  \
  --interface-name="InterfaceName"  \
  --path-service="/modules/custom/modulename/src/"

User creation:

drupal user:create  username password  \
  --roles='authenticated'  \
  --email="[email protected]"  \
  --status="1"

As we can see, Drupal Console gives us a lot of possibilities. Another interesting option that this Command Line Interface (CLI) provides us with is running a local PHP server to test our website.

$ drupal server

This command will launch a local server on port 8088 for us.

2. Examples for Developers

The Examples for Developers project is one great collection of examples of how we can write our own modules in Drupal. We have 33 different modules at our disposal, from simple blocks, through various types of forms, to controllers with REST API support. We'll probably find everything we need. This module will allow us to learn new things and work faster.

3. Devel

The Devel module includes additional functions and help pages for developers and administrators. It provides us with blocks and toolbars for quick access and developer information. We can use it to “simulate” another user. It's a very helpful functionality, especially when we need to test roles and permissions in Drupal. Devel provides us with features that help us with debugging. And the icing on the cake – we can use it to generate test content.

To install this module, we use Composer.

composer require --dev drupal/devel

4. Weight

Sometimes, it happens in our project that we use modules that use the same hooks. By default, Drupal doesn't allow to choose the order of module execution. However, we can work around this limitation in three ways.

Method 1 – setting the Drupal module weight during its installation

In the install file of our module, we can add HOOK_install and use it to set the module weight.

Drupal 9 provides a built-in feature to deal with this issue:

function your_module_name_install() {
  module_set_weight('[your_module_name]', [your_preferred_weight]);
}

It's a little more complicated in Drupal 7, because we have to change this field in the database by ourselves:

function your_module_name_install() {
  db_update('system')
    ->fields(array('weight' => your_preferred_weight))
    ->condition('name', '[your_module_name]', '=')
    ->execute();
}

Method 2 – Changing the weight in core.extension.yml

If we use configurations in our project, we can change the weight of our module in the core.extension.yml file after exporting them. The weight is shown as a number after the module name. The larger the weight, the earlier the methods in the module will be executed.

Method 3 – using Modules weight

The Modules weight Drupal module will add an easy-to-use configuration of module weights. When we go to the configuration page (/admin/config/system/modules-weight/configuration), we'll see this:

Setting module weights in a Drupal development tool – Modules weight

Here we can easily set the weights of our modules.

5. Settings.php and services.yml

We can find these two files in the sites folder of our Drupal installation. With just a few lines, we can make developing software a lot easier. It's a good idea to create a settings.dev.php file and put all the changes in it. During development, we can include it in the main settings.php file and remove it when we upload our website to the server.

An even more convenient option is to use an environment variable such as ENV and set it to "dev". Next, we check in settings.php if there's a settings.$env.php file. If so, we include it.

.
$env = getenv("D_ENV");

if (file_exists($app_root . '/' . $site_path . '/settings.' . $env . '.php')) {
  include $app_root . '/' . $site_path . '/settings.' . $env . '.php';
}

We can keep all our support changes for development in the settings.dev.php file.

What exactly can we do there?

1. Enable error display in addition to the message that our website has encountered a problem.

$config['system.logging']['error_level'] = 'verbose';

2. Disable CSS and JS aggregation – something we often forget when editing JS styles or scripts.

$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;

3. Disable render cache:

$settings['cache']['bins']['render'] = 'cache.backend.null';

4. Include the development.services.yml file.

$settings['container_yamls'][] = $app_root . '/' . $site_path . '/development.services.yml';

The content of the latter file may look like the one we show below.

parameters:
  twig.config:
    debug: true
    auto_reload: null
    cache: false
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

We enable Twig debugging. These are helpful comments in the HTML structure that'll make it easier for us to find a template file or create hooks. In addition, we disable cache. Also, we add the cache.backend.null service, which we used earlier to disable the render cache.

6. Lando

It's a tool that facilitates the local development of our website. It allows us to run literally anything and is based on Docker. We have CLI at our disposal, which allows us to easily manage our installations. To set up a Drupal installation locally, all we need is a few commands.

First, we create a Drupal project using Composer.

composer create-project drupal/recommended-project lando-demo

Then, we go to the created directory and run the command:

lando init
  • Choose drupal9 as the recipe,
  • name your webroot web,
  • and then name your project.

After this process, a .lando.yml file will be created. We'll find there a lot of different information, such as the name of our project, what services we use (initially, these will include appserver and database), addresses of our application, PHP version, or access data for our database.

Next, we run the command

lando start

Now we can go to the address given in .lando.yml and finish the Drupal installation.

7. Drush

A tool that every Drupal developer should know. Drush has been with us since Drupal 4.7 and is still developed. As of writing this, the latest version is 11.0.8, and it was released on 9 April 2022. Drush allows us to manage our website by importing and exporting configurations, updating the database, or cleaning the cache. Newer versions of this tool also allow us to generate code. Let's go over some useful commands:

Cache cleaning

drush cr

Configuration importing

drush cim

Configuration exporting

drush cex

Creating a one-time login link

drush uli

Module enabling

drush en module_name

Module disabling and removing

drush pmu module_name

Database updating

drush updb

Watchdog checking (dblog module)

drush ws

When it comes to code generation, we have a lot of options to choose from. We recommend using the command

drush generate

and finding the option that interests us the most.

Drupal development tools - summary

We went through some interesting tools that we can use when working with Drupal. As we can see, Drupal development can be quite quick and pleasant when we know the tools that make the job easier. It's good to take some time to explore these solutions. This investment will pay for itself quickly!