Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Drupal coding Standard and Best Practice

By editor , 29 June, 2017
Question

How to use phpcs and phpcbf to check and correct drupal 8 coding Standard ?


Create a Drupal 8 module is a good idea, but develop as Drupal is BEST.

The famous tools PhpCS and PhpCBF allow you to check and correct (some) drupal coding standards and best practice errors.

Install Drupal coder.

Step 1. Download drupal coder via drush (drush dl coder) or from https://www.drupal.org/project/coder and extract into a folder out side of the drupal project.
Here, for the example : /home/myhome/coder

Step 2. Download / Install additional packages using composer. (How to install composer)
Example:
cd /home/myhome/coder
composer update

Step 3. Configure envirenement.
Example:
export PATH="$PATH:/home/myhome/coder/vendor/bin"
set PATH $PATH /home/myhome/coder/vendor/bin
phpcs --config-set installed_paths /home/myhome/coder/coder_sniffer
phpcbf --config-set installed_paths /home/myhome/coder/coder_sniffer


Use PhpCS anf PhpCBF

-> Go to your custom module directory. Example : /var/www/drupal8/modules/custom/mymodule
You can use phpcs using absolute path of the module. But I got some errors when using phpcbf.
phpcs --standard=Drupal /var/www/drupal8/modules/custom/mymodule

Example of the Errors :
Array
(
[0] => Ignoring potentially dangerous file name /var/www/drupal8/modules/custom/mymodule.....
[1] => can't find file to patch at input line 3

Check drupal coding Standard errors

phpcs --standard=Drupal .
OR
phpcs --standard=Drupal /var/www/drupal8/modules/custom/mymodule

Correst some drupal coding Standard errors (Merked as [X])

phpcbf --standard=Drupal .
OR
# This may not work : To test
phpcbf --standard=Drupal /var/www/drupal8/modules/custom/mymodule

Check drupal Best Practice errors

phpcs --standard=DrupalPractice .
OR
phpcs --standard=DrupalPractice /var/www/drupal8/modules/custom/mymodule

Tags

  • Drupal 8
  • Code
  • Debug
  • Module
Install and Update Composer
Configure PhpStorm for Drupal coding standards

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer