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