Presentation is loading. Please wait.

Presentation is loading. Please wait.

How does Drupal Work? Information Systems 337 Prof. Harry Plantinga.

Similar presentations


Presentation on theme: "How does Drupal Work? Information Systems 337 Prof. Harry Plantinga."— Presentation transcript:

1 How does Drupal Work? Information Systems 337 Prof. Harry Plantinga

2 Drupal Modules Defining a Module: guess how? Defining a Module: guess how? Create a directory, e.g. /sites/all/modules/annotate Create a directory, e.g. /sites/all/modules/annotate Create a file annotate.info : Create a file annotate.info : name = Annotate description = Allows users to annotate nodes. package = Example core = "6.x"

3 Hooks Module system based on the concept of hooks Module system based on the concept of hooks Hook: Hook: a PHP function named foo_bar() a PHP function named foo_bar() foo: name of the module foo: name of the module bar: name of the hook bar: name of the hook Extending Drupal: Extending Drupal: a module extends Drupal by implementing hooks a module extends Drupal by implementing hooks

4 Hooks An event happens, e.g. a user logs into the site. An event happens, e.g. a user logs into the site. Drupal fires the "user hook". Drupal fires the "user hook". That means each module's user hook function will be called: That means each module's user hook function will be called: comment_user() comment_user() node_user() node_user() locale_user() locale_user() myCustomModule_user() myCustomModule_user() Documented on Drupal.org Documented on Drupal.orgDrupal.org

5 Examples Example: hook_access($op, $node, $account) Example: hook_access($op, $node, $account) this hook allows modules to limit access to the node types they define this hook allows modules to limit access to the node types they define $op: create, delete update, view $op: create, delete update, view returns TRUE, FALSE, or NULL (don't override settings in the node_access table returns TRUE, FALSE, or NULL (don't override settings in the node_access table to override in "example" module, you would implement example_access() to override in "example" module, you would implement example_access()

6 Add a Menu Entry /** * Implementation of hook_menu() * Implementation of hook_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;}

7 Define Settings Form /** * Define the settings form. * Define the settings form. */ */ 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 these node types to make user-specific notes.', '#description' => 'A text field will be available on 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);}

8 Add Annotation to Node /** * Implementation of hook_nodeapi(). * Implementation of hook_nodeapi(). */ */ 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 ); ); }}

9 How does Drupal Work? Web server tasks: Web server tasks: Request arrives at server, e.g. example.com/foo/bar Request arrives at server, e.g. example.com/foo/bar mod_rewrite rules (in.htaccess file) change URL to mod_rewrite rules (in.htaccess file) change URL tohttp://example.com/index.php?q=foo/bar Processing begins with index.php, with path (foo/bar) passed in as a GET parameter Processing begins with index.php, with path (foo/bar) passed in as a GET parameter

10 How does Drupal work? Drupal takes over… Drupal takes over… Drupal bootstrap process (defined in bootstrap.inc) Drupal bootstrap process (defined in bootstrap.inc) Initialization: set up internal config array. Process settings.php Initialization: set up internal config array. Process settings.php Open database connection Open database connection Sessions are initialized or read from database Sessions are initialized or read from database Page cache: Drupal loads enough code to determine whether to serve a page from the page cache 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 Path: code that handles path aliasing is run Full load Full load Process request Process request Theming Theming

11 Processing the Request Callback function does whatever is required to process request Callback function does whatever is required to process request For example, q=node/3: For example, q=node/3: "Callback mapping": URL is mapped to function node_page_view() "Callback mapping": URL is mapped to function node_page_view() (A function like node_page_view() is called a "hook") (A function like node_page_view() is called a "hook") Node module generates output variables Node module generates output variables Then, it's time for theming Then, it's time for theming

12 Summary Modules are defined with.info files Modules are defined with.info files Modules implement hooks to do stuff 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 Hooks are functions that are called for each module when system events happen, e.g. page load or user login


Download ppt "How does Drupal Work? Information Systems 337 Prof. Harry Plantinga."

Similar presentations


Ads by Google