๐Ÿš€ Developing a Drupal 8 module with a controller and custom template

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be creating the simplest module with controller and custom template will be defining the module in the info file we'll be creating the team function in the dot module file and we'll be creating the simple controller then we'll create the routing for the controller and the actual to attempt late so we have a simple when you a Drupal instance here and we'll switch to the code base under modules custom would create our module folder would call it Drupal up controller and with the name of it would create also a new file called Drupal up controller dot info dot yeah Mel and here we have to define the name of our module description I would have to say that type is module and the core is 8x that's our info file so the next thing we want to do is we want to create a dot module file where we are able to define [Music] who functions so let's go ahead and check in Google four through eight hoop team so that we know from the Drupal documentation which variables we have to pass to it so we copy this bit here and [Music] we go ahead and see a function and [Music] instead of hook would have to put the name of our module so we see drew up [Music] troller and here would have to return actually our custom team which we would be able to use in the controller that will define later so we see return and we are returning a array and in the array we could say the name of our team function so we'll call it article article list and for it we could say we want to pass these variables we could say we want to pass a list of articles which would be called items let's call it just items which would be an array and would also pass the title of our list dotto and this would be a simple string that's great we're ready with the team function so we save here we can go ahead and say that we are basically implementing the implementing who team it's always a good practice and afterwards what we want to do is we want to already define our controller so in order to do that would create a new folder in our module and it would be called SLC and then in the SRC folder we want to create a folder called controller and in the controller folder we would basically put the controller name that we would use also for the class and we would call it drew just call it let's just call it article controller time to put dot php' at the end that's great and basically here we have to define the name space and the name space would be starting with Drupal then the name of our module roller and name the folder controller great and in addition we could extend the controller base we don't have to do it but let's simply create a class hold article controller and in this class let's define a public function called page and basically this page is where we're going to be rendering the the page of our controller so would have to be returning so return array and in this area we could see team and we could say the team that we just created the team function that we just created it's article list and in addition we could pass also the variable so would pass once the items it would be an array of items article items would create it in a second and then we'll pass also the title let's call it call it our article list so regarding the items let's go ahead here and create an array of items an array of arrays with named article one let's say article to article to article 3 and article 4 that's great now we want to create the routing of this controller which would be in a file called the name of our module Drupal up controller dot routing dot yml that's great and here we're going to be starting with namespace which would be again the namespace is the name of our module group controller and we say article list this is something completely random and then the pot this would be the part that we would put in the web browser so we'll call it Drupal up and then articles then we can define defaults and here under default so you could say controller and basically the these paste parts to our controller so it would be drupal then grew up controller then controller then drupal and then it would be article controller this one here and then would have to put also the name of the method that we created there it is called page and we can put also title of the page it's our custom article list and in the end we can go ahead and declare also requirements and as requirements we could say their mission access content that's the very basic permission so we just activated for this page and the last thing really the last thing here we want to do is we want to create the actual template so we'll create a template folder and under the templates folder we'll create a new file which would be the name of the team that we declared so it's article but instead of underscore here would have to use dash and say list it's the name but instead of underscore - and would say dot HTML dot tweak that's great and we would want to put the title up and let's put it in h4 h4 and we would simply print the title that's the title variable that we're passing and for the items we could for each the items with for let's say our article in items since we're we're passing items and then we are for reaching them as article and here we can say our article dot title which would simply render the title of the article and here we say an end and for and just let's go ahead and put it in unordered list ul and each of the article titles into L eyes that's perfect we're saving it and let's go ahead and check if our module does what it's supposed to do so we go under extent and we'll search for our module here Drupal up controller with template we activate it that's perfect and if we now go under Drupal up articles we see our custom page that's the routing that we declare to declare this part in the routing we declared also the title in the routing the page title comes also from the routing and directly underneath this bit comes from the template that we declared it's the title variable that were passing to our team function and here are our article titles that are reached and at our render if you liked this video please check the links in the description like subscribe if you have any questions don't hesitate to ask them have a great day and thanks for watching
Info
Channel: Drupal Up - Drupal 8 Video Tutorials
Views: 36,749
Rating: 4.8930483 out of 5
Keywords: drupal, drupal8, drupal tutorial, drupal 8 module, drupal controller, drupal custom template for a module, drupal custom page, routing
Id: mHTuXXfBs7Y
Channel Id: undefined
Length: 15min 18sec (918 seconds)
Published: Wed Feb 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.