Prettier is a fantastic tool for JavaScript developers, and after reading about its PHP plugin, I decided to give it a try. Since I primarily work with Drupal, I was curious to see how well Prettier would work with it.

Setting up Prettier for PHP

To get started, I created a package.json file in the root of my Drupal project and installed Prettier:

npm init --yes
npm install --save-dev prettier @prettier/plugin-php

The Prettier PHP plugin comes with several options that can be configured in a .prettierrc.json file. To make it format code according to Drupal Coding Standards, I set the following options:

"phpVersion": "8.1",
"tabWidth": 2,
"braceStyle": "1tbs",
"singleQuote": true

Prettier's PHP plugin has excellent documentation for configuring it in various editors and IDEs.

Can it be used with Drupal?

The short answer is no.

The longer answer is that there are some Coding Standards rules that can't be configured according to Drupal:

  1. TRUE, FALSE and NULL must be uppercase, not going to happen according to this issue.
  2. Prettier wraps lines longer than the printWidth in a way that makes Drupal CS complain on some array elements.
  3. Drupal requires an empty line before the closing bracket of a class, which Prettier removes.

While there may be other issues, these are the ones I encountered during my testing. It's a shame, because the "format on save" feature is so nice.

I think it can be a good fit for other PHP projects who follow the PSR-12 standard.