SlideShare a Scribd company logo
Coding Standards
(Drupal Best Practices)
Module Folder Structure and Files:
● Module name should which reflects our functionality
○ Prefer to use prefix as Site name. (Ex: missiontix_custom_price)
○ Extending features of any contrib module (Ex: missiontix_views_ext)
MY_MODULE
-- MY_MODULE.info
-- MY_MODULE.module
-- MY_MODULE.install
- js
-- MY_MODULE.js
- css
-- MY_MODULE.css
- src
-- EXT_LIBRARY.php
-- ...
● Files:
○ Each file ends with empty at end
Naming Conventional
1. Variables Name
a. String Variables
b. Number Variables
c. Array
d. Object
2. Function Name
3. Constant Name
4. Class Name
Control and Conditional Statements
● White Space between control statement and
opening parenthesis.
● Curly braces- Increases readability.
● Closing brace should be on new line.
● Space before and after the operator.
● No Space between Increment/Decrement
operators.
● Don't use "else if".
Looping Statements
Function Declaration and Definition
● Ensure your function return
some value.
● Include Description of the
function in command.
● Command contains the
Arguments details and return
type details.
● Specifying the implementing
HOOK details in function
descriptions.
Class and Object
● Class name should starts with
Capital.
● Try to use Namespace for class.
● Leave an empty line between end
of method and end of class
definition.
● Use “$this” as object to access the
same class Methods and variables.
INFO File
● Drupal uses “.info” files to
store metadata about
themes and modules.
● Optionally able to add
○ Files
○ Dependencies
○ Script
○ CSS
Module File
Install File
● hook_schema() take care of create and
deletion of tables.
● hook_update_N() will works on update.php
callback.
● Delete the variables(variable_del()) and other
table entries in hook_uninstall().
● External libraries related implementations are
done in hook_requirements().
● Install is similar in both Drupal 7 and Drupal 8.
INC Files
● .inc files used for Splitting module code into
files.
● .inc file is only loaded for the specific path.
● The function module_load_include('inc',
MODULE_NAME, MODULE_NAME.INCLUDE)
help to load INC files
Syntax: module_load_include('inc', MODULE_NAME,
MODULE_NAME.INCLUDE)
JS & CSS Files
● These JS and CSS files are only used for specific features.
● Including Files
○ In Info File
scripts[] = MODULE.js
stylesheets[all][] = MODULE.css
○ Added using function
■ drupal_add_js(PATH) ;
■ drupal_add_css(PATH) ;
Tools
● PAReview.sh online tool to review the code
○ https://pareview.sh/
○ https://www.drupal.org/project/pareviewsh
● PHP CodeSniffer
○ Drupa Coder module (https://www.drupal.org/project/coder)
phpcs --standard=Drupal /var/www/html/annotation_store/annotation_store.info.yml
phpcs --standard=Drupal /var/www/html/annotation_store
Do and Don’t
● Don’t write your PHP code inside Block , Views, Panels,. Instead of write in
your module and call the function.
● Usage of “function_exists()”.
● Use isset() and empty().
● Function module_load_include().
● variable_get(), variable_set() and variable_del().
● In alters don’t unset() reference variables just hide().
● Custom Query parameter instead of URL parameter.
Cond,.
● Use LANGUAGE_NONE instead of und.
● Power of “entity_metadata_wrapper()”
$entityObj = entity_metadata_wrapper(ENTITY_TYPE, ENTITY_ID);
// Print values
$value_1 = $entityObj->FIELD_NAME_1->value();
print_r($value_1);
$value_2 = $entityObj->FIELD_NAME_2->value();
print_r($value_1);
//Update values
$entityObj->FIELD_NAME_1->set(UPDATE_VALUE_1);
$entityObj->FIELD_NAME_2->set(UPDATE_VALUE_2);
Cond,.
Hacking contrib Module
● Applying Patches
● Adding our custom hacks.
● Create your “Custom Hook Alter” for changing values.
○ Drupal 7: drupal_alter('MYMODULE_FUNCTION_NAME', $param1, $param2);
○ Drupal 8: Drupal::moduleHandler()->alter('MYMODULE_FUNCTION_NAME', $param1, $param2);
function MYMODULE_FUNCTION_NAME_alter(&$param1, &$param2) {
--------
--------
}
Thanks All

More Related Content

PPTX
Structuring and packaging your python project
PPTX
Node js Lunch and learn: Session 2
PDF
Bio2RDF - Make the most of Virtuoso Open Source
PPTX
Cake (C# Make)
PDF
Glusterfs session #2 1 layer above disk filesystems
PDF
Drupal and security - Advice for Site Builders and Coders
PPTX
Becoming A Drupal Master Builder
PDF
Why Drupal is Rockstar?
Structuring and packaging your python project
Node js Lunch and learn: Session 2
Bio2RDF - Make the most of Virtuoso Open Source
Cake (C# Make)
Glusterfs session #2 1 layer above disk filesystems
Drupal and security - Advice for Site Builders and Coders
Becoming A Drupal Master Builder
Why Drupal is Rockstar?

Similar to Drupal Coding Standards - do and don't (20)

PPT
Drupal Modules
ODP
Clean code and refactoring
PPTX
Drupal coding standards and best practices
PPTX
Drupal Camp Porto - Developing with Drupal: First Steps
PPTX
Creating Drupal A Module
PDF
Intro to Drupal Module Developement
PPT
Drupal - Introduction to Drupal Creating Modules
DOCX
13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overview
PDF
Drupal 8: A story of growing up and getting off the island
ODP
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
PDF
Staging Drupal 8 31 09 1 3
PDF
Drupal 8: Most common beginner mistakes
PPTX
Building API's
PPTX
Drupal module development
PDF
Modernize Your Drupal Development
ODP
Drupal Best Practices
PDF
Drupal 8 - Core and API Changes
ODP
Drupal 6 - podstawy - www.openbit.pl
KEY
Modules Building Presentation
Drupal Modules
Clean code and refactoring
Drupal coding standards and best practices
Drupal Camp Porto - Developing with Drupal: First Steps
Creating Drupal A Module
Intro to Drupal Module Developement
Drupal - Introduction to Drupal Creating Modules
13th Sep - Drupal Global Training Day by TCS - Drupal core advanced overview
Drupal 8: A story of growing up and getting off the island
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Staging Drupal 8 31 09 1 3
Drupal 8: Most common beginner mistakes
Building API's
Drupal module development
Modernize Your Drupal Development
Drupal Best Practices
Drupal 8 - Core and API Changes
Drupal 6 - podstawy - www.openbit.pl
Modules Building Presentation
Ad

More from Arunkumar Kupppuswamy (6)

PDF
Drupal Development Tools.pdf
PPTX
Build website using Open Source Technology
PPTX
Drupal 8 Performance & SEO optimizaion
PPTX
D8 search api
PPTX
Object Oriented Programming - Basic Concepts
PPTX
Drupal 8 : Introduction to Big Pipe
Drupal Development Tools.pdf
Build website using Open Source Technology
Drupal 8 Performance & SEO optimizaion
D8 search api
Object Oriented Programming - Basic Concepts
Drupal 8 : Introduction to Big Pipe
Ad

Recently uploaded (20)

PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Classroom Observation Tools for Teachers
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
01-Introduction-to-Information-Management.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Final Presentation General Medicine 03-08-2024.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 4: Burden of Disease Tutorial Slides S2 2025
GDM (1) (1).pptx small presentation for students
Yogi Goddess Pres Conference Studio Updates
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Classroom Observation Tools for Teachers
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Complications of Minimal Access Surgery at WLH
Orientation - ARALprogram of Deped to the Parents.pptx
A systematic review of self-coping strategies used by university students to ...
01-Introduction-to-Information-Management.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE

Drupal Coding Standards - do and don't

  • 2. Module Folder Structure and Files: ● Module name should which reflects our functionality ○ Prefer to use prefix as Site name. (Ex: missiontix_custom_price) ○ Extending features of any contrib module (Ex: missiontix_views_ext) MY_MODULE -- MY_MODULE.info -- MY_MODULE.module -- MY_MODULE.install - js -- MY_MODULE.js - css -- MY_MODULE.css - src -- EXT_LIBRARY.php -- ... ● Files: ○ Each file ends with empty at end
  • 3. Naming Conventional 1. Variables Name a. String Variables b. Number Variables c. Array d. Object 2. Function Name 3. Constant Name 4. Class Name
  • 4. Control and Conditional Statements ● White Space between control statement and opening parenthesis. ● Curly braces- Increases readability. ● Closing brace should be on new line. ● Space before and after the operator. ● No Space between Increment/Decrement operators. ● Don't use "else if".
  • 6. Function Declaration and Definition ● Ensure your function return some value. ● Include Description of the function in command. ● Command contains the Arguments details and return type details. ● Specifying the implementing HOOK details in function descriptions.
  • 7. Class and Object ● Class name should starts with Capital. ● Try to use Namespace for class. ● Leave an empty line between end of method and end of class definition. ● Use “$this” as object to access the same class Methods and variables.
  • 8. INFO File ● Drupal uses “.info” files to store metadata about themes and modules. ● Optionally able to add ○ Files ○ Dependencies ○ Script ○ CSS
  • 10. Install File ● hook_schema() take care of create and deletion of tables. ● hook_update_N() will works on update.php callback. ● Delete the variables(variable_del()) and other table entries in hook_uninstall(). ● External libraries related implementations are done in hook_requirements(). ● Install is similar in both Drupal 7 and Drupal 8.
  • 11. INC Files ● .inc files used for Splitting module code into files. ● .inc file is only loaded for the specific path. ● The function module_load_include('inc', MODULE_NAME, MODULE_NAME.INCLUDE) help to load INC files Syntax: module_load_include('inc', MODULE_NAME, MODULE_NAME.INCLUDE)
  • 12. JS & CSS Files ● These JS and CSS files are only used for specific features. ● Including Files ○ In Info File scripts[] = MODULE.js stylesheets[all][] = MODULE.css ○ Added using function ■ drupal_add_js(PATH) ; ■ drupal_add_css(PATH) ;
  • 13. Tools ● PAReview.sh online tool to review the code ○ https://pareview.sh/ ○ https://www.drupal.org/project/pareviewsh ● PHP CodeSniffer ○ Drupa Coder module (https://www.drupal.org/project/coder) phpcs --standard=Drupal /var/www/html/annotation_store/annotation_store.info.yml phpcs --standard=Drupal /var/www/html/annotation_store
  • 14. Do and Don’t ● Don’t write your PHP code inside Block , Views, Panels,. Instead of write in your module and call the function. ● Usage of “function_exists()”. ● Use isset() and empty(). ● Function module_load_include(). ● variable_get(), variable_set() and variable_del(). ● In alters don’t unset() reference variables just hide(). ● Custom Query parameter instead of URL parameter.
  • 15. Cond,. ● Use LANGUAGE_NONE instead of und. ● Power of “entity_metadata_wrapper()” $entityObj = entity_metadata_wrapper(ENTITY_TYPE, ENTITY_ID); // Print values $value_1 = $entityObj->FIELD_NAME_1->value(); print_r($value_1); $value_2 = $entityObj->FIELD_NAME_2->value(); print_r($value_1); //Update values $entityObj->FIELD_NAME_1->set(UPDATE_VALUE_1); $entityObj->FIELD_NAME_2->set(UPDATE_VALUE_2);
  • 16. Cond,. Hacking contrib Module ● Applying Patches ● Adding our custom hacks. ● Create your “Custom Hook Alter” for changing values. ○ Drupal 7: drupal_alter('MYMODULE_FUNCTION_NAME', $param1, $param2); ○ Drupal 8: Drupal::moduleHandler()->alter('MYMODULE_FUNCTION_NAME', $param1, $param2); function MYMODULE_FUNCTION_NAME_alter(&$param1, &$param2) { -------- -------- }