Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman.

Similar presentations


Presentation on theme: "Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman."— Presentation transcript:

1 Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman

2 Modules Use PHP to customize Drupal. Can access variables used in Drupal Core. Can override/enhanc e core functions through “hooks”.

3 Use them wisely…

4 …or you may have conflicts.

5 Why Module Development? Content Types ViewsThemesModules GUI Code DataPresentation Data

6 Where Modules Live sites/ all/ modules/ contrib/ custom/ themes/

7 Where Modules Live sites/ all/ modules/ contrib/ custom/ themes/

8 Where Modules Live sites/ all/ modules/ contrib/ custom/ themes/ 3 rd party modules

9 Where Modules Live sites/ all/ modules/ contrib/ custom/ themes/ DIY modules

10 Recipe for a Module Ingredients.info.module optional files optional folders

11 Recipe for a Module Ingredients.info – module info, files, and dependencies.module optional files optional folders

12 Recipe for a Module Ingredients.info – module info, files, and dependencies.module – function of the module optional files optional folders

13 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

14 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.

15 Drupal Hooks PHP function Modifies Drupal behavior Similar to callback – Like triggered events Hook similar to object- oriented – Strict naming convention

16 Some Drupal Hooks hook_menu hook_block hook_footer hook_schema hook_help hook_permission hook_form Hook Information: http://api.drupal.org

17 Hello World!

18 Test1.info

19 Test1.module

20 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; }

21 Test1.module (continue)

22

23 Test2.module

24 hook_help() function test2_help($path, $arg) { switch ($path) { case 'admin/help#test2':{ $ret_val = ' '. t('About'). ' '; $ret_val = ' '. t('This module will show detailed information of a specific ice cream.'). ' '; return $ret_val; break; }

25 hook_permission function test2_permission() { return array( 'administer test2' => array( 'title' => t('Administer test2'), 'description' => t('Perform administrative tasks on test2 functionality'), ), ); }

26 Configuration

27 Test3.info

28 Test3.module PSU Drupal Camp 2013 // 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'), );

29 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' => ' ', '#suffix' => ' ’,); $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; }

30 Validation

31 Test4.module PSU Drupal Camp 2013

32 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.')); }}

33 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'); }

34 Enable the Module PSU Drupal Camp 2013

35 .install file runs when a module is enabled for the first time creates database tables and fields. instructions are included in a _install() function.

36 Test5.install

37 hook_install() PSU Drupal Camp 2013 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.')); }

38 hook_uninstall() function test5_uninstall() { // Delete variables. variable_del('maxnum'); $t = get_t(); drupal_set_message($t('test5 variables removed.')); }

39 Test5.module PSU Drupal Camp 2013 Remove the validated submission variable_get('maxnum'), Return System_settings_form($form);

40 Thank you!


Download ppt "Introduction to Module Development Yi Zhang & Nikki Massaro Kauffman."

Similar presentations


Ads by Google