81
1 by April Sides

Introduction to Module Development (Drupal 7)

Embed Size (px)

Citation preview

Page 1: Introduction to Module Development (Drupal 7)

1

by April Sides

Page 2: Introduction to Module Development (Drupal 7)

2

April Sides

Assessment Web Developer

North Carolina Institute of Climate Studies (NCICS)North Carolina State University

National Centers for Environmental Information (NCEI)

Drupal ID: weekbeforenext

Asheville Drupal User Group Member

Page 3: Introduction to Module Development (Drupal 7)

3

• Tools - what you need to develop a module• API/Hooks - how to get Drupal to run your code• Anatomy - what files you need and what code

should be included in those files• Resources - where to find the right hooks• Example - how to modify an existing form, etc.

Page 4: Introduction to Module Development (Drupal 7)

4

• Some PHP coding experience• Plain text editor

Mac: Sublime Text - http://www.sublimetext.com

PC: Notepad++ - https://notepad-plus-plus.org

• A working Drupal site Acquia Dev Desktop - https://www.acquia.com/downloads

• Troubleshooting /research resources Devel module - https://www.drupal.org/project/devel

Internet access

Page 5: Introduction to Module Development (Drupal 7)

5

• What is an API? An application programming interface (API) expresses a

software component in terms of its operations, inputs, outputs and underlying types.

Drupal core modules contain MODULE_NAME.api.php files defining functions that other modules can “hook” into.

Well documented at http://api.drupal.org

Page 6: Introduction to Module Development (Drupal 7)

6

• What is a hook? Hooks are how modules can interact with the core code

of Drupal. A hook can be thought of as an event listener in the sense

that an event triggers an action. Ex. hook_delete Some hooks let you alter things that have been generated

just before they're processed. Ex. hook_form_alter Other hooks allow you to add or register new content

items. Ex. hook_block_info

Page 7: Introduction to Module Development (Drupal 7)

7

Drupal

sites

all

modules

block_example

Page 8: Introduction to Module Development (Drupal 7)

8

block_example.module

block_exampleblock_example.info block_example.test

block_example.install

Page 9: Introduction to Module Development (Drupal 7)

9

block_example.info

Page 10: Introduction to Module Development (Drupal 7)

10

block_example.module

Page 11: Introduction to Module Development (Drupal 7)

11

block_example.install

Page 12: Introduction to Module Development (Drupal 7)

12

• I want to modify the contact form

Page 13: Introduction to Module Development (Drupal 7)

13

• I want to modify the contact form: Enable “Send yourself a copy.” checkbox by default Change the title of the form to “AWESOME Contact

Form” Change the field label “Your name” to “Your AWESOME

name” Add help text to the Subject field with suggestions Change the text of the submit button to “Send your

AWESOME message”

Page 14: Introduction to Module Development (Drupal 7)

14

my_module.info

Page 15: Introduction to Module Development (Drupal 7)

15

my_module.module

Page 16: Introduction to Module Development (Drupal 7)

16

Page 17: Introduction to Module Development (Drupal 7)

17

api.drupal.org – hook_form_alter

Page 18: Introduction to Module Development (Drupal 7)

18

Page 19: Introduction to Module Development (Drupal 7)

19

Page 20: Introduction to Module Development (Drupal 7)

20

Page 21: Introduction to Module Development (Drupal 7)

21

Page 22: Introduction to Module Development (Drupal 7)

22

Page 23: Introduction to Module Development (Drupal 7)

23

Page 24: Introduction to Module Development (Drupal 7)

24

api.drupal.org – Form API Reference

Page 25: Introduction to Module Development (Drupal 7)

25

Page 26: Introduction to Module Development (Drupal 7)

26

Page 27: Introduction to Module Development (Drupal 7)

27

Page 28: Introduction to Module Development (Drupal 7)

28

Page 29: Introduction to Module Development (Drupal 7)

29

api.drupal.org – drupal_set_title

Page 30: Introduction to Module Development (Drupal 7)

30

Page 31: Introduction to Module Development (Drupal 7)

31

Page 32: Introduction to Module Development (Drupal 7)

32

Page 33: Introduction to Module Development (Drupal 7)

33

api.drupal.org – Form API Reference

Page 34: Introduction to Module Development (Drupal 7)

34

api.drupal.org – Form API Reference

Page 35: Introduction to Module Development (Drupal 7)

35

Page 36: Introduction to Module Development (Drupal 7)

36

Page 37: Introduction to Module Development (Drupal 7)

37

Page 38: Introduction to Module Development (Drupal 7)

38

api.drupal.org – Form API Reference

Page 39: Introduction to Module Development (Drupal 7)

39

api.drupal.org – Form API Reference

Page 40: Introduction to Module Development (Drupal 7)

40

Page 41: Introduction to Module Development (Drupal 7)

41

Page 42: Introduction to Module Development (Drupal 7)

42

Page 43: Introduction to Module Development (Drupal 7)

43

api.drupal.org – Form API Reference

Page 44: Introduction to Module Development (Drupal 7)

44

Page 45: Introduction to Module Development (Drupal 7)

45

Page 46: Introduction to Module Development (Drupal 7)

46

• I want to add a non-node page: The page should be accessible by anyone who can see

published content at: mysite.com/awesome-page The title of the page should be “I can make a page!” Include the following paragraph on the page: “I am so

awesome that I can create my own page in a custom module.”

Page 47: Introduction to Module Development (Drupal 7)

47

api.drupal.org – hook_menu

Page 48: Introduction to Module Development (Drupal 7)

48

Page 49: Introduction to Module Development (Drupal 7)

49

Page 50: Introduction to Module Development (Drupal 7)

50

api.drupal.org – hook_menu

Page 51: Introduction to Module Development (Drupal 7)

51

Page 52: Introduction to Module Development (Drupal 7)

52

Page 53: Introduction to Module Development (Drupal 7)

53

Page 54: Introduction to Module Development (Drupal 7)

54

Page 55: Introduction to Module Development (Drupal 7)

55

Page 56: Introduction to Module Development (Drupal 7)

56

api.drupal.org – hook_menu

Page 57: Introduction to Module Development (Drupal 7)

57

Page 58: Introduction to Module Development (Drupal 7)

58

api.drupal.org – hook_menu

Page 59: Introduction to Module Development (Drupal 7)

59

Page 60: Introduction to Module Development (Drupal 7)

60

Page 61: Introduction to Module Development (Drupal 7)

61

Page 62: Introduction to Module Development (Drupal 7)

62

Page 63: Introduction to Module Development (Drupal 7)

63

FLUSH THE CACHE

Page 64: Introduction to Module Development (Drupal 7)

64

Page 65: Introduction to Module Development (Drupal 7)

65

• I want the following to happen when an author creates a new article: Change the author to me, so it looks like I’m doing more

work than I am. Make sure that all new articles are set to publish by

default, so my boss will see “my” work.

Page 66: Introduction to Module Development (Drupal 7)

66

api.drupal.org – Node API Hooks

Page 67: Introduction to Module Development (Drupal 7)

67

api.drupal.org – hook_node_insert

Page 68: Introduction to Module Development (Drupal 7)

68

Page 69: Introduction to Module Development (Drupal 7)

69

Page 70: Introduction to Module Development (Drupal 7)

70

Page 71: Introduction to Module Development (Drupal 7)

71

Page 72: Introduction to Module Development (Drupal 7)

72

Page 73: Introduction to Module Development (Drupal 7)

73

Page 74: Introduction to Module Development (Drupal 7)

74

Page 75: Introduction to Module Development (Drupal 7)

75

Page 76: Introduction to Module Development (Drupal 7)

76

Page 77: Introduction to Module Development (Drupal 7)

77

Page 78: Introduction to Module Development (Drupal 7)

78

node/add/article

Page 79: Introduction to Module Development (Drupal 7)

79

Page 80: Introduction to Module Development (Drupal 7)

80

node/15/edit

Page 81: Introduction to Module Development (Drupal 7)

81

• Examples for Developers - https://www.drupal.org/project/examples

• Drupal API reference - https://api.drupal.org

• DrupalContrib API reference - http://www.drupalcontrib.org

• Drupal Answers - http://drupal.stackexchange.com