Drupal 7 Module Development Basics - Daily Dose of Drupal 16

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is Shane with another episode of the daily dose of Drupal this is episode 16 and this is brought to you by code karate comm today we're going to do something a little bit different and we're going to look at some module development basics I'm going to go through the very basics of building your own module and in this example all we're going to do is build a simple module from scratch that outputs a custom block right in here somewhere this custom block right now will just be static content but you'll learn going through this video the basics of what what is involved in creating a drupal module and how it can be used to extend the functionality of your drupal website and we're doing this in drupal 7 I have a Drupal 7 test site up right now with just some basic content and a few content types and other modules installed but one of the things that you're first going to have to do when creating a drupal 7 module is you're going to have to come in to your modules directory which i have up here and we were we're going to create a new folder i'm just going to call this my block because we're just going to create a simple block in this module so I now have a folder called my block and generally the folder name is the same as the whatever module file name you're going to have doesn't have to be and we're going to then create an empty file called my block dot module and we're also going to create an empty file called my black dot info the info file is basically you can think of it as a configuration file it tells Drupal about your module and it gives Drupal the information it needs to know in order to install the module onto the Drupal website everything in the dot module file is the code that actually runs that module and you can if you look in other modules that you may have downloaded there's there can be a ton of other files in here but this is the basic structure of what you'll need to create a module and we will go ahead and open this up now we just have these empty files here go ahead and I'll open these up and we'll start with the info file the first thing you need is a name I'm just going to call it my block module you'll that you can then add a description if you want the core is the version of Drupal you are using in this case it's a seven Drupal seven site you can specify a minimum PHP version that's required for your module in this case I'll put five to four you can specify a version of your module so for instance you may start at 7 X - 1 0 and the next time you make a change you can change that to 7 X - 1.1 and go up incremental e from there you can also add dependencies for other modules that you may need so let's say we also needed the date module we could put that there just like that or if you needed the CTools module or the views module you would add that dependency just like that I'll go ahead and just leave that out because it's not really required for this very simple module so I will save that and now I have an info file for the my block dot module file we're just going to start out with a simple file header and this is just a comment that I like to do at the top of my files that basically can be used with certain different documentation systems like doxygen or other things and it can actually load in some information about your module if you use a system like that but you can also add a description up here or just leave it out if you don't need it now once we got this far the next step is learning how to interact with the drupal api and drupal is set up to be extremely flexible it's set up with something called hooks which are basically functions that you can implement in your module to interact with the Drupal website so this could be interacting by adding a page on your site using a hook called hook menu or it could be you're running something periodically on your site maybe it's a process that has to run every so often you could use something called hook cron for that but one of the things that's going to be invaluable along with just the standard Drupal documentation which you can find on drupal.org is the API drupelet org website and I've pulled up two hooks here that we're going to be using that are that's going to let us create this custom block the first one is hook block info you can read about it here but it allows you to define blocks within your module and that is essentially what we need to do is define a block within our new module so you can see down here there's something there's always some example code when you first start you maybe want to just grab this code copy it and paste it in and the first thing we're going to do with this hook block info function has changed the word hook to my block because that is the name of your module it's my block module so you'll change hook to my block in this example in this my block block info function there's to block examples that are defined the first one is called syndicate tells you the information about it and then it tells you that it's going to use a cache called Drupal no cache which in this case isn't going to cache the block in this one there is no cache so Drupal cache per role is assumed which means that it's big the caching is based on the user's role on the site will go ahead and not worry about caching now this is just a real simple example so we'll get rid of this and we're going to call this my block and for the info we will do my custom block it will save this and now we should have everything we need to at least install the module and get started so we'll come over to the modules page scroll down into the other section goes through the other fields sent and we'll click on my block module which is what we created here's the description here's the version that we said we wanted to use and you'll see that there's a whole bunch of different field sets for different types of modules and you can define where your module falls using a package you're defining the package in the info file in this case we didn't define one so it defaults to falling in the other field set on the modules page we'll go ahead and save this and now that that is saved we'll come over to structure click on blocks if we scroll all the way down to the bottom we should be able to see my custom block somewhere in there here it is we're going to go ahead and in my case I'm going to add it to sidebar first and I'll go ahead and I'll drop it all the way at the top and I will click Save now if I go to the home page I still don't see anything that's because we just defined the block we haven't actually told the block what to display yet so in order to display what should be viewed for that block you need to define hook block view you can of course read the information here about hook block view but what I'm going to do is I'm going to copy this in so I'm going to take the example code and I will paste it right into our dot module file of course change hook to the name of our module so call it my block and you'll see there's one parameter here and the parameter is Delta and what that's going to do is is this function is going to be called and the Delta value is going to be passed in so if I had three or four different blocks one called my block one my block to my block three however I wanted to call that then when my block one needed to be rendered it would pass in with a delta value of my block one so in this case you'll see we do a switch statement on that Delta value all we care about is my block in this case we don't really care about anything else because our module doesn't define any others but if you had other blocks you would of course add additional case statements to this switch statement so if it's my block we'll have a subject of call it this is my custom block for the subject and what you'll see here is for the content this is an array and what this is called as a renderable array basically Drupal knows how to render this what Drupal is going to do is it's going to call a specific theme function you can give it a specific title but in this example we don't really want to go through all of the details of a renderable array so we'll leave that for another episode you can also go ahead and just define straight a string of HTML here the preferred way is of course to use that renderable array but for this simple example we're just going to go through it with just HTML so if we save that now and we come over to our page refresh you'll see our custom block now shows up one thing to keep in mind with this this example is just creating a basic content within the block so if you if that's all you needed to do you could easily come into structure in the blocks and add a custom block this way but the reason I'm going through it building a custom module is because there may be times when you need to be able to deploy the block on multiple sites and having it a module makes it easier or being able to have the block in code is or there may be more things like a forum or a view or something else inside of the block that you need to actually customize in this case of course we're just going through the simple example one thing I'm going to do is I'm going to add some simple comments here generally when you're building a module file you want to tell anyone who'd be reading it what hook this implements so if you're reading it later or someone else is reading it or you need to quickly search through your module you will be able to determine which hook functions you're implementing so that's generally the standard it just implements whatever the function name for the hook you're implementing in the end it with a period so we'll save that and I'll show you now that you can add some HTML in here now if I save that and refresh you can see it's now changed you can see that they show a Liz of course taking effect and we have a custom lock that we can now turn on on any site and drop it into any different section inside of our blocks that we want and everything will work out great you've created your first Drupal module so that's all there is this time now next time we'll be going over a different topic hopefully you learned a little bit about building or in developing a custom module in Drupal and how you can create a custom block within that module until next time this is Shane with daily dose of drupal you can follow me on twitter @ sm tomas 3 thanks for watching
Info
Channel: Code Karate
Views: 42,317
Rating: 4.9330544 out of 5
Keywords: Drupal, Module development, API, Hooks, Daily Dose of Drupal DDoD
Id: 6DQrFE2ok0U
Channel Id: undefined
Length: 13min 21sec (801 seconds)
Published: Wed Sep 26 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.