Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.
-
Upload
isabel-copeland -
Category
Documents
-
view
216 -
download
1
Transcript of Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.
![Page 1: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/1.jpg)
Introduction to Module Development
Yi Zhang &Nikki Massaro Kauffman
![Page 2: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/2.jpg)
Modules• Use PHP to
customize Drupal.
• Can access variables used in Drupal Core.
• Can override/enhance core functions through “hooks”.
![Page 3: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/3.jpg)
Use them wisely…
![Page 4: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/4.jpg)
…or you may have conflicts.
![Page 5: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/5.jpg)
Why Module Development?
Content Types
Views Themes Modules
GUI GUI Code Code
Data Presentation Presentation Data
![Page 6: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/6.jpg)
Where Modules Live
sites/all/
modules/contrib/custom/
themes/
![Page 7: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/7.jpg)
Where Modules Live
sites/all/
modules/contrib/custom/
themes/
![Page 8: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/8.jpg)
Where Modules Live
sites/all/
modules/contrib/custom/
themes/
3rd party modules
![Page 9: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/9.jpg)
Where Modules Live
sites/all/
modules/contrib/custom/
themes/DIY modules
![Page 10: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/10.jpg)
Recipe for a Module
Ingredients
.info
.module
optional files
optional folders
![Page 11: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/11.jpg)
Recipe for a Module
Ingredients
.info – module info, files, and
dependencies
.module
optional files
optional folders
![Page 12: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/12.jpg)
Recipe for a Module
Ingredients
.info – module info, files, and
dependencies
.module – function of the module
optional files
optional folders
![Page 13: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/13.jpg)
Recipe for a Module
Ingredients
.info – module info, files, and
dependencies
.module – function of the module
optional files – .install, .inc, .tpl.php,
etc.
optional folders
![Page 14: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/14.jpg)
Recipe for a Module
Ingredients
.info – module info, files, and
dependencies
.module – function of the module
optional files – .inc, .tpl.php, etc.
optional folders – css, images, etc.
![Page 15: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/15.jpg)
Drupal Hooks
• PHP function• Modifies Drupal
behavior• Similar to callback–Like triggered events
• Hook similar to object-oriented–Strict naming
convention
![Page 16: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/16.jpg)
Some Drupal Hooks
• hook_menu• hook_block• hook_footer• hook_schema• hook_help• hook_permission• hook_form
Hook Information: http://api.drupal.org
![Page 17: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/17.jpg)
Hello World!
![Page 18: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/18.jpg)
Test1.info
![Page 19: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/19.jpg)
Test1.module
![Page 20: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/20.jpg)
hook_menu()function test1_menu() { $items = array(); $items['test1'] = array( 'title' => 'PSU Creamery ice cream', 'description' => ’Select your favorite ice cream’, ‘page callback’ => ‘test1_page’, ‘access callback’ => TRUE, ‘type’ => MENU_CALLBACK, );
return $items;}
![Page 21: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/21.jpg)
Test1.module (continue)
![Page 22: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/22.jpg)
![Page 23: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/23.jpg)
Test2.module
![Page 24: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/24.jpg)
hook_help()
function test2_help($path, $arg) { switch ($path) { case 'admin/help#test2':{ $ret_val = '<h3>' . t('About') . '</h3>'; $ret_val = '<p>' . t('This module will show detailed information of a specific ice cream.') . '</p>'; return $ret_val; break; } }}
![Page 25: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/25.jpg)
hook_permission
function test2_permission() { return array( 'administer test2' => array( 'title' => t('Administer test2'), 'description' => t('Perform administrative tasks on test2 functionality'), ), );}
![Page 26: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/26.jpg)
Configuration
![Page 27: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/27.jpg)
Test3.info
![Page 28: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/28.jpg)
PSU Drupal Camp 2013
Test3.module // Admin configuration group. $items['admin/config/drupalcamp'] = array( 'title' => 'drupalcamp', 'description' => 'Administer drupalcamp', 'access arguments' => array('administer drupalcamp'), ); // Admin configuration - Settings. $items['admin/config/drupalcamp/test3/manage'] = array( 'title' => 'test3', 'description' => 'Manage test3 settings and configurations.', 'access arguments' => array('administer test3'), 'page callback' => 'drupal_get_form', 'page arguments’=> array('test3_admin_settings_form'), );
![Page 29: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/29.jpg)
hook_form()function test3_admin_settings_form($node, &$form_state) { $form = array(); $form['overview'] = array( '#markup' => t('This interface allows administrators to manage general test3 Settings'), '#prefix' => '<p>', '#suffix' => '</p>’,); $form['test3_maxnum'] = array( '#title' => t('Max number of cones per customer'), '#type' => 'textfield', '#default_value' => '2', '#required' => TRUE,); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'),); return $form;}
![Page 30: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/30.jpg)
Validation
![Page 31: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/31.jpg)
PSU Drupal Camp 2013
Test4.module
![Page 32: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/32.jpg)
hook_validate()function test4_admin_settings_form_validate($form, &$form_state) {// Regular expression for validating input number. $maxnum_regex = '/^[+-]?\d+$/';
// Shorthand for long array names. $input_num = $form_state['values']['maxnum'];
// Validate maxnum format. if (!preg_match($maxnum_regex, $input_num)) { form_set_error('maxnum', t('Invalid number. Must be an integer.')); }
// Validate maxnum value. if ($input_num <= 0) { form_set_error('maxnum', t('Must input a positive number.')); }}
![Page 33: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/33.jpg)
validated test4 setting submission
function test4_admin_settings_form_submit($form, &$form_state) { // Rebuild the form. $form_state['rebuild'] = TRUE;
// Save test4 setting variables. variable_set('maxnum', $form_state['values']['maxnum']);
// Notify user. drupal_set_message(t('test4 settings saved.'), 'status');}
![Page 34: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/34.jpg)
PSU Drupal Camp 2013
Enable the Module
![Page 35: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/35.jpg)
.install file
• runs when a module is enabled for the first time
• creates database tables and fields. • instructions are included in a
_install() function.
![Page 36: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/36.jpg)
Test5.install
![Page 37: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/37.jpg)
PSU Drupal Camp 2013
hook_install()function test5_install() { // Set default variables. variable_set('maxnum', 2);
// Get localization function for installation as t() may be unavailable. $t = get_t();
// Give user feedback. drupal_set_message($t('test5 variables created.'));}
![Page 38: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/38.jpg)
hook_uninstall()
function test5_uninstall() { // Delete variables. variable_del('maxnum');
$t = get_t();
drupal_set_message($t('test5 variables removed.'));}
![Page 39: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/39.jpg)
PSU Drupal Camp 2013
Test5.module
Remove the validated submission
variable_get('maxnum'),
Return System_settings_form($form);
![Page 40: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d0b5503460f949df362/html5/thumbnails/40.jpg)
Thank you!