So, you could not resist the temptation to try out the much-praised Drupal 8! You've installed it, you've already “played” with its core modules, its contributed modules and that's it? Don't you think it's about time you leveled up your Drupal 8 skills?
The next step to take is learning how to team up your own skills and knowledge with Drupal 8's full potential for putting together some amazing custom-made modules. Modules that would instantly add extra value to the sites you'll build.
Don't let the “rumors” stating that the whole custom modules building process is so different, and therefore more challenging, from Drupal 7, that it's lengthier and way too discouraging.
We're here to show you that it takes just 8 quick and easy steps for you to create new modules (to meet your sites' specific needs) in Drupal 8! Walk us through all these steps:
1. File Structure
It's under modules folder in the root directory that you should keep both your contributed and your custom modules in Drupal 8:
So, go ahead and get your first custom module started by creating a folder in your Drupal installation at the path: sites/all/modules/custom/sa_module.
Keep in mind: If you're configuring multiple sites, you need to use modules specifically for each one of them:
2. Create the .install file
A Drupal schema (representing one or more tables and their related keys and indexes) is defined by hook_schema(). You'll find it in modulename.install file. hook_schema()
and it should return an array mapping ‘tablename’ => array (table definition) for one of the tables defined by the module.
3. Create the .info.yml file
That's right, you have a “info.yml” file extention in Drupal 8 (replacing the “.info file” that you got used to using Drupal 7). It applies to profiles modules and themes, too.
So, that being said, you now need to create your .info.yml file (i.e. sa_module.info.yml), thus letting Drupal know about your new custom module. Your file will look something like this:
name: Drupal 8 custom module example
description: 'Example for Drupal 8 modules.'
Follow this path for enabling your module: https://goo.gl/ADSWyt OR Click on Extend from Menu.
4. Create the .routing.yml File
When it comes to handling routing in Drupal, you'll be using Symfony 2 components. The routing file that you'll create will help you navigate into Drupal using the method of a controller class (meaning that you'll be specifying different controller actions).
You'll be writing the path in routing.yml file and the resulting file will look something like this:
_title: 'My first page in Drupal8'
: 'access content'
1. The first line is the route
(a symfony component mapping an HTTP request to a set of configuration variables).
Define your route as a module_name.route_name ‘module_name => sa_module’ ‘route_name => list’.
2. The second line is the path
: you'll need to specify the path that this route should register to, in short: the path (of the module) where the users will be redirected. See the leading forward slash “\” in the URL to the route from our example.
3. The third line shows your defaults: you get to specify several things here. For instance, in our above example we have 2 defaults:
_contoller: referencing a method on the ExampleController class
_title: the default page title
You, could, for instance, have "_form"
define classes under defaults, as well, helping you define the forms included in your custom module (for example EditForms, DeleteForms, AddForms)
4. The fourth line displays requirements
: here you get to specify the type of permissions to be granted to your users for accessing specific pages on your website: “add”, “edit”, “delete” and “access” for instance.
5: Create the .module file
Unlike in Drupal 7, where the hook menu() defines page callback functions, in Drupal 8 it defines menu items exclusively.
Make sure that the path and the route in which in your example.module matches the ones written in your example_module.routing.yml.
Let's have a look at our above-example, for instance: there the route 'route' => 'example.my_page' in example.module should be identical to example.my_page: in example.routing.yml
6: Create Controller Class
First, a few words about “controller”:
- it may contain multiple types of arbitrary logic that your site needs for rendering content on a certain page
- it's a php function that processes the HTTP request and returns a response
Create a “modules/custom/sa_module/src/Controller" folder and then (within the same folder) a file named AdminController.php.
7. Create Model Class and Forms
Once you've set up your AdminController, you need to introduce a model class (i.e. SaUserInfo.php) in order to link it to your database. It's this model class that you'll create now that will help you set up methods like add(), delete(), update() etc.
8. Create Menu on Admin
One more step and you're ready to launch your very first custom Drupal 8 module right from the Admin Panel ! Create a file sa_module.links.menu.yml, which will set Menu at the Top.
And this is it! 8 quick and easy steps for building your first custom module in Drupal 8! Give it a try!