How does Drupal Work?
-
Upload
colette-barton -
Category
Documents
-
view
59 -
download
1
description
Transcript of How does Drupal Work?
How does Drupal Work?How does Drupal Work?
Information Systems 337Information Systems 337
Prof. Harry PlantingaProf. Harry Plantinga
Drupal ModulesDrupal Modules
Defining a Module: guess how?Defining a Module: guess how? Create a directory, e.g. Create a directory, e.g.
/sites/all/modules/annotate/sites/all/modules/annotate Create a file Create a file annotate.infoannotate.info::
name = Annotate name = Annotate
description = description = Allows users to annotate nodes. Allows users to annotate nodes.
package = Example package = Example
core = "6.x" core = "6.x"
HooksHooks
Module system based on the concept of Module system based on the concept of hookshooks
Hook:Hook: a PHP function named foo_bar()a PHP function named foo_bar() foo: name of the modulefoo: name of the module bar: name of the hookbar: name of the hook
Extending Drupal:Extending Drupal: a module extends Drupal by implementing a module extends Drupal by implementing
hookshooks
HooksHooks
An event happens, e.g. a user logs into the An event happens, e.g. a user logs into the site.site.
Drupal fires the "user hook". Drupal fires the "user hook".
That means each module's user hook function That means each module's user hook function will be called:will be called: comment_user()comment_user() node_user()node_user() locale_user()locale_user() myCustomModule_user()myCustomModule_user()
Documented on Documented on Drupal.org
ExamplesExamples
Example: hook_access($op, $node, $account)Example: hook_access($op, $node, $account)
this hook allows modules to limit access to the this hook allows modules to limit access to the node types they definenode types they define
$op: create, delete update, view$op: create, delete update, view
returns TRUE, FALSE, or NULL (don't override returns TRUE, FALSE, or NULL (don't override settings in the node_access tablesettings in the node_access table
to override in "example" module, you would to override in "example" module, you would implement example_access()implement example_access()
Add a Menu EntryAdd a Menu Entry
/**/** * Implementation of hook_menu()* Implementation of hook_menu() */ */ function annotate_menu() {function annotate_menu() { $items['admin/settings/annotate'] = array($items['admin/settings/annotate'] = array( 'title' => 'Annotation settings','title' => 'Annotation settings', 'description' => 'Change how annotations behave','description' => 'Change how annotations behave', 'page callback' => 'drupal_get_form','page callback' => 'drupal_get_form', 'page arguments' => array('annotate_admin_settings'),'page arguments' => array('annotate_admin_settings'), 'access arguments' => array('administer site configuration'),'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,'type' => MENU_NORMAL_ITEM, ); ); return $items; return $items; } }
Define Settings FormDefine Settings Form
/**/** * Define the settings form.* Define the settings form. */ */ function annotate_admin_settings() {function annotate_admin_settings() { $form['annotate_node_types'] = array($form['annotate_node_types'] = array( '#type' => 'checkboxes','#type' => 'checkboxes', '#title' => 'Users may annotate these node types''#title' => 'Users may annotate these node types' '#default_value' => '#default_value' => variable_get('annotate_nodetypes', array('page')),variable_get('annotate_nodetypes', array('page')), '#description' => 'A text field will be available on'#description' => 'A text field will be available on
these node types to make user-specific notes.', these node types to make user-specific notes.', );); $form['array_filter'] = array('#type' => 'hidden');$form['array_filter'] = array('#type' => 'hidden'); return system_settings_form($form); return system_settings_form($form); } }
Add Annotation to NodeAdd Annotation to Node
/**/** * Implementation of hook_nodeapi().* Implementation of hook_nodeapi(). */ */ function annotate_nodeapi(&$node, $op, $teaser, $page) {function annotate_nodeapi(&$node, $op, $teaser, $page) { global $user;global $user; switch ($op) {switch ($op) { case 'view'case 'view' if ($user->uid == 0 || !$page) break; if ($user->uid == 0 || !$page) break; $types_to_annotate = variable_get('annotate_node_types'); $types_to_annotate = variable_get('annotate_node_types'); if (!in_array($node->type, $types_to_annotate, TRUE)) break;if (!in_array($node->type, $types_to_annotate, TRUE)) break; $result = db_query('SELECT note FROM {annotations} WHERE nid = %d $result = db_query('SELECT note FROM {annotations} WHERE nid = %d AND uid = %d', $node->nid, $user->uid);AND uid = %d', $node->nid, $user->uid); $node->annotation = db_result($result);$node->annotation = db_result($result); $node->content['annotation_form'] = array($node->content['annotation_form'] = array( '#value' => drupal_get_form('annotate_entry_form', $node),'#value' => drupal_get_form('annotate_entry_form', $node), '#weight' => 10'#weight' => 10 );); }}} }
How does Drupal Work?How does Drupal Work?
Web server tasks:Web server tasks:Request arrives at server, e.g. Request arrives at server, e.g.
example.com/foo/barexample.com/foo/bar
mod_rewrite rules (in .htaccess file) change mod_rewrite rules (in .htaccess file) change URL to URL to
http://example.com/index.php?q=foo/barhttp://example.com/index.php?q=foo/bar
Processing begins with index.php, with path Processing begins with index.php, with path (foo/bar) passed in as a GET parameter(foo/bar) passed in as a GET parameter
How does Drupal work?How does Drupal work?
Drupal takes over…Drupal takes over…Drupal bootstrap process (defined in Drupal bootstrap process (defined in
bootstrap.inc)bootstrap.inc) Initialization: set up internal config array. Process Initialization: set up internal config array. Process
settings.phpsettings.php Open database connectionOpen database connection Sessions are initialized or read from databaseSessions are initialized or read from database Page cache: Drupal loads enough code to determine Page cache: Drupal loads enough code to determine
whether to serve a page from the page cachewhether to serve a page from the page cache Path: code that handles path aliasing is runPath: code that handles path aliasing is run Full loadFull load Process requestProcess request ThemingTheming
Processing the RequestProcessing the Request
Callback function does whatever is required Callback function does whatever is required to process requestto process request
For example, q=node/3:For example, q=node/3: "Callback mapping": URL is mapped to function "Callback mapping": URL is mapped to function
node_page_view() node_page_view() (A function like node_page_view() is called a (A function like node_page_view() is called a
"hook")"hook") Node module generates output variables Node module generates output variables Then, it's time for themingThen, it's time for theming
SummarySummary
Modules are defined with .info filesModules are defined with .info files
Modules implement Modules implement hookshooks to do stuff to do stuff
Hooks are functions that are called for each Hooks are functions that are called for each module when system events happen, e.g. page module when system events happen, e.g. page load or user loginload or user login