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) ;
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);