1 00:00:00,260 --> 00:00:03,850 Let's continue to work on our module from the last video. 2 00:00:03,850 --> 00:00:07,460 If we go to our form on the website, fill it out, 3 00:00:10,320 --> 00:00:14,900 and click the submit button, the page just reloads. 4 00:00:14,900 --> 00:00:17,670 Nothing actually happens with the form data. 5 00:00:17,670 --> 00:00:22,490 That's because we haven't told the module what to do with that form submission. 6 00:00:22,490 --> 00:00:26,038 Let's do that by hooking into the form submission event. 7 00:00:26,038 --> 00:00:35,540 It's called hook_form_submit. 8 00:00:35,540 --> 00:00:40,810 We'll implement it by adding a function to our summed up module file named 9 00:00:40,810 --> 00:00:45,900 after the hook, replacing the word hook with the name of our module. 10 00:00:46,960 --> 00:00:53,011 Followed by the rest of the name of the hook, giving us, sum_form_submit. 11 00:00:54,270 --> 00:00:58,980 I'll add in the function keyword, give it some parentheses, 12 00:01:00,400 --> 00:01:02,920 curly brackets, and we're ready. 13 00:01:02,920 --> 00:01:07,208 Next, we need to pass in two variables to our function. 14 00:01:07,208 --> 00:01:10,598 $form, 15 00:01:10,598 --> 00:01:17,805 &$form_state. 16 00:01:17,805 --> 00:01:21,354 Their names are defined by the documentation of the hook, 17 00:01:21,354 --> 00:01:23,898 which you can find in the teachers notes. 18 00:01:23,898 --> 00:01:32,150 The first one, $form, is the one that we created in the function sum_form. 19 00:01:32,150 --> 00:01:34,900 It defines which fields the forms should display. 20 00:01:36,350 --> 00:01:37,490 But what is this one? 21 00:01:38,600 --> 00:01:42,800 After the form is submitted all of the user supplied details or 22 00:01:42,800 --> 00:01:47,290 stored in another large array called form state. 23 00:01:47,290 --> 00:01:53,670 This array also includes additional information, such as the form's HTML ID. 24 00:01:53,670 --> 00:01:58,190 We're going to set the value of the redirect key in this array 25 00:01:58,190 --> 00:02:00,470 to the string success. 26 00:02:00,470 --> 00:02:04,000 This tells Drupal where to send our form. 27 00:02:04,000 --> 00:02:08,500 So I'm going to say, $form_state. 28 00:02:08,500 --> 00:02:11,700 You don't need to use the & once you are inside of the function. 29 00:02:13,070 --> 00:02:17,810 And give it some square brackets, pair of quotes and 30 00:02:17,810 --> 00:02:25,420 I am setting the redirect key equal to the value of the string success. 31 00:02:26,440 --> 00:02:27,660 Don't forget the semicolon. 32 00:02:28,700 --> 00:02:31,250 Now let's try submitting your form. 33 00:02:31,250 --> 00:02:34,550 I've already cleared my cache and reloaded the module. 34 00:02:34,550 --> 00:02:37,830 So let's click on Calculate Sum and fill it out. 35 00:02:40,020 --> 00:02:43,710 I'll click on the Calculate Sum button to submit the form. 36 00:02:45,020 --> 00:02:48,075 And this time we're redirected to a new URL, 37 00:02:48,075 --> 00:02:52,900 localhost:8888/success. 38 00:02:52,900 --> 00:02:56,093 Just as we defined in the form_state array, 39 00:02:56,093 --> 00:02:58,872 we are redirecting to the URL success. 40 00:03:01,402 --> 00:03:06,199 For now, we're seeing page not found when we submit the form, but 41 00:03:06,199 --> 00:03:12,730 that makes sense because we haven't told Drupal to create the success page yet. 42 00:03:12,730 --> 00:03:14,530 Let's do that next. 43 00:03:14,530 --> 00:03:19,170 Remember, if you don't see any changes on your site after you've changed your module 44 00:03:19,170 --> 00:03:24,580 code, then clear your cash, disable, and re-enable your module. 45 00:03:24,580 --> 00:03:29,150 We need to add a key value pair to the items array in hook menu, 46 00:03:29,150 --> 00:03:33,720 linking the URL's success to a page call back function that we will write. 47 00:03:35,060 --> 00:03:41,120 So I'm going to say, items at success 48 00:03:41,120 --> 00:03:45,650 is equal to a blank array for now. 49 00:03:45,650 --> 00:03:48,990 We can fill this in with our attributes in a moment. 50 00:03:48,990 --> 00:03:51,020 Now, let's write the page callback function. 51 00:03:53,150 --> 00:03:59,190 We're going to call it, sum_success. 52 00:03:59,190 --> 00:04:02,360 Give it is parentheses and curly brackets. 53 00:04:02,360 --> 00:04:06,734 And this function will just print 54 00:04:06,734 --> 00:04:11,590 the message, you calculated a sum. 55 00:04:11,590 --> 00:04:16,160 We use the return keyword because we are just returning some HTML, 56 00:04:17,330 --> 00:04:18,430 and then a semicolon. 57 00:04:19,710 --> 00:04:23,640 Now let's finish filling out the attributes for the URL and hook menu. 58 00:04:26,880 --> 00:04:28,290 First I'll give it a title. 59 00:04:30,610 --> 00:04:32,530 Let's call it, success. 60 00:04:33,940 --> 00:04:35,380 Don't forget the comma. 61 00:04:35,380 --> 00:04:37,655 Next, we don't want a link for 62 00:04:37,655 --> 00:04:40,900 the success page the page to appear in our navigation menu. 63 00:04:40,900 --> 00:04:44,990 The page should automatically appear when the user submits the form. 64 00:04:44,990 --> 00:04:52,077 To hide that page from the menu, set the type attribute to be MENU_CALLBACK 65 00:04:52,077 --> 00:04:57,150 instead of MENU_NORMAL_ITEM. 66 00:04:57,150 --> 00:05:02,580 And finally, let's not forget to set our access callback 67 00:05:02,580 --> 00:05:09,280 to be true, so that anyone can view the page if they submit the form. 68 00:05:09,280 --> 00:05:13,700 Next, let's actually calculate the sum of the two form fields. 69 00:05:13,700 --> 00:05:18,130 In Drupal, when a form is submitted, form data is passed 70 00:05:18,130 --> 00:05:23,330 to hook form submit inside of the form state array. 71 00:05:23,330 --> 00:05:28,580 The actual form data is stored in the values key of that array. 72 00:05:28,580 --> 00:05:33,130 We'll pull the numbers our user entered out of this nested array, and 73 00:05:33,130 --> 00:05:38,470 do our calculation inside of the hook form submit function, like this. 74 00:05:40,140 --> 00:05:45,870 In the first line, we create a variable called first number. 75 00:05:45,870 --> 00:05:49,700 And will store the value from the first form field in it. 76 00:05:49,700 --> 00:05:54,760 To get to that number, we look inside of the form state array 77 00:05:54,760 --> 00:06:00,490 at the key values, which is also an array. 78 00:06:00,490 --> 00:06:03,913 So we look inside of that array for 79 00:06:03,913 --> 00:06:08,570 the key named after our field left_number. 80 00:06:10,153 --> 00:06:13,111 And don’t forget the semicolon. 81 00:06:13,111 --> 00:06:16,304 Here is where we defined that left_number. 82 00:06:18,154 --> 00:06:23,398 Let’s do the same for the other number, which is here, right_number. 83 00:06:23,398 --> 00:06:26,868 And we’ll put it inside of another variable, which we'll call. 84 00:06:30,800 --> 00:06:36,399 Second_number. 85 00:06:36,399 --> 00:06:39,357 Don't forget to change that to right_number. 86 00:06:39,357 --> 00:06:43,490 And now, we can add the two numbers together and 87 00:06:43,490 --> 00:06:47,429 store the solution in a variable called sum. 88 00:06:49,910 --> 00:06:58,610 So sum = first_number + second_number;. 89 00:06:58,610 --> 00:07:04,580 But if we try this out our success page will just say you calculated a sum. 90 00:07:04,580 --> 00:07:06,570 How do we display the sum on the new page? 91 00:07:06,570 --> 00:07:12,250 The simplest way is to store our data as a session variable like this. 92 00:07:14,625 --> 00:07:21,970 $_SESSION is another associative array. 93 00:07:21,970 --> 00:07:25,890 It is a special variable, built into the PHP language. 94 00:07:25,890 --> 00:07:29,320 Which can be accessed from anywhere in your code. 95 00:07:29,320 --> 00:07:31,920 It give us a place to store information so 96 00:07:31,920 --> 00:07:35,520 that we can use it on multiple pages in our website. 97 00:07:35,520 --> 00:07:39,840 We're calculating the answer to our sum when the form is submitted. 98 00:07:39,840 --> 00:07:47,610 So at that time, we're still on the page at URLLocalhost8888/sum. 99 00:07:47,610 --> 00:07:55,340 Then, we want to redirect to a new page at URL/success to show to answer. 100 00:07:55,340 --> 00:07:59,010 So, after we process the form, we store its data 101 00:07:59,010 --> 00:08:04,300 inside of this session so that we can display it on a new page. 102 00:08:04,300 --> 00:08:09,620 We store our answer in the session array by creating a key value pair in it, 103 00:08:09,620 --> 00:08:11,120 just like with any other array. 104 00:08:12,130 --> 00:08:18,030 The key is sum_output. 105 00:08:18,030 --> 00:08:19,680 And I just chose that name. 106 00:08:19,680 --> 00:08:22,920 It could be any name that isn't already in use. 107 00:08:22,920 --> 00:08:27,810 And we're going to set it equal to the value of our calculated sum. 108 00:08:29,200 --> 00:08:34,980 Then inside of our sum_success function, we can get the answer to display 109 00:08:34,980 --> 00:08:40,435 by looking inside of that session array at the key which we created, 110 00:08:40,435 --> 00:08:44,420 sum_output. 111 00:08:44,420 --> 00:08:48,169 And it will give us the value of the sum which we can then print. 112 00:08:53,788 --> 00:08:58,240 Okay, let's try out our new module and see if it works. 113 00:08:58,240 --> 00:09:00,760 I have already cleared the cache. 114 00:09:00,760 --> 00:09:04,820 I have disabled and re-enabled the module just to be safe. 115 00:09:04,820 --> 00:09:06,759 And I've logged out of the site just so 116 00:09:06,759 --> 00:09:10,105 that we can see what this looks like to a normal, anonymous user. 117 00:09:10,105 --> 00:09:13,389 So I will click on calculate sum, 118 00:09:13,389 --> 00:09:19,387 enter in two numbers to add, and click on the submit button. 119 00:09:19,387 --> 00:09:20,254 Oh, no! 120 00:09:20,254 --> 00:09:22,032 Access denied? 121 00:09:22,032 --> 00:09:24,285 Something is up here. 122 00:09:24,285 --> 00:09:27,453 Let's go take a look at our code and see what's going on. 123 00:09:30,170 --> 00:09:35,020 Well, first of all, I check the access callback. 124 00:09:35,020 --> 00:09:39,050 It's set to true, so really, anyone should be able to view the success page. 125 00:09:40,110 --> 00:09:43,920 But, we did forget one important attribute here. 126 00:09:43,920 --> 00:09:48,620 We have actually not named the page callback, so 127 00:09:48,620 --> 00:09:52,860 Drupal doesn't really know what to do when it gets to this URL. 128 00:09:52,860 --> 00:09:59,755 We have to actually tell it to run our function, sum_success, 129 00:09:59,755 --> 00:10:05,430 which I will put here in quotes, comma, save. 130 00:10:05,430 --> 00:10:06,510 Let's go back in. 131 00:10:07,590 --> 00:10:10,840 And we'll have to log in and clear our cache again. 132 00:10:13,140 --> 00:10:17,520 Once again we go to configuration, performance, 133 00:10:17,520 --> 00:10:21,430 clear all caches and then back to the modules page. 134 00:10:22,450 --> 00:10:29,682 And just to be safe, disable, save, and then re-enable the sum module. 135 00:10:34,098 --> 00:10:40,110 Now let's log out and go back to our home page and try it again. 136 00:10:41,590 --> 00:10:42,650 Calculate Sum. 137 00:10:44,080 --> 00:10:49,200 Lets add 5 and 6, calculate sum. 138 00:10:50,970 --> 00:10:53,750 Yay, you calculated a sum. 139 00:10:53,750 --> 00:10:55,550 The answer is 11. 140 00:10:55,550 --> 00:11:00,440 Congrats, you have now created your first Drupal site from scratch. 141 00:11:00,440 --> 00:11:04,310 You've added custom content to it, and stylized the layout. 142 00:11:04,310 --> 00:11:08,660 You've even written a couple of your own modules and used a custom form to gather 143 00:11:08,660 --> 00:11:13,830 information from a user, process it, then display a result on a new page. 144 00:11:13,830 --> 00:11:14,910 Cameron will be thrilled. 145 00:11:16,080 --> 00:11:21,560 But remember, we've only just scratched the surface of what Drupal has to offer. 146 00:11:21,560 --> 00:11:23,600 Keep digging and exploring. 147 00:11:23,600 --> 00:11:28,840 Drupal is completely open source and has a very friendly and helpful community. 148 00:11:28,840 --> 00:11:31,630 See the teachers notes to find more resources to find more resources 149 00:11:31,630 --> 00:11:32,640 for where to go next. 150 00:11:32,640 --> 00:11:33,336 Bye for now.