Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Coding Standards
(Drupal Best Practices)
Module Folder Structure and Files:
● Module name should which reflects our functionality
○ Prefer to use prefix as Site na...
Naming Conventional
1. Variables Name
a. String Variables
b. Number Variables
c. Array
d. Object
2. Function Name
3. Const...
Control and Conditional Statements
● White Space between control statement and
opening parenthesis.
● Curly braces- Increa...
Looping Statements
Function Declaration and Definition
● Ensure your function return
some value.
● Include Description of the
function in com...
Class and Object
● Class name should starts with
Capital.
● Try to use Namespace for class.
● Leave an empty line between ...
INFO File
● Drupal uses “.info” files to
store metadata about
themes and modules.
● Optionally able to add
○ Files
○ Depen...
Module File
Install File
● hook_schema() take care of create and
deletion of tables.
● hook_update_N() will works on update.php
callba...
INC Files
● .inc files used for Splitting module code into
files.
● .inc file is only loaded for the specific path.
● The ...
JS & CSS Files
● These JS and CSS files are only used for specific features.
● Including Files
○ In Info File
scripts[] = ...
Tools
● PAReview.sh online tool to review the code
○ https://pareview.sh/
○ https://www.drupal.org/project/pareviewsh
● PH...
Do and Don’t
● Don’t write your PHP code inside Block , Views, Panels,. Instead of write in
your module and call the funct...
Cond,.
● Use LANGUAGE_NONE instead of und.
● Power of “entity_metadata_wrapper()”
$entityObj = entity_metadata_wrapper(ENT...
Cond,.
Hacking contrib Module
● Applying Patches
● Adding our custom hacks.
● Create your “Custom Hook Alter” for changing...
Thanks All
Upcoming SlideShare
Loading in …5
×

Drupal Coding Standards - do and don't

1,017 views

Published on

These slides will explain about best practices for Drupal code writing.

Published in: Education
  • Be the first to comment

  • Be the first to like this

Drupal Coding Standards - do and don't

  1. 1. Coding Standards (Drupal Best Practices)
  2. 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. 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. 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".
  5. 5. Looping Statements
  6. 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. 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. 8. INFO File ● Drupal uses “.info” files to store metadata about themes and modules. ● Optionally able to add ○ Files ○ Dependencies ○ Script ○ CSS
  9. 9. Module File
  10. 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. 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. 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. 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. 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. 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. 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) { -------- -------- }
  17. 17. Thanks All

×