Intro to Drupal Module Developement

22
Drupal 7 Module Development A Brief Introduction Matt Mendonca

description

Intro to custom Drupal 7 module development.

Transcript of Intro to Drupal Module Developement

Page 1: Intro to Drupal Module Developement

Drupal 7 Module DevelopmentA Brief Introduction

Matt Mendonca

Page 2: Intro to Drupal Module Developement

Background

• IT Specialist at the National Institute of Standards and Technology

• 2+ years professional experience with Drupal • Previously worked for VOX Global

(FleishmanHillard) and High Rock Studios

Page 3: Intro to Drupal Module Developement

Content

• What are modules • Why you should modularize your code • What makes up a module

• What is the hook system • How does that (magic) work

• Resources • Make a module

Page 4: Intro to Drupal Module Developement

Modules Are…

• Building blocks • Encapsulated functionality • Frameworks • API’s

Page 5: Intro to Drupal Module Developement

Why You Should Modularize Your Code

• Code encapsulation • Enable / disable code • Cleanly extend core and / or contributed modules

• Prevents hacking other's code • Preserves upgrade path • https://www.drupal.org/best-practices/do-not-hack-core

• Share your code • Allows your code to be extendable

Page 6: Intro to Drupal Module Developement

Modules And Themes

• Make things work using modules • Make things pretty using themes • Developing modules allows you to keep logic

out of your themes • Separation of concerns

Page 7: Intro to Drupal Module Developement

Module Dissected• Folder designated by the machine name of the

module (/sites/all/modules/dev/my_module)* • Info file designated by the machine name of the

module (my_module.info) • Module file designated by the machine name of

the module (my_module.module) * Drupal’s module scanning is recursive; you can add folders with in the module folder and it will find it. E.g. /sites/all/module/contrib | /sites/all/module/dev | etc.

Page 8: Intro to Drupal Module Developement

Info File

• The .info file describes your module • At the minimum describe name, description, package, and core

Page 9: Intro to Drupal Module Developement

Info File: Name

• Name is the user friendly name of your module • E.g.

name = “My Module”

Page 10: Intro to Drupal Module Developement

Info File: Description

• Description is the description of your module • E.g.

description = “This my module.”

Page 11: Intro to Drupal Module Developement

Info File: Package

• Package is the category of your module • Groups modules together on the module admin

page • E.g.

package = “User Interface”

Page 12: Intro to Drupal Module Developement

Info File: Core

• Core is the major version of Drupal core that your module is designed for

• E.g.

core = 7.x

Page 13: Intro to Drupal Module Developement

Info File: Version• Version describes which release your module is at • By convention, version of Core followed by version of the

module • Note: this is only to be filled out when your module is not on

Drupal.org • E.g.

version = 7.x-1.0

Page 14: Intro to Drupal Module Developement

Info File: Dependencies• Dependencies is an array listing all the modules that your

module requires • You do not need to list the dependencies of your

dependencies • E.g.

dependencies[] = views

dependencies[] = panels

Page 15: Intro to Drupal Module Developement

Info File: Configure

• Configure specifies the url of the module’s configuration page (if any)

• Adds a configure link on the module admin page

• E.g.

configure = admin/config/content/my-module

Page 16: Intro to Drupal Module Developement

Module File

• The module file is the file that Drupal always loads for enabled modules - always

• Should contain your bootstrap code / hooks • When possible, store code in .inc files

Page 17: Intro to Drupal Module Developement

(Captain) Hook

• The hooks system provides access points to Drupal's response cycle

• Used by Modules and Themes (preprocessing) • Like a roll call

Page 18: Intro to Drupal Module Developement

How Does The Hook System Work

• At certain points in Drupal's response cycle, Drupal (and module's with hooks) will run hook-able functions

• Lets explore: https://github.com/matt-mendonca/hooks-explained

Page 19: Intro to Drupal Module Developement

Modules That Don't Have Any Hooks

• There are modules that don't use any hooks • Use Case: loading a php library for use by other

modules

Page 20: Intro to Drupal Module Developement

Resources

• https://www.drupal.org/developing/modules • https://www.drupal.org/best-practices • https://api.drupal.org/api/drupal • https://drupal.stackexchange.com/ • https://stackoverflow.com/questions/tagged/drupal • https://www.google.com/

Page 21: Intro to Drupal Module Developement

Questions?

Page 22: Intro to Drupal Module Developement

Lets Make A Module !

https://github.com/matt-mendonca/example-d7-module