school of information university of michigan si.umich.edu drupal: configuration and customization...
TRANSCRIPT
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Drupal: Configuration and Customization
Week 4: Installation, Module Development
January 25, 2007
Professor Paul Resnick
(with help from Michael Hess, as usual)
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Today’s Learning Objectives At the end of this week, students should
be able to:– Debug minor installation and upgrade
problems– Explain the major differences between
Drupal 4.7 and 5.0– Explore the code behind a contributed
module– Create small custom modules
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Drupal 4.7 Installation
If you don’t have an auto-installer (e.g., fantastico)… Download and extract the tar.gz file
– Place in a web-accessible folder on your server• E.g., htdocs
Edit sites/default/settings.php file– Directions in the comments in this file– location of your database – $base_url path
Import the database– Get directions from your ISP– E.g., mysql dbname –u dbuser –ppassword < drupaldb_file
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Drupal 5.0 Installation
If you don’t have an auto-installer (e.g., fantastico)…
Download and extract the tar.gz file– Place in a web-accessible folder on your server
• E.g., htdocs
Change perms on /sites/default/settings.php to 666– Owner, group, and other all read/write privileges
Access your Drupal install via web and follow the directions
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Upgrading 4.7 to 5
See Lullabot screencast Note: you can’t do this on cms.si.umich
– We have a multi-site installation
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Drupal Security
If you’re the site maintainer…– Join the security mailing list
• Login to drupal.org• My account >> my newsletters
– When you get a security alert for a module you’re using
• Upgrade as soon as you can
– When you get a security alert for Drupal core• Follow the suggested action ASAP
Do NOT install modules not found on Drupal.org– No security alerting system
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Drupal 5.0 Preview
Key changes– Installation is easier– Administration interface changes– Themes
• New: Garland• Some color changes without changing code
– CCK is part of core– Installation Profiles
See Lullabot screencast
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Some PHP Basics
Semi-colon at end of statements { } enclose collection of statements Variables If-statements Functions Arrays For loops
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
PHP Variables
Variables begin with $ Set using =
$x = 23; Local unless you specify global
global $user;
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
PHP If/Else and Switch/Caseif ($op == "list") {
stm1; stm2; …}else {}
switch ($section) { case “a”: stmt; break; case “b”: stmt; }
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
PHP Function Definition
function onthisdate_help($section='') { $output = ''; switch ($section) { case "admin/modules#description": $output = t("Displays links to nodes created on this date");
break; } return $output;} // function onthisdate_help
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
PHP Arrays
Example: an array whose first/only item is an array
$items = array(); $items[] = array('path' => 'onthisdate', 'title' => t('on this date'), 'callback' => 'onthisdate_all', 'access' => user_access('access
content'), 'type' => MENU_CALLBACK);
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
PHP Foreach Loops
<?php$arr = array(1, 2, 3, 4);foreach ($arr as &$value) { $value = $value * 2;}// $arr is now array(2, 4, 6, 8)?>
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Extensibility Concepts: Defining Add-ons
Adding new names– Only problem is name collisions
• Variables in programs– local and global• XML markup tags– XML Namespaces
Defining meaning of named items– Set variable values– Define functions
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Extensibility Concepts: Integrating Add-ons
Hooks/callbacks– Base calls the add-on
Function libraries– Add-on calls the base
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
What Programmers Need to Know About Callbacks
How to register the callback so it will be invoked
When the callback will be invoked Parameters passed and returned
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Drupal Callbacks (Hooks)
Registering a hook– Define a function with the right name
• <modulename>_hookname
– No separate registration process; will automatically be invoked
When hook will be invoked– Check documentation; look at source code
Parameters passed and returned– api.drupal.org
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Drupal Naming Conventions One big namespace Don’t define a module that has same
name as any other module All functions in module begin with prefix
– <module-name>_• For functions that may be invoked from other
files
– _<module-name>_• For private functions
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
What Programmers Need to Know About Library Functions Parameters passed and returned
– See api.drupal.org Any side effects of the function
– E.g., saving a node in the database
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Some Useful Hooks
Documentation hook_access hook_block hook_cron hook_menu hook_settings (4.7; use hook_menu in 5.0) hook_nodeapi
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Cron.php
When cron.php is executed– Executes the cron hooks for all contributed
modules Set up a UNIX cron job to cause cron.php to
be executed periodically– Done for you on cms.si
• Once per hour
Poormanscron module if you can’t run a cron job on your installation– Checks on each user access whether it’s time to
run cron.php
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Some Useful Drupal library functions Documentation at http://api.drupal.org/ Theme related
– l(), t() Node functions
– node_load(), node_save() Database functions
– Db_query() et al Forms API Access control
– User_access()
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Drupal Licensing
Drupal Core is Distributed under GPL– You can modify and redistribute it– You can’t prevent others from doing so– You don’t have to distribute your modifications
If you distribute modifications, must be under GPL– Wiggle room about whether a consultant who
gives the module only to a client who paid for its production is distributing it
• Consult an attorney
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Module Development
Tutorial OnThisDate– http://drupal.org/node/22801
A Sign-me-up custom feature for AARecon
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Devel Module
Menu block– Clear the cache– View variable values
PHP block to execute some code Display SQL queries used in building a
page Object structure display for current node
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
OnThisDate Tutorial
http://drupal.org/node/22801 Final version of 4.7 code (rename it to
OnThisDate.module)
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Making module information visible hook_help
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Permission Actions
hook_perm
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Create Block(s) in a Module hook_block
– List op– View op
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Create Module Settings
hook_settings– Add a new variable that can be set through
administer >> settings >> this_module
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Create a Page
Define a function that outputs a page Give it a URL path and/or add it to a
menu
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
SignMeUp Custom Module Adds a link to sign up as the closer For production we’ll do better
– A custom field type using CCK’s API• Copy and edit userreference
– Button in each of the fields where user can sign up
• Access controls; don’t allow double sign-up, etc.
– Display sign-ups with photos
SCHOOL OF INFORMATION
UNIVERSITY OF MICHIGANsi.umich.edu
Exercises
Which hook would you use? Write or modify a module