Key development tools and best practices in Drupal 8

1 April 2019
1
Command-line tools, known as Command Line Interfaces (CLI), have become a must-have tool for the Drupal developer community. Let’s check them out together!
In Drupal 7, Drush was the tool that no developer could do without. Drupal 8 has created a palette of tools, which are even more magical and powerful than before. This is thanks to Drupal's dramatic shift from version 7, which characterised the content management system as a simple CMS (Content Management System), to version 8. Drupal 8 is considered as a Content Management Framework (CMF) due to the independent components and tools it integrates. In particular, version 8 relies on the integration of object-oriented programming, Symfony components, Twig templating engine, etc.

Moreover, new and powerful tools facilitating the life of the community have been integrated in Drupal 8 projects. Additional tools such as Composer and Drupal Console but also the Git versioning tool are now essential in project development workflows, as well as for collaborative work.

Adding new tools to Drupal 8 means learning about them but also being able to understand how they are used in the development phases of Drupal projects. What does each tool do? When are they used? What best practices should be implemented? It is easy to confuse their uses and implement them for functions they were not intended for.

 

Drush

Drush (Drupal Shell) is a command-line tool used to administer a Drupal site. It has been used since version 7 of Drupal. Configuration Management is a new feature in Drupal 8 and Drush offers commands for its management. It is great for admin tasks such as:

Activating a module:

$ drush in <module_name>

Clear the caches:

$ drush cr

 

Understanding that Drupal 8 is essentially based on a configuration management interface (CMI), Drush is also used to import and export configurations between multiple environments and servers (local, stage, preprod, prod):

To import the configuration:

$ drush cim

To export the configuration:

$ drush cex

 

Composer

Composer is a dependency management tool in PHP. It has been adopted in Drupal 8 but is not specific to this CMF. It can be used in other PHP projects. It allows you to download and install a Drupal project, implement contrib modules, themes, external libraries, patches, etc. A composer.json file is generated containing all the libraries required by the project. Once all the libraries are installed, Composer creates a composer.lock file containing the list of libraries and the versions currently installed.

Composer can be used in all development environments (local, stage, preprod).

To install a Drupal project with Composer, simply launch the command:

$ composer create-project drupal-composer/drupal-project:8.x-dev my_site_name_dir --no-interaction

 

To install a module or theme with Composer:

$ composer require drupal/<modulename>

$ composer require 'drupal/<modulename>:<version>'

 

To update a module or package:

$ composer update

 

It is advisable to use Composer to manage Drupal, modules and themes. It allows a more comprehensive use of libraries and external solutions than Drush. Composer is a key tool for managing dependencies. To install Drush, we use Composer with the command:

$ composer global require drush/drush

 

Drupal Console

Drupal Console is a Drupal CLI that can generate a skeleton code and interact with a Drupal 8 instance or application. It has been integrated into Drupal 8 like the Symfony console in order to comply with the best practices of the PHP community and simplify the lives of developers.

It is installed with Composer:

$ composer require drupal/console:~1.0 \

--prefer-dist \

--optimize-autoloader

 

It contains around a hundred commands that can be displayed by launching:

$ drupal list

 

For example, you can generate a module with the command:

$ drupal generate:module

 

And then be guided by filling in the proposed fields as follows:

drupal generate:module 

 

 // Welcome to the Drupal module generator

 

 Enter the name of the new module:

 > My Module

 Enter the name of the machine of the module [my_module]:

 >

 Enter the path of the module [/modules/custom]:

 >

 Enter the description of the module [My Awesome Module]:

 > My super module of the test

 Enter the name of the packet [Other]:

 > Celdia

 Enter the core Drupal version [8.x]:

 >

 Define the module as a functionality <em>feature</em> (yes/no) [no]:

 > no

 Do you want to add a composer.json file to your module (yes/no) [yes]:

 > yes

 Do you want to add dependencies to your module (yes/no) [no]:

 > no

 Do you want to confirm the generation (yes/no) [yes]:

 > yes

 

Generated or updated files

 Site map /media/vhosts/drupal8-test.dev/web

 1 - modules/custom/my_module/my_module.info.yml

 2 - modules/custom/my_module/my_module.module

 3 - modules/custom/my_module/composer.json

 

After confirming the generation of files and exiting the console command prompt, a module will be created in the modules/custom folder.

You can also generate controllers, forms, types of entities, themes, services, etc.

 

Versioning with Git

Git is a distributed versioning tool generally used in open source projects and in Drupal in particular. It allows developers to work in a team by tracking modifications in files. Unlike a centralised environment, distributed tools offer developers greater flexibility by allowing several remote repositories. Each developer has write-access to his/her own public repository and read-access to the repositories of other contributors (or developers). You can also view changes and, if necessary, return to previous versions of modified codes.

When using Git for the first time, make sure you configure it properly. This ensures you will be correctly identified by Git for future modifications:

$ git config --global user.name "First name Surname"

$ git config --global user.email first name surname@example.com

 

You can check the configurations with the command:

$ git config --list

 

Always check the status of files by launching the command:

$ git status

 

Do not hesitate to launch this command each time to see the status of your working copy before starting another task.

To collaborate in a Git project, you need to know how to manage remote repositories. These are versions of the project you are working on, which are hosted on the internet or a corporate network. You may need to clone a repository or project, retrieve and pull data from a remote repository, push work to a remote repository.

 

To clone a repository:

$ git clone <repository>

 

To retrieve it, pull it from a remote repository and merge with the local branch:

$ git pull

 

When the changes are ready to be shared, you can push them to the remote repository as follows:

$git push <remote name> <branch name>

 

This corresponds to:

$git push origin master    

 

It is necessary, and important, to always pull before you push in order to retrieve all the changes from the remote repository otherwise you risk overwriting them. As it is a collaborative project, you must first retrieve modifications made by other contributors using git pull to be on the same index (level) before pushing (git push) yours.

 

Remember that during the deployment process between different environments (local, staging, preprod, prod), not all Drupal files have to be versioned. Only the following files must be versioned:

- composer.lock: this file contains the list of libraries and their current versions. Therefore, it is important to version them.

- composer.json: can be versioned locally

- modules/custom/ needs to be versioned due to the project’s specific code

- themes/custom/ important to version for the same reasons

- config/: the folder containing all the YML configurations.

- sites/: for configuration files only

 

Conclusion

All of these tools are important for developing and deploying applications with Drupal 8. They also perform different and complementary tasks:

- Drush for site admin

- Composer for managing libraries

- Drupal Console for generating code

- Git for versioning

However, you need to master these tools before using them on a large scale project. In a project, it is advisable to start using them in a limited scope at first. This gives project collaborators time to develop their skills before deploying them on a large scale project.

 

1

The author:

Cellou Diallo is a PHP/DRUPAL Consultant and Developer at AUSY in Montpellier. He is a big fan of free and open source software and everything related to sharing, collaboration and collective intelligence. A member of the Drupal community in Montpellier and the Drupal France association, he organises and speaks at Drupal Montpellier Meet-ups, and contributes to the development of Drupal by translating it into French, collecting feedback and fixing issues and bugs, etc. He continues to develop his skills and hopes to be - in a few years - a Drupal Expert. Cellou is also skilled in mapping and GPS thanks to his geomatic training. He participated in the humanitarian mapping of towns and villages in Guinea (his country of origin) during the Ebola epidemic. These maps were intended for health workers and Doctors without Borders to help them travel around the country and identify, treat and save Ebola patients.

Don’t hesitate to check out our Drupal offer.

Let's have a chat about your projects.

contact