Configuring Drupal 8 coding standards verification In Sublime Text 3

In this blog, we will configure Drupal coding standards in Sublime Text 3.  Before proceeding make sure you have Sublime text 3 and composer installed on your machine.

1. Installing and configuring Coder and PHPCS

PHP_CodeSniffer(phpcs) is a library which checks for coding standard violations in PHP files against a set of defined coding standards. Coder, on the other hand, contains sniffs or rules for Drupal coding standards  and other best practices.

Let’s first install coder package using composer. Use the command below in your terminal to install coder. This will automatically download coder and phpcs along with other dependencies.

composer global require drupal/coder

Then after we need to make sure that phpcs command is accessible globally. To do so update system $PATH variable to point to the vendor/bin directory inside composer installation directory.

Next, we have to register the Drupal and DrupalPractice coding standards in phpcs. These standards are provided by the coder package that we have installed earlier. Execute the below command in your terminal.

phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer

If you are using windows machine execute:

phpcs --config-set installed_paths C:/Users/<username>/AppData/Roaming/Composer/vendor/drupal/coder/coder_sniffer

Now phpcs will recognise the Drupal coding standards. Let’s confirm this by executing the command below :

phpcs -i

It will now show Drupal and DrupalPractice along with other available coding standards in phpcs.

2. Configuring Sublime Text

We have successfully configured phpcs and Drupal coding standards in our system. Now we need to configure the Sublime editor to follow the Drupal coding standards.

Open Sublime Text, go to Preferences > Settings and add the below configurations in the newly opened window. These are the basic coding standards recommended by Drupal.

  "rulers": [80],
  "tab_size": 2,
  "translate_tabs_to_spaces": true,
  "use_tab_stops": true,
  "trim_automatic_white_space": true,
  "trim_trailing_white_space_on_save": true,
  "ensure_newline_at_eof_on_save": true,
  "fallback_encoding": "UTF-8",
  "default_line_ending": "unix",
  "shift_tab_unindent": true,
  "word_separators": "./\\()\\'-:,.;<>~!@#%^&*|+=[]{}`~?"

Next, install the below packages in sublime text using Sublime Package control.

  1.  SublimeLinter (A framework for interactive code linting in the Sublime Text editor)
  2.  SublimeLinter-phpcs (Plugin for SublimeLinter provides an interface to phpcs)

If you are not familiar with package installation in sublime text., please follow this link.

3. Adding Drupal coding standards to Sublime Linter

In the last step, we need to configure the Drupal standards in the Sublime Linter. In Sublime editor go to Preferences > Package Settings > SublimeLinter > Settings. In the User settings window add Drupal, DrupalPractice to standards key under phpcs.

"linters": {
  "phpcs": {
      "@disable": false,
      "args": [],
      "excludes": [],
      "standard": "Drupal,DrupalPractice"

That’s it. We have now successfully configured our Sublime editor to follow the Drupal coding standards. To confirm, open any PHP file inside your Drupal project. The linter will identify the coding standard violations and display them nicely.

If it’s not linting your code by default, it can be done manually by right-clicking the file, then clicking SublimeLinter > Lint This View.

Note: The coding standard violations can be fixed automatically by the tool phpcbf which stands for PHP code beautifier. It will be automatically installed when you install coder. To fix the code violations in your file just execute the command below passing in the correct file path:

phpcbf --standard=Drupal,DrupalPractice <php-file-path>

About The Author

Malay Nayak

Acquia Certified Drupal8 Developer

Leave a Reply