Drupal 8 is just around the corner and it's going to be a revolution, not an evolution like Drupal 7 was compared to Drupal 6, as it has a completely different architecture and coding methodology. I'm convinced that these major changes were necessary to professionalize and modernize the CMS. I think the shift to object-orientation will be of interest to developers and will improve the quality of the software in general.
I am very grateful to the Drupal core developers for their bravery and work on all of this! And to help you get familiar with this new version I will show you how to create a module in Drupal 8.
WARNING: Please note that because Drupal 8 is still in development at the time of writing, some parts of the code may not work for you, so please let us know if this happens, so we can fix it immediately.
Well let's start:
Naming and positioning your module in Drupal 8
The first thing we must do is create a folder with the machine name of our module. This is used in the names of various files and functions in your module, and is used by the Drupal core to refer to your module. Remember this name:
- It must start with a letter.
- It may contain lowercase letters and underscores.
- Must be unique. Your module cannot have the same name as another module, theme, or installation profile that you will use on your site.
Important Note: Be sure not to use capital letters in your module name or Drupal will not recognize your hook implementations .
We then select " hello " for its name.
Previous versions of Drupal had core modules in /modules however in Drupal 8, this folder is now for modules you develop yourself. All files including core modules and libraries are now in the /core directory. In Drupal 8, you can still use the Drupal 7/6 best practice of putting your custom modules and those downloaded from Drupal.org (contrib) in /sites/all/modules but putting it in the /modules directory has the same effect.
Giving information to drupal 8 about your module.
Inside this folder we create a hello.info.yml file which is similar to the .info in previous versions of Drupal:
The first three lines are mainly used in the administration user interface by allowing users to install or uninstall the module. The name and description keys are required and indicate the text displayed on the module management page, and the package key allows you to group all modules that define this key; Drupal 8 core, for example, uses the core package to group all of its modules, just as you can use the custom package to group all of your custom modules together making them easier to locate.
The type key, which is new in Drupal 8, is required and indicates the type of extension, for example, module, theme, or profile.
The core key is also mandatory and specifies which version of Drupal your module is compatible with.
And that's it, just one file. Now you can go to the Extend page, find the module you just created, and enable it.
So far it doesn't have any functionality, so we're going to make it show us a message when visiting a specific page.
Defining our route
As you may have read, several Symfony components have been added to Drupal 8, including Routing, so the way of defining a route in Drupal 8 changes, now we must create a file following the following pattern: {your_module_name}. routing.yml in our case hello.routing.yml:
Important note : To indent or indent the beginning of each line in these YAML files, it is prohibited to use tabs, for this we must use spaces, otherwise our code would cause problems.
hello.hello:
This line is the name of our route. It is usually constructed as follows: {your_module_name}.{action} This can be used in other parts of the code, for example, to generate a URL based on a route.
path: '/hello'
Here we define the URL of the path, so that in our browser, we can see it pointing to www.example.com/hello.
Under the defaults we have:
_controller: '\Drupal\hello\Controller\HelloController::hello'
Which refers to the method to execute when accessing the above URL. It takes the form \Drupal\{module_name}\{controller_folder}\{class_name}::{method}
_title: 'Is anyone here?'
Title that the page will have.
Under requirements we have:
_permission: 'access content'
Permissions required to access this page. In this case, any user will be able to access it.
You can check this documentation page for more options to include in this routing file.
Creating our first controller
Now we must create the controller for this path, it will return the message “Hello from Drupal 8”.
Inside our module folder, we will create a subfolder called src.
This stands for “source”, and it's part of the PSR-4 standard so our controller class will be loaded automatically.
Inside the “src” folder we create another folder called Controller.
Once again, it is part of the standard. Inside the “Controller” folder, we create a file called HelloController.php, with the following structure:
Now inside the HelloController.php file we will write this:
That is all we need to do to display a text on a page. At the beginning of the file we specify the namespace and below we declare the class.
Inside the HelloController class, we only have the hello method that returns a render array similar to how it was done in Drupal 7, nothing else.
All we have to do is install the module or, if we have done it before, clear cache (“drush cr” or go to Configuration->Performance->Clear all caches) and go to our page that we just created: http://www.example.com/hello:
Adding to our menu
In Drupal 7 when we implemented hook_menu, we could also register a shortcut in the menus. This also, is no longer managed by the hook_menu, to undertake this we must do it via a YAML file.
We will then see how to display a link in our Tools menu.
We create a hello.links.menu.yml file in the root of our module:
With the following content:
As you will notice in the first line we have our path name.
Then we will see the title, the description (when we hover over the link), the menu name (where it will be positioned), and the name of the route we want to appear in that menu. If you notice, it is the same route assigned in our hello.routing.yml file. If we omit the menu_name key, Drupal will automatically position our link in the Tools menu. We have only put it here to explain the key.
Next we clear cache and it should show in our Tools menu:
Well that's all, we have created a fairly basic module in Drupal 8, in the next installments we will go a little deeper, so stay tuned.