1 00:00:00,000 --> 00:00:04,901 [MUSIC] 2 00:00:04,901 --> 00:00:08,900 We've been building our website using the Drupal web interface so far. 3 00:00:08,900 --> 00:00:12,400 But there's a lot more you can do if you can program PHP. 4 00:00:13,520 --> 00:00:17,560 Drupal is written in PHP, and with a little programming, 5 00:00:17,560 --> 00:00:21,660 you can create your own modules to make Drupal do exactly what you need it to. 6 00:00:22,920 --> 00:00:26,800 There aer many reasons to learn how to build your own modules. 7 00:00:26,800 --> 00:00:30,430 A custom module could create new content types. 8 00:00:30,430 --> 00:00:34,760 Custom modules can also implement behaviors like email notification, 9 00:00:34,760 --> 00:00:39,750 personal messaging between users, and displaying content dynamically with Ajax. 10 00:00:40,760 --> 00:00:44,570 Another common use for custom modules, is to supplement or 11 00:00:44,570 --> 00:00:47,820 modify the behavior of existing contrib modules. 12 00:00:49,140 --> 00:00:53,490 But if we start simple and learn the basic mechanics of how to create and 13 00:00:53,490 --> 00:00:57,900 enable your own module, then you'll be able to expand these techniques 14 00:00:57,900 --> 00:01:00,560 to customize Drupal in whatever ways you need. 15 00:01:01,640 --> 00:01:05,760 Let's make a simple module that will greet a user with the message, 16 00:01:05,760 --> 00:01:07,410 hello my dear friend. 17 00:01:07,410 --> 00:01:13,102 First, open your project directory and create a folder for your module in sites, 18 00:01:13,102 --> 00:01:16,890 all, modules. 19 00:01:16,890 --> 00:01:20,920 Notice this is the same place we put our contrib modules. 20 00:01:20,920 --> 00:01:26,030 This new folder must have the same name as the module that you're building. 21 00:01:26,030 --> 00:01:29,990 It should have a meaningful name, suggesting its purpose. 22 00:01:29,990 --> 00:01:32,610 In this case, we're going to call it greeting. 23 00:01:34,380 --> 00:01:36,360 Now, we're going to open our text editor. 24 00:01:37,390 --> 00:01:42,470 I'm using Sublime text, but you can use whatever editor you'd like. 25 00:01:42,470 --> 00:01:45,260 See the teacher's notes for suggestions. 26 00:01:45,260 --> 00:01:47,790 Now we're going to create two files and 27 00:01:47,790 --> 00:01:50,810 save them inside of our new greeting folder. 28 00:01:50,810 --> 00:01:58,663 The first one is going to be called greeting.module, 29 00:01:58,663 --> 00:02:06,892 and the second one is going to be called greeting.info. 30 00:02:06,892 --> 00:02:10,910 The .module file will contain the core of the code 31 00:02:10,910 --> 00:02:13,850 defining the behavior of the module. 32 00:02:13,850 --> 00:02:18,062 The .info file gives Drupal information about your module, 33 00:02:18,062 --> 00:02:21,690 such as the module name and description. 34 00:02:21,690 --> 00:02:24,940 There can be other files included in a module, but 35 00:02:24,940 --> 00:02:29,680 the dot module and dot info files are the only ones required. 36 00:02:29,680 --> 00:02:32,331 Let's start with the greeting.info file. 37 00:02:34,870 --> 00:02:40,337 We're going to type name = Greeting, 38 00:02:40,337 --> 00:02:43,318 with a capital G, and 39 00:02:43,318 --> 00:02:48,470 description = Display greetings. 40 00:02:48,470 --> 00:02:56,915 Then we're going to type package = My First Modules. 41 00:02:56,915 --> 00:03:01,684 Core = 7.x. 42 00:03:01,684 --> 00:03:07,450 Finally, we'll say, files and then opening and 43 00:03:07,450 --> 00:03:16,940 closing square brackets = greeting.module, the name of our other file. 44 00:03:16,940 --> 00:03:21,301 The .info file needs these five things, name, 45 00:03:21,301 --> 00:03:27,890 description, package, core, and files. 46 00:03:27,890 --> 00:03:32,150 Name, description and package are displayed in the module's administration 47 00:03:32,150 --> 00:03:35,800 page where you can enable your module. 48 00:03:35,800 --> 00:03:41,050 Core defines which version of Drupal the module is compatible with. 49 00:03:41,050 --> 00:03:45,602 We're using Drupal 7, so core is set to 7.x. 50 00:03:45,602 --> 00:03:49,620 The files parameter is an array 51 00:03:49,620 --> 00:03:54,790 containing the relative paths to any external files to load. 52 00:03:54,790 --> 00:03:58,310 Also, while the .module code is written in PHP, 53 00:03:59,550 --> 00:04:04,250 we don't want to use the PHP tags like this. 54 00:04:04,250 --> 00:04:04,800 Leave it plain. 55 00:04:04,800 --> 00:04:10,361 Now let's save that file and work on the greeting.module file. 56 00:04:10,361 --> 00:04:17,280 The Drupal standard says that we should start with the opening PHP tag like this, 57 00:04:17,280 --> 00:04:21,740 but you do not use the closing tag at the bottom of the file. 58 00:04:23,460 --> 00:04:27,720 By not closing out the PHP tag, we eliminate the possibility for 59 00:04:27,720 --> 00:04:32,190 excessive white space errors, as well as some validation issues. 60 00:04:32,190 --> 00:04:35,060 This is one of a number of coding standards for 61 00:04:35,060 --> 00:04:38,810 Drupal that you can find on the drupal.org website. 62 00:04:38,810 --> 00:04:41,320 See the teacher's notes for more information. 63 00:04:41,320 --> 00:04:45,470 After we open our PHP tag, we declare two functions. 64 00:04:45,470 --> 00:04:48,720 The first one's job is to return an array of information, 65 00:04:48,720 --> 00:04:53,160 and the second one's job is to return some HTML. 66 00:04:53,160 --> 00:04:56,710 We start with the function keyword and 67 00:04:56,710 --> 00:05:01,495 we'll name this function, greeting_menu. 68 00:05:02,980 --> 00:05:08,410 Give it some parentheses and the opening and closing curly brackets. 69 00:05:08,410 --> 00:05:11,317 Then let's go ahead and declare our second one. 70 00:05:11,317 --> 00:05:18,911 Function, and we'll call this one say_hello. 71 00:05:18,911 --> 00:05:23,328 Parentheses and curly brackets. 72 00:05:23,328 --> 00:05:30,639 Now, inside of the first one, we're going to declare a variable called items, 73 00:05:30,639 --> 00:05:35,060 and we're going to set it equal to a blank array. 74 00:05:36,890 --> 00:05:43,160 Then, we're going to insert a key and value into the items array. 75 00:05:43,160 --> 00:05:52,240 The key will be, hello_friend, and it is going to equal another array. 76 00:05:55,880 --> 00:05:57,250 Put the semicolon at the end and 77 00:05:58,670 --> 00:06:02,790 then this array will also consist of key value pairs. 78 00:06:02,790 --> 00:06:10,765 First we have title, which we're setting equal to Hello Friend, 79 00:06:10,765 --> 00:06:15,000 exclamation mark, and then a comma. 80 00:06:16,750 --> 00:06:21,040 Then we have page callback. 81 00:06:23,960 --> 00:06:27,995 We're going to set that equal to the name of our second function, 82 00:06:27,995 --> 00:06:33,860 say_hello. 83 00:06:33,860 --> 00:06:37,620 Then, we have the access callback, 84 00:06:39,520 --> 00:06:44,690 which is equal to TRUE, and we have the type, 85 00:06:46,140 --> 00:06:53,630 which is equal to menu_normal_item. 86 00:06:53,630 --> 00:06:54,600 All in caps. 87 00:06:55,660 --> 00:07:00,210 Finally, after creating this array, we want to return it. 88 00:07:00,210 --> 00:07:02,026 Return items. 89 00:07:02,026 --> 00:07:08,400 And now we're gonna put our HTML into the second function and it will say return. 90 00:07:09,680 --> 00:07:17,030 Let's give it a paragraph tag, and then the phrase, Hello my dear friend. 91 00:07:18,640 --> 00:07:20,300 And the closing paragraph tag. 92 00:07:22,190 --> 00:07:23,540 And don't forget the semicolon. 93 00:07:24,730 --> 00:07:29,630 Now we can save this, and go into the Drupal web interface to 94 00:07:29,630 --> 00:07:33,980 enable our module the same way that we did with contrib modules. 95 00:07:33,980 --> 00:07:36,060 Click on Modules in the management menu, 96 00:07:36,060 --> 00:07:40,790 and then scroll down until you see your newly added greeting module. 97 00:07:40,790 --> 00:07:45,390 It will be in a package labeled my first modules. 98 00:07:45,390 --> 00:07:49,280 You can also see the description that you listed in the .info file, 99 00:07:49,280 --> 00:07:51,700 displayed in the module's row. 100 00:07:51,700 --> 00:07:56,370 Click the check box to enable it and then click Save configuration. 101 00:07:58,540 --> 00:08:02,752 At this point if your browser gets stuck at a blank page at 102 00:08:02,752 --> 00:08:10,150 localhost:888/admin/modules/list/confirm, or 103 00:08:10,150 --> 00:08:14,570 if it's doing what my page is doing right now, where it doesn't say that the options 104 00:08:14,570 --> 00:08:19,630 have been successfully saved, then you know there's an error in your code. 105 00:08:19,630 --> 00:08:22,430 Usually a syntax error of some sort. 106 00:08:22,430 --> 00:08:26,490 Go look at your code and compare it to the code above. 107 00:08:26,490 --> 00:08:31,310 Try to fix your error and then reload the page and try again to enable your module. 108 00:08:32,540 --> 00:08:33,520 See here? 109 00:08:33,520 --> 00:08:35,340 I skipped a comma. 110 00:08:35,340 --> 00:08:39,860 Each of these lines in the array needs to have a comma at the end of it. 111 00:08:39,860 --> 00:08:41,350 Let's save and try it now. 112 00:08:43,180 --> 00:08:52,450 I will reload the modules page, and I will scroll down to the greeting module, 113 00:08:52,450 --> 00:08:58,140 click to enable it again, and now I will click Save configuration. 114 00:08:58,140 --> 00:09:02,720 And since we have successfully fixed it, the browser comes back to the module's 115 00:09:02,720 --> 00:09:08,320 page displaying the message, the configuration options have been saved. 116 00:09:08,320 --> 00:09:13,140 Now, if you go to your homepage, you'll see a new link that says, 117 00:09:13,140 --> 00:09:16,310 Hello Friend, under the navigation menu. 118 00:09:16,310 --> 00:09:22,527 If you click it, you'll be taken to localhost:8888/hello_friend, 119 00:09:22,527 --> 00:09:28,730 where your greeting appears, styled using Drupal's current theme. 120 00:09:28,730 --> 00:09:31,270 Let's take a closer look at the .module file. 121 00:09:33,520 --> 00:09:38,325 The .module file is divided into two PHP functions, 122 00:09:38,325 --> 00:09:41,439 greeting_menu and say_hello. 123 00:09:41,439 --> 00:09:47,310 greeting_menu handles input and say_hello handles output. 124 00:09:47,310 --> 00:09:49,810 Let's go through each step in greeting_menu. 125 00:09:49,810 --> 00:09:55,820 First, the greeting menu function is declared with no arguments. 126 00:09:55,820 --> 00:09:59,580 Notice also that the first word of the function name 127 00:09:59,580 --> 00:10:02,780 is the same as the name of the module. 128 00:10:02,780 --> 00:10:06,740 Then we create a new array called items, 129 00:10:06,740 --> 00:10:10,200 then we create a key value pair in the items array. 130 00:10:11,450 --> 00:10:15,190 The key is the string hello friend, and 131 00:10:15,190 --> 00:10:20,780 the value is another array with four other key value pairs. 132 00:10:21,810 --> 00:10:25,030 Then, we return the items array. 133 00:10:25,030 --> 00:10:28,480 You're probably wondering what that items array is used for. 134 00:10:28,480 --> 00:10:33,600 Well, it's key is the URL we use to access our module. 135 00:10:33,600 --> 00:10:38,305 We define it to be hello_friend so that we can go to 136 00:10:38,305 --> 00:10:45,580 http://localhost:8888/hello_friend and 137 00:10:47,360 --> 00:10:50,310 then we see our greeting on the screen. 138 00:10:50,310 --> 00:10:53,900 The URL key makes a menu item for us. 139 00:10:53,900 --> 00:10:54,460 And there it is. 140 00:10:55,940 --> 00:11:00,450 The array attached to this key holds information 141 00:11:00,450 --> 00:11:05,690 that the page needs to load, called the attributes of the menu item. 142 00:11:05,690 --> 00:11:08,510 The attribute named page callback 143 00:11:09,800 --> 00:11:14,590 stores the name of the function to run when your browser goes to the URL. 144 00:11:15,610 --> 00:11:18,700 In this case we run the function say hello, 145 00:11:18,700 --> 00:11:21,950 which returns a string of HTML to display on the page. 146 00:11:23,410 --> 00:11:27,480 Setting the access call back attribute to TRUE 147 00:11:27,480 --> 00:11:32,090 just means that any user has permission to access this URL. 148 00:11:32,090 --> 00:11:32,705 There you have it! 149 00:11:32,705 --> 00:11:36,210 You've completed your first custom Drupal module. 150 00:11:36,210 --> 00:11:37,120 You're well on your way.