how does drupal work?

Download How does Drupal Work?

Post on 30-Dec-2015

38 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

  • How does Drupal Work?Information Systems 337

    Prof. Harry Plantinga

  • Drupal ModulesDefining a Module: guess how?Create a directory, e.g. /sites/all/modules/annotateCreate a file annotate.info:

    name = Annotate description = Allows users to annotate nodes. package = Example core = "6.x"

  • HooksModule system based on the concept of hooksHook:a PHP function named foo_bar()foo: name of the modulebar: name of the hookExtending Drupal:a module extends Drupal by implementing hooks

  • HooksAn 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

  • ExamplesExample: hook_access($op, $node, $account)this hook allows modules to limit access to the node types they define$op: create, delete update, viewreturns TRUE, FALSE, or NULL (don't override settings in the node_access tableto 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/barmod_rewrite rules (in .htaccess file) change URL to http://example.com/index.php?q=foo/barProcessing begins with index.php, with path (foo/bar) passed in as a GET parameter

  • How does Drupal work?Drupal takes overDrupal bootstrap process (defined in bootstrap.inc)Initialization: set up internal config array. Process settings.phpOpen database connectionSessions are initialized or read from databasePage cache: Drupal loads enough code to determine whether to serve a page from the page cachePath: code that handles path aliasing is runFull loadProcess requestTheming

  • Processing the RequestCallback function does whatever is required to process requestFor 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

  • SummaryModules are defined with .info filesModules implement hooks to do stuffHooks are functions that are called for each module when system events happen, e.g. page load or user login

    Suppose I want to make the banner image less tall or move the menus lower on the page. How?This will take a bit more understanding of the theme system.*Example: I want to move the title "Prepsoccer.org" down a nudge. How?Fire up firebug. Look at the stylesheets used.Edit typography stylesheet. Cool!

    *$items is an array of menu items. We're adding one for the path admin/settings/annotate.Menu items have title, description, function to call when selected*You've noticed that forms all look more or less alike? They are handled by drupal; it's much easier to create a drupal form than "roll your own". This function is called by the previous one when hook_menu is called. **developer module turn on query logging. Watch these things happenLots of queries, eh?*