Drupal 8: Create a Simple Module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys akshay here in this video I'm going to be showing you guys how to create a simple hello world module in Drupal 8 so as you can see I already have a Drupal 8 site installed and we're going to get right into it so under the Drupal directory right here you see that there is a modules folder so going here and let's create a folder for our module name hello underscore world and this will serve as our machine name for the module in here we're going to we're going to have to define some parameters so this is going to be defined under hello underscore world info dot yml this is a file that's going to be written in the M language ruses which is basically many keys and values so we're going to set a name for our module to be hello world and this is going to be a type of module and give it a description of let's say say hello world and it's going to be under the package of custom and we're going to see this later in the admin panel and it's going to work with the eight point x core of Drupal so let's save this file and go into the extend page in Drupal and we should see our module listed here so here we go under the custom package we see hello world say hello world and our machine name is hello underscore world so let's enable this module great it's been enabled so module hello world has been enabled next we need to define the routes which will activate our module so to do this create a new file called hello underscore world dot routing dot yml now here we're going to have a key with our machine name so hello underscore world and next tab everything else and if the path hello slash world is accessed we're going to set some defaults for which controller should be accessed so now all classes in Drupal 8 are going to be namespace so we're going to write out this whole class name it's going to be Drupal slash hello your school world slash controller slash what the name of our class is going to be so this is going to be hello world controller colon colon hello so the hello method in the hello world controller is going to be called whenever the hello slash world path is accessed next we're going to have to set some requirements and this is basically permissions so to view this page you will need to have the access content permission now you may notice that some of these values have quotes and some don't well in the yema language it's not really necessary however you can use it if you want to and to be safe always use it in this case I'll just use it for access content to show that it works so save it and now let's move on to creating our controller so under the hello world create a new folder called SRC this is our source folder and where all the code to this module will live under here create another folder named controller this is where all our controllers are going to be now if you wanted to add forms to your module you would create a form folder and you're all your forms would go under that so in click controller we're going to create what we defined here hello world controller so this is exactly what we're going to name the file hello world controller dot PHP click enter open it up and let's start the PHP tag go down a few lines and create our class HelloWorld controller in here we have to find our class so next we need to namespace this as I talked about before Triple Eight use name spacing to standardize and specify which classes you are using so we need to put this under the name space of Drupal slash hello world slash controller copy this and let's type it up here age space paste and there we go we have set the namespace next we're going to have to create that hello method that we mentioned here so let's do that can do public function hello and this is going to return a render array so Drupal you can either return Symphony responses which triple is built on top of or you can return the rendered race which Drupal handle and add theming to and things like that so let's return an array and let's give it the keys of title which will equal hello world and another key of markup and this would speed the contents of um the page that we're going to see insulin should be this is some contents and let's save this and let's go to our route so Drupal slash hello slash world click enter and let's see our module whoa page not found okay so this is a pretty standard issue in Drupal with the cache so let's go ahead and clear the cache with something called Drupal console so Drupal console is an easy way to use your websites and manage things through the command line so to install this let's use these commands let's first download it then let's move it to our binary folder permission can I have excuse Kudo and then give it executable permissions and that's all I need to do so as you can see if you want to test this out a people list and you can see all the commands that you can used with Drupal console and the one specific we're going to use right now to rebuild the cache is Drupal router rebuild enter that and it should rebuild the router now if you don't have Drupal console you can obviously go to configuration scroll down to performance and click clear all caches however as you can see that's a way to long in Drupal router you build is much faster so I definitely recommend getting Drupal console and let's refresh our hello world page and we get access to nine okay so let's go back to our routing file and see what happened aha I misspelled requirements there we go requirements let's save that and let's rebuild your router one more time refresh this page and there you go hello world is our title and this is some content is our markup so there you go guys your first Drupal eight module hello world it was pretty easy and that's it so if you want to learn more about Drupal 8 modules go ahead and check out some of my other Drupal 8 tutorials and I'll see you guys next time
Info
Channel: Akshay Kalose
Views: 61,158
Rating: 4.8108749 out of 5
Keywords: Drupal, Hello World, Module, PHP, YAML, Content Management System, CMS
Id: 79zYcIoheCc
Channel Id: undefined
Length: 8min 15sec (495 seconds)
Published: Sat Jan 09 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.