Drupal Module Development



Similar documents
Data Management Applications with Drupal as Your Framework

Introduction to Module Development

MASTER DRUPAL 7 MODULE DEVELOPMENT

Drupal 8. Core and API Changes Shabir Ahmad MS Software Engg. NUST Principal Software Engineser PHP/Drupal

How does Drupal 7 Work? Tess Flynn, KDØPQK

The truth about Drupal

Web Development using PHP (WD_PHP) Duration 1.5 months

Drupal for Designers

Drupal and ArcGIS Yes, it can be done. Frank McLean Developer

Entites in Drupal 8. Sascha Grossenbacher Christophe Galli

Auditing Drupal sites for performance, content and optimal configuration

Online shopping store

Using your Drupal Website Book 1 - Drupal Basics

Drupal CMS for marketing sites

Microsoft Windows PowerShell v2 For Administrators

Advanced Web Development SCOPE OF WEB DEVELOPMENT INDUSTRY

Commerce Services Documentation

Develop a Native App (ios and Android) for a Drupal Website without Learning Objective-C or Java. Drupaldelphia 2014 By Joe Roberts

The Tiny Book of Rules

The SkySQL Administration Console

Lesson 07: MS ACCESS - Handout. Introduction to database (30 mins)

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

UNIVERSITY OF CALGARY Information Technologies WEBFORMS DRUPAL 7 WEB CONTENT MANAGEMENT

A (Web) Face for Radio. NPR and Drupal7 David Moore

5 Mistakes to Avoid on Your Drupal Website

Informix Administration Overview

Rochester Institute of Technology. Finance and Administration. Drupal 7 Training Documentation

When a variable is assigned as a Process Initialization variable its value is provided at the beginning of the process.

Broker Portal Tutorial Broker Portal Basics

STATISTICA VERSION 9 STATISTICA ENTERPRISE INSTALLATION INSTRUCTIONS FOR USE WITH TERMINAL SERVER

Oracle Database 10g Express

Certified PHP/MySQL Web Developer Course

Multivendor Extension User Guide

Product Name: ANZ egate Connect Version: Document Type: Help doc Author: Milople Inc.

Smooks Dev Tools Reference Guide. Version: GA

FileMaker 12. ODBC and JDBC Guide

System Administration and Log Management

GETTING STARTED WITH DRUPAL. by Stephen Cross

SuiteCRM for Developers

Shop by Manufacturer Custom Module for Magento

Cloud Elements ecommerce Hub Provisioning Guide API Version 2.0 BETA

X-POS GUIDE. v3.4 INSTALLATION SmartOSC and X-POS

A Brief Introduction to MySQL

Getting Content into Drupal Using Migrate

Design principles of the Drupal CSC website

InstantSearch+ for Magento Extension

FileMaker 13. ODBC and JDBC Guide

Geodatabase Programming with SQL

Drupal Drush Guide. Drupal.org

ODBC Driver Version 4 Manual

Everything you ever wanted to know about Drupal 8*

Unicenter NSM Integration for Remedy (v 1.0.5)

Database Studio is the new tool to administrate SAP MaxDB database instances as of version 7.5.

LAMP [Linux. Apache. MySQL. PHP] Industrial Implementations Module Description

Who? Wolfgang Ziegler (fago) Klaus Purer (klausi) Sebastian Gilits (sepgil) epiqo Austrian based Drupal company Drupal Austria user group

MXSAVE XMLRPC Web Service Guide. Last Revision: 6/14/2012

Getting Started with Telerik Data Access. Contents

IceWarp to IceWarp Server Migration

FormAPI, AJAX and Node.js

STATISTICA VERSION 10 STATISTICA ENTERPRISE SERVER INSTALLATION INSTRUCTIONS

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Content Management System

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

This is a training module for Maximo Asset Management V7.1. It demonstrates how to use the E-Audit function.

Getting Started with the Aloha Community Template for Salesforce Identity

Note: With v3.2, the DocuSign Fetch application was renamed DocuSign Retrieve.

PAW Web Filter Version 0.30 (release) This Software is Open Source. project.sourceforge.net

Build it with Drupal 8

How To Fix A Bug In Drupal 8.Dev

Simple Tips to Improve Drupal Performance: No Coding Required. By Erik Webb, Senior Technical Consultant, Acquia

How To Use Query Console

Magento Extension REVIEW BOOSTER User Guide

Hack-proof Your Drupal App. Key Habits of Secure Drupal Coding

WebSphere Business Monitor

Listed below are the common process in creating a new content type, and listing a summary of all contents via view and/or panel custom page.

Plugin Integration Guide

System Administration Training Guide. S100 Installation and Site Management

Quick Start Guide. Contents. Quick Start Guide Version 1.0 webcrm November 09

CiviCRM for Drupal Developers

Reseller Panel Step-by-Step Guide

Oracle FLEXCUBE Universal Banking 12.0 RAD Notification Development. Release 1.0

Sophos Mobile Control Web service guide

FileMaker 11. ODBC and JDBC Guide

CSCI110 Exercise 4: Database - MySQL

MyanPay API Integration with Magento CMS

Bazaarvoice for Magento Extension Implementation Guide v6.3.4

PHP Language Binding Guide For The Connection Cloud Web Services

Top Navigation menu - Tabs. User Guide 1. &

Developing SQL and PL/SQL with JDeveloper

Installation Instruction STATISTICA Enterprise Small Business

J j enterpririse. Oracle Application Express 3. Develop Native Oracle database-centric web applications quickly and easily with Oracle APEX

How to pull content from the PMP into Core Publisher

Transcription:

Drupal Module Development Or: How I Learned to Stop Worrying and Love the Module Alastair Moore & Paul Flewelling 1

What does a module do? Core modules provides functionality Contributed modules extends functionality 2

Examples of modules Core required modules: System, Filter, Node, Block, User Core optional modules: Taxonomy, Menu, Comment, Search 3rd party modules: Views, Panels, CCK, Devel 3

Hooks Can be thought of as internal Drupal events Allow modules to interact with the Drupal core Events include: Creation, deletion of nodes and users Logging in, logging out Hooks can also create and alter menus and forms 4

Hook examples hook_user login, logout, insert, view, update hook_block view, save, configure, list hook_form_alter hook_insert hook_comment insert, update, view, publish, unpublish hook_user - allows the module to react when operations are performed hook_block - declares a block or set of blocks hook_form_alter - performs alterations to a form or forms before it s rendered 5

Useful modules Devel (drupal.org/project/devel) Admin Menu (drupal.org/project/admin_menu) Drush (drupal.org/project/drush) Any others? Module builder - still in beta. For the lazy. Drush - command line utility. Drupal Shell. Installing modules, clearing cache, run SQL commands on the database 6

Our first module Hello World block module Two files required for a module: helloworld.info helloworld.module (Optionally) helloworld.install What is a block? Blocks are snippets of text or functionality that usually live outside the main content area of a web site. 7

Our first module Where should I put my module? Everyone elses: /sites/drupalsouth.local/modules/contrib Yours (in development): /sites/drupalsouth.local/modules/dev Yours (after development): /sites/drupalsouth.local/modules/boost 8

helloworld.info ; $Id$ name = Hello World description = Creates a Hello World block package = Dev core = 6.x 9

helloworld.module <?php function helloworld_block($op = list, $delta = 0, $edit = array()) { switch($op) { case list : $block[0][ info ] = t( Hello World ); return $block; break; 10 Only first <?php declaration needed. Removes possibility of errors occurring due to unwanted whitespace. Edit contains submitted form data from block config form.

helloworld.module case view : $block[ subject ] = t( Hello World! ); $content = Hello world! ; $block[ content ] = $content; return $block; break; 11

helloworld.module case configure : $form[ helloworld_count ] = array( #type => textfield, #title => t( Number of Hello Worlds to display ), #default_value => variable_get( helloworld_count, 1) ); return $form; break; 12

helloworld.module case 'save': variable_set('helloworld_count', $edit['helloworld_count']); break; 13

helloworld.module case view : $block[ subject ] = t( Hello World! ); $count = variable_get( helloworld_count, 1); for ($i = 0; $i < $count; $i++) { $content.= '<li>hello world!</li>'; $block['content'] = $content; return $block; break; 14

So far, so good? Any questions? 15

Theming a module Register a theme Implement a theme function Applying the theme Utilise external theme templates 16

Registering a theme function helloworld_theme() { return array( helloworld_show => array( arguments => array( content => NULL), ), ); 17

Implementing theme function theme_helloworld_show($content) { $output = <ul>$content</ul> ; return $output; 18

Applying the theme case 'view': $block['subject'] = t('hello World!'); $count = variable_get('helloworld_count', 1); for ($i = 0; $i < $count; $i++) { $text.= '<li>hello world!</li>'; $content = theme('helloworld_show', $text); $block['content'] = $content; return $block; break; 19

External theme file function helloworld_theme() { return array( 'helloworld_show' => array( 'arguments' => array('content' => NULL), 'template' => 'helloworld_show' ), ); 20

helloworld_show.tpl.php <p> This is being called from the external file. </p> <ul> <?php print $content;?> </ul> 21

Using deltas function helloworld_block($op = list, $delta = 0, $edit = array()) { switch($op) { case list : $blocks[0][ info ] = t( Hello World ); $blocks[1][ info ] = t( Goodbye Cruel World ); return $blocks; break; Delta - unique block id (per implementation of hook_block). Can be an integer or string. 22

Using deltas case 'view': switch ($delta) { case 0: $block['subject'] = t('hello World!'); $content = 'Hi! :-)'; break; case 1: $block['subject'] = t('goodbye Cruel World!'); $content = 'Bye... :-('; break; $block['content'] = $content; 23

Persisting Our Message Databases and Admin Forms 1. Create a table in the database that will hold our message 2. Create an administration form so we change the message 24 We decide that we want to store the hello world message in the database. This would usually be used for a more sophisticated problem, but I d like to demonstrate the use of the database and the Form API to create an administration form. So, we need to 1. Create a table in the database to hold our hello world message and 2. Create an administration form so we can change the message

Modifying the database Create a new file called helloworld.install Define the schema using the hook hook_schema Install the schema using hook_install To modify the database we create a new file called helloworld.install 25 We then define the schema of our database changes using the hook hook_schema We then install the schema using the hook hook_install

Define the Schema hook_schema function helloworld_schema() { $schema['helloworld'] = array( 'description' => t('helloworld Text'), 'fields' => array( 'helloworld_text' => array( 'description' => t('text to say to the World'), 'type' => 'varchar', 'length' => 128, 'not null' => TRUE, ), ); return $schema; <-- DEFINES A TABLE <-- DEFINES A FIELD The hook_schema looks something like this. 26 Here we are describing a new table called helloworld and a single field to store the message. In fact our schema is slightly more complicated than it is shown here. We will also be defining a display_count and unique identifier.

hook_install Install the schema function helloworld_install() { drupal_install_schema('helloworld'); 27

hook_uninstall Uninstall the schema function helloworld_uninstall() { drupal_install_schema('helloworld'); 28 And for good measure, the hook_uninstall. This removes the table (and related data) from the database. This is run when using Uninstall in the modules area of the admin console. So we end up with a file that looks like this SHOW ACTUAL SCHEMA CODE

Creating the Admin Form New file called helloworld.admin.inc Create the form using the Form API So, we ve created out database, now its time to create the administration form. 29

Creating the Form - 1 function helloworld_admin_edit() { $form = array(); $form['helloworld'] = array( '#type' => 'fieldset', '#title' => t('helloworld'), '#description' => t("the message to send to the World") ); $form['helloworld']['message'] = array( '#title' => t('helloworld Message Text'), '#type' => 'textfield', ); '#description' => t('the Helloworld Message Text'), '#size' => 30, '#maxlength' => 30, <-- DEFINES THE FORM <-- DEFINES 1ST FIELD Here we re building up the form using the Form API to define the form and its fields. 30 For example the first chunk of code here defines the form, its name and its purpose. The second chunk represents the field the message will be typed into, currently limited 30 characters.

Creating the Form - 2 (continued) $form['helloworld']['display_count'] = array( ); '#title' => t('display Count'), '#type' => 'textfield', '#description' => t('the number of times to display'), '#size' => 2, '#maxlength' => 2, $form['submit'] = array( ); '#type' => 'submit', '#value' => t('update'), return $form; <-- DEFINES THE DISPLAY COUNT FIELD <-- SUBMIT BUTTON Another field defined here allows us to record the display_count, i.e. how many times the message will be repeated in the block. 31 And finally we define the form s submit button

Form Validation Check the data is legit function helloworld_admin_edit_validate($element, $form_state) { $message = $form_state['values']['message']; if (empty($message)) { form_set_error('message', t('please enter a message for the World')); $display_count = $form_state['values']['display_count']; if (!is_numeric($display_count)) { form_set_error('display_count', t('please enter a display count')); Note the name of the method, it specifically validates module Helloworld s admin_edit form 32 1. Validates that the message isn t empty 2. Validates the display_count is a number

Submit Update the database function helloworld_admin_edit_submit($form, &$form_state) { $result = db_query("update {helloworld SET message = '%s', display_count = '%d' WHERE {helloworld.hwid = 1", $form_state['values']['message'], $form_state['values']['display_count']); if ($result) drupal_set_message(t('the Helloworld text has been updated.')); 33

Permissions hook_perm function helloworld_perm() { return array('change message', 'view message'); 34 This gives us to options under /users/permissions, allowing us to determine who can change the message and who can view the message. *SHOW USER / PERMISSION ADMIN PAGE*

Menus hook_menu function helloworld_menu() { $items['admin/settings/helloworld/settings'] = array( 'title' => 'Hello World settings', 'page callback' => 'drupal_get_form', 'page arguments' => array('helloworld_admin_edit'), 'access arguments' => array('change message'), 'file' => 'helloworld.admin.inc', 'type' => MENU_NORMAL_ITEM, ); return $items; <-- FORM API <-- PERMISSION 35 hook_menu is placed in the module file, it defines an entry in Drupals menu hierarchy so that we can access the administration form. *SHOW MENU*

Updating the Module Now need to retrieve data from the database Modifiy the block code to display the data We ve completed the major steps, now all we need to do is 36 1. Retrieve the data from the database and 2. Modifiy the block code display the data

Retrieve Message Query database and return data objects function _helloworld_get_message() { $result = db_query('select {helloworld.message, {helloworld.display_count FROM {helloworld WHERE {helloworld.hwid = 1'); $rows = array(); while($row = db_fetch_object($result)) { $rows[] = $row; return $rows; This code also goes in the module file. 37 It executes a query to retrieve the data from the helloworld table. It returns a database object

Modify Block Update hook_block view to use data objects case 'view': $block['subject'] = t('hello World!'); $hw_vars = _helloworld_get_message(); for($i = 0; $i < $hw_vars[0]->display_count; $i++) { $content.= "<p>".$hw_vars[0]->message."</p>"; $block['content'] = $content; return $block; break; Here we see the view case of the hook_block method we defined earlier. 38 It has been modified to call call the helloworld_get_message method that queries the database. It then formats the data and places it in the $content variable ready for display by the block. *SHOW THE FINISHED PRODUCT FROM END TO END*

Further resources api.drupal.org Pro Drupal Development (2nd Edition) Learning Drupal 6 Module Development www.lullabot.com 39