Upload
isabel-copeland
View
217
Download
1
Tags:
Embed Size (px)
Citation preview
Introduction to Module Development
Yi Zhang &Nikki Massaro Kauffman
Modules• Use PHP to
customize Drupal.
• Can access variables used in Drupal Core.
• Can override/enhance core functions through “hooks”.
Use them wisely…
…or you may have conflicts.
Why Module Development?
Content Types
Views Themes Modules
GUI GUI Code Code
Data Presentation Presentation Data
Where Modules Live
sites/all/
modules/contrib/custom/
themes/
Where Modules Live
sites/all/
modules/contrib/custom/
themes/
Where Modules Live
sites/all/
modules/contrib/custom/
themes/
3rd party modules
Where Modules Live
sites/all/
modules/contrib/custom/
themes/DIY modules
Recipe for a Module
Ingredients
.info
.module
optional files
optional folders
Recipe for a Module
Ingredients
.info – module info, files, and
dependencies
.module
optional files
optional folders
Recipe for a Module
Ingredients
.info – module info, files, and
dependencies
.module – function of the module
optional files
optional folders
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
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.
Drupal Hooks
• PHP function• Modifies Drupal
behavior• Similar to callback–Like triggered events
• Hook similar to object-oriented–Strict naming
convention
Some Drupal Hooks
• hook_menu• hook_block• hook_footer• hook_schema• hook_help• hook_permission• hook_form
Hook Information: http://api.drupal.org
Hello World!
Test1.info
Test1.module
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;}
Test1.module (continue)
Test2.module
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; } }}
hook_permission
function test2_permission() { return array( 'administer test2' => array( 'title' => t('Administer test2'), 'description' => t('Perform administrative tasks on test2 functionality'), ), );}
Configuration
Test3.info
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'), );
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;}
Validation
PSU Drupal Camp 2013
Test4.module
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.')); }}
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');}
PSU Drupal Camp 2013
Enable the Module
.install file
• runs when a module is enabled for the first time
• creates database tables and fields. • instructions are included in a
_install() function.
Test5.install
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.'));}
hook_uninstall()
function test5_uninstall() { // Delete variables. variable_del('maxnum');
$t = get_t();
drupal_set_message($t('test5 variables removed.'));}
PSU Drupal Camp 2013
Test5.module
Remove the validated submission
variable_get('maxnum'),
Return System_settings_form($form);
Thank you!