Back in 2011, development of Drupal 8 began. Since then, thousands of web developers around the world have been looking forward to the release of a brand-new version of this well-known content management system. In November 2015, Drupal 8 was released and developers were able to explore its features.

The 8th Drupal version kept and significantly improved all the best features from the previous versions. This user-friendly tool provides new opportunities for data structuring, website customization, and content presentation. Besides that, it opens new mobile development and multilingual accessibility horizons. Powered by Symfony2, it is a great choice for web development.

However, writing excellent code is still immensely difficult, and getting it perfect the first time is almost impossible. You can save your project from most potential issues if you work independently. But if you are working with a large enterprise solution built by many developers over the years, you need to be an expert in Drupal 8 debugging techniques. Take a look at the best ways to improve your code and use this article as your Drupal debugging handbook!

6 Ways to Debug a Drupal Project

Unfortunately, there is no one-size-fits-all bug-proofing plugin for Drupal. Similar to working with other development tools, you need to think and work hard to fix your bugs. Sooner or later, even the most experienced software engineers must detect bugs and fix them promptly. To simplify your work and efforts, we gathered the best techniques for Drupal 8 debugging in one place.

1. Disable the Cache

Caching comes as an enhancement of Drupal 8 functionality. But unfortunately, this method can be inconvenient. Cache in Drupal 8 stores a lot of information, which may complicate the developers’ work. Every modification in the code may cause issues with debugging, so it is recommended to turn the cache off. Keep in mind that if you clear the cache, it becomes possible to visualize the changes and progress. At the same time, you will be able to notice the potential issues and fix them immediately.

Drupal 8 is enriched with an out-of-the-mode debugging feature. You can enable this feature to debug your code.

Add a few lines of code either in the sites/default/service or development.service file.

parameters:

twig.config:

debug: true auto_reload: true

cache: false

The debug: true parameter enables debugging and improves the development process.

All in all, you can simply clear the cache manually every time by going along this path - Configuration->Performance and clicking ‘Clear all caches’.

But it’s really convenient to disable the cache for some time. Here’s how it’s done:

enter the /sites catalog and copypaste example.settings.local.php into /sites/default rename the file to settings.local.php open the file settings.php delete any comments from the next lines: open the file settings.local.php and do the same procedure with the line (if there is no such line in the file, add it): do the same in the settings.local.php: enter /sites, open development.services.yml in /sites, add the following piece of code after the line http.response.debug_cacheability_headers: true: do the rebuild via drush.

That’s it!

2. Connect the Devel Module

The Devel module is a fast and convenient way to start debugging. It consists of 2 submodules:

Kint injects its native library into your project. It allows you to print the variables in special Twig templates which have a user-friendly formatting for the information about objects and arrays provided.

WebProfiler is the submodule that creates a bar with the following data on each page of the web solution: Drupal version; PHP version with modules; Route and controller name; Page load time; Memory usage; Number of queries; Number of blocks used; Number of views, modules and themes; Cache data.



Web Profiler provides access to Symfony components for Drupal 8 developers. With its help, you can the website performance data. Install the Devel module in order to employ the full capabilities of this solution. In order to do that, enter the following in Drush:

$ drush dl devel

$ drush webprofiler

and in Drupal CLI:

$ drupal module: download devel --latest

$ drupal module: install webprofiler

A click on any of the sections will lead you to detailed information regarding the corresponding topic. And each of them will be useful if you need to debug Drupal 8 project.

Devel also features a very useful submodule - Devel Generate, with the help of which Dummy data can be generated. It's commonly used when there's a necessity to view visual components of web pages, but one doesn't have the readymade text or media files to fill the corresponding containers. Devel, basically, provides very helpful content 'fillers'.

If you haven't connected the Devel module yet, enter the following commands into CLI:

$ drush dl devel

$ drush en devel -y

Then, enable the Generate module:

$ drush en devel_generate -y

Now you can try out the module by generating 30 nods with the help of the next command:

$ drush generate-content 30

Devel package also includes several useful functions to help optimize the debugging procedure:

dd() - allows tracking a value of a variable in the file drupal_debug.txt. Here’s an input example:

dd ($ data, $ label = NULL)

dmp() - forces a variable onto a web page in the form of $messages. An input example:

dpm ($ input, $ name = NULL)

kpr() - forces a variable onto a web page header via krumo (can be used if the $messages variables have a restricted view). An input example:

kpr($input, $return = FALSE, $name = NULL)

3. Use the Drupal Console for Debugging

Drupal’s CLI provides many great opportunities, including building themes and modules, interacting with Drupal installations, and of course, code debugging.

There are many ways to enhance the debugging process in Drupal 8, namely:

There are many commands you will find extremely useful when looking for the ways to debug Drupal 8 code.

Drupal check allows you to check the system requirements;

Drupal site status provides information about the versions of the tools used for the current project, like the version of PHP, MySQL, database connection, etc.;

Drupal database: table: debug shows the tables used in the database;

Drupal database: table: debug my_table shows the columns of the defined table;

Drupal config:debug gives you a list of the configurations;

Drupal container: debug displays the services’ IDs;

Drupal route:debug shows the routes;

Drupal database:log:debug provides information about log events;

Drupal site:statistics shows the precise data about enabled and disabled modules, the number of the website visitors, comments, etc.

In addition, there are a lot of other great commands to make debugging much easier.

4. Install XDebug and Use PHPStorm

This debugging instrument can be your handy tool for making sure your code is impeccable. It gives you the opportunity to keep an eye on the program execution, gain access to the content of variables, visualize variables, etc. This is considered to be an advanced debugging tool that you should add to the technology stack of your project.

In order to connect the XDebug PHP debugger, enter the following command in xdebug.ini (or in php.ini, in the case if you are using the 2.3 or earlier version of debugger):

xdebug.max_nesting_level=1000

PHPStorm is an IDE that can help you with debugging, too. It triggers the autocomplete function for hooks. Also, there are a lot of additional PHPStorm plugins for code improvement, such as PHP Annotations, Drupal Symfony Bridge, etc. Remember to enable Symfony integration before using PHPStorm.

5. Display the Errors

One of the Drupal 7 features is that you can set up the list of the issues on the display. You can pick one of the options: no issues to be displayed, errors and warnings only, or all troubles to be seen on the screen. In the new version of Drupal, you have a 4th option: “All Messages with Backtrace Information”. With the help of this option, you can debug the problematic code.

However, you can stay out of using any code structures whatsoever if you want and still make Drupal 8 highlight your errors. Take the following path: /admin/config/development/logging and set the mark in the required ‘Error Messages to Display’ field’s line.

6. Work with Log Messages

There is one more major difference between the 7th and the 8th versions of Drupal. With the help of the database logging module, you can log messages in the DB. The watchdog() function is the essential part of the process. In Drupal 8, this module is present as well. However, it is enhanced and is known as a Drupal 8 logger class. It generates a copy of the logger service and is useful for debugging purposes.

Take a look at how one and the same operation realized in Drupal 7 (screenshot 1) is now realized in Drupal 8 (screenshot 2):

Screenshot 1

Screenshot 2

Final Thoughts

So how can you debug Drupal 8? Reference this list of easy-to-implement techniques to obtain the best results and create excellent projects. Using the right modules, it becomes easier to ensure that the frontend and backend web development are of the highest quality possible. Xdebug and PHPStorm can be your best assistant tools during this process.

If you still have some questions about Drupal 8 development and debugging, just drop us a line! Our years of expertise in web development allow us to answer your questions thoroughly and precisely.