Introduction to Module

architectgroundhogInternet και Εφαρμογές Web

4 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

72 εμφανίσεις

Introduction to
Module
Development

Yi Zhang &

Nikki Massaro Kauffman

Modules


Use PHP to
customize Drupal.


Can access
variables used in
Drupal Core.


Can
override/enhanc
e 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
L
ive

s
ites/

all/

m
odules/

contrib
/

custom/

t
hemes/

Where Modules
L
ive

s
ites/

all/

m
odules/

contrib
/

custom/

t
hemes/

Where Modules
L
ive

s
ites/

all/

m
odules/

contrib
/

custom/

t
hemes/

Where Modules
L
ive

s
ites/

all/

m
odules/

contrib
/

custom/

t
hemes/

DIY modules

Recipe for a Module

Recipe for a Module

Recipe for a Module

Recipe for a Module

Recipe for a Module

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

h
ook_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

h
ook_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;



}


}

}


h
ook_permission

function test2_permission() {


return array(



'
administer test2' => array(



'title' => t('Administer test2'),



'description' => t('Perform administrative
tasks on test2 functionality'),



),


);

}


Configuration

Test3.info

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'),


);

h
ook_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

Test4.module

PSU Drupal Camp 2013

h
ook_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');

}


Enable the Module

PSU Drupal Camp 2013

.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

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

}


hook_uninstall
()

function test5_uninstall() {


// Delete variables.


variable_del
('
maxnum
');



$t =
get_t
()
;



drupal_set_message
($t('test5 variables
removed.'));

}


Test5.module

PSU Drupal Camp 2013

Remove the
validated
submission

variable_get
('
maxnum
')
,

Return
System_settings_form
($form);

Thank you!