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
To make it format code according to Drupal Coding Standards, I set the following options:
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:
- TRUE, FALSE and NULL must be uppercase, not going to happen according to this issue.
- Prettier wraps lines longer than the
printWidthin a way that makes Drupal CS complain on some array elements.
- 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.