Showing posts with label Drupal 9. Show all posts
Showing posts with label Drupal 9. Show all posts

Sunday, November 28, 2021

Drupal Coding standard (Drupal and DrupalPractice) Setup

 Hello Drupalers,

Hope all are good and healthy.

Today we are going to discuss about how we can keep our code according the Drupal coding standard. While we write any code we keep logics according the functionality but its also important to write code according the standard.

Now Question is how we can check our code is according to standard or not. So in Drupal we usually check according the Drupal and DrupalPractice Coding Standard.

First we have to install and setup phpcs and phcbf in our local repo.


 composer create-project drupal/recommended-project drupal_contrib  
 composer require drupal/coder dealerdirect/phpcodesniffer-composer-installer  
 vendor/bin/phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer/  
 vendor/bin/phpcs -i  

 

we have to run all phpcs commands from vendor/bin/phpcs because we are setting up standards with local repo.

after setup Drupal and DrupalPractice standard install need to check the installed version.
To check installed version run 

The installed coding standards are PSR2, Squiz, PSR1, Zend, MySource, PEAR, PSR12, Drupal, DrupalPractice and VariableAnalysis


That's it if you find the Drupal and DrupalPractice in installed version you can use the to check your code accordingly.


Commands to check code according the Drupal Standard.

 vendor/bin/phpcs --standard=Drupal web/modules/contrib/<Module_name>  
 vendor/bin/phpcbf --standard=Drupal web/modules/contrib/<Module_name>  
 vendor/bin/phpcs --standard=DrupalPractice web/modules/contrib/<Module_name>  

Friday, March 20, 2020

Check deprecation and Make your project Drupal 9 compatible


Hello Drupalers,

As we know Drupal 9 will be release at 3rd of june 2020.

Drupal 9 = Drupal 8 - remove deprecation + increase dependency package


you are here because you also want to know ->

  • How we can make our project Drupal 9 compatible ? 
  • How we can check deprecated code ?


Drupal-check :- We can check all deprecated method using the "drupal-check".


Drupal check will provide you a result based on your project which deprecated method you used in your code and in the place of that code what should we should.


Install Drupal -check : - Download package using below command

 curl -O -L https://github.com/mglaman/drupal-check/releases/latest/download/drupal-check.phar  
 mv drupal-check.phar /usr/local/bin/drupal-check  
 chmod +x /usr/local/bin/drupal-check  


Composer :-
You can install this in your project using Composer as a development dependency like so:

 composer require mglaman/drupal-check --dev  


You can also install this globally using Composer like so:


 composer global require mglaman/drupal-check  


 Check Deprecation :- Now your system is ready to check any Drupal check with deprecation then you can make the project compatible with Drupal 9

If you installed drupal-check locally, open your terminal and run below command.

 lenovo@lenovo-ThinkPad-L450:/var/www/html/drupal_directory $ vendor/bin/drupal-checck web/modules/contrib/module_name  



If there is any error you will be able to see result like :-


  21   Call to deprecated function db_select():                  
      in drupal:8.0.0 and is removed from drupal:9.0.0. Instead, get       
      a database connection injected into your service from the container and   
      call select() on it. For example,                      
  ------ -------------------------------------------------------------------------  
  [ERROR] Found 1 errors  


Now you can remove deprecated code and make its Drupal 9 compatible.



How Drupal will know your project Drupal 9 compatible ?

Here is a way to let know Drupal.org that our module is Compatible with Drupal 8 and Drupal 9

 core_version_requirement: ^8 || ^9  


Add this line in your project info file. If want to know more about core_version_requirement see core_version_requirement



I hope guys you like this, if you have any suggestion and correcction feel free to comment here. Thanks 😊