40
Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Embed Size (px)

Citation preview

Page 1: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Introduction to Module Development

Yi Zhang &Nikki Massaro Kauffman

Page 2: 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”.

Page 3: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Use them wisely…

Page 4: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

…or you may have conflicts.

Page 5: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

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

Where Modules Live

sites/all/

modules/contrib/custom/

themes/

Page 7: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Where Modules Live

sites/all/

modules/contrib/custom/

themes/

Page 8: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Where Modules Live

sites/all/

modules/contrib/custom/

themes/

3rd party modules

Page 9: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Where Modules Live

sites/all/

modules/contrib/custom/

themes/DIY modules

Page 10: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Recipe for a Module

Ingredients

.info

.module

optional files

optional folders

Page 11: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

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

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

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

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

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

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

Hello World!

Page 18: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Test1.info

Page 19: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Test1.module

Page 20: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

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

Test1.module (continue)

Page 22: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman
Page 23: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Test2.module

Page 24: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

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

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

Configuration

Page 27: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

Test3.info

Page 28: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

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

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

Validation

Page 31: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

PSU Drupal Camp 2013

Test4.module

Page 32: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

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

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

PSU Drupal Camp 2013

Enable the Module

Page 35: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

.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

Test5.install

Page 37: Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

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

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

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

Thank you!