

_controller: '\Drupal\custom_page\Controller\CustomPageController::CustomPage' custom_page.custom_page_controller_CustomPage:
#Drupal devel generate content template code
The code below is a simple route to a custom page that I have created for our custom module.
#Drupal devel generate content template full
You start off by setting a route name, typically before the full stop is the module name, the path is the url that user should visit, the _controller sets the namespace of the controller and the method that we should call when the page is requested, we can set a title using the _title key and we can also restrict the access to the page, for the purposes of this tutorial, we are using the custom permission that we added earlier and we call the permission using the machine name that we set when we added the permission. routing.yml goes in the root directory for the module at the same level as the info.yml file. To add a page, we need two files, one is a routing file that notifies Drupal that we want to add a route that it should know about. If the route is found, then Drupal attempts to show the content that has been defined for the route, if unsuccessful then we show Drupal's 404 page. When Drupal receives a request, it tries to match the requested path to a route it knows about. For example, the default front page, '/node' is a route. If you now look at Drupals permissions page, you may need to clear Drupals cache, but a section for the newly installed module (in our case 'custom page' and just like in the screenshot below) will be listed showing the permission that we have just added, allowing us to assign to other user roles.Ī route is a path which is defined for Drupal to return some sort of content on. access custom page:ĭescription: 'Allows users to access the custom page' permissions.yml will look a bit like the below.

You then set a title and an optional description, so that the new permission can be found on the permissions page. I prefer to keep these names as simple as possible but descriptive enough. In the permissions yml file, you start off with adding a machine name for the permission that we will use when we add the permission to the route. permissions.yml and this file lives in the root directory of the module at the same level as the modules info.yml file. The name of which will be in the format of. If you want to restrict parts of your module so that only certain users can access the page, but you don't want to hard code the logic the you can add a permissions configuration file to your custom module.

'' įor the purposes of this module at the moment, we are going to leave the contents of the .module file as shown above. When using Drupal console to generate the module, it adds an implementation of the Drupal hook called hook_help, which can be used to provide help information to the help pages within Drupal. module file is to store any functions needed to provide some of the functionality that you wish the module to provide, such as using any of Drupals hooks that can add new or alter existing functionality or manipulate data/ output.ĭrupal 8/9 utilises a mix of procedural and object oriented programming methods. If you clear Drupal's cache and go to the 'Extend' page, you should see a group called 'Custom' that lists one module called 'Custom page', just like what can be seen in the image below. dependencies:īelow is the contents of the info.yml file that we have created for this tutorial, incorporating what we have mentioned above. For example, below is the dependencies set in Drupal cores node module, which requires the Drupal core text module to function. If dependencies are specified, then these would be listed in the. So we now have to add the follow line to the info.yml file. So setting the core version here is no longer needed really. Setting the core version requirement using the core key is now only needed for versions of Drupal 8 before 8.7.7, versions after 8.7.7 require a different key to be set to let Drupal know if the module can be used on Drupal 8 and/or 9. Likewise you can also give the module a package name, this is used to group modules together so that they can be found more easily on Drupal's extend page. Optionally, you can give the module a description. The first thing you need to set is a name for the module.įollowed by specifying that we are creating a module, this is done using the type key, so that Drupal knows what you are creating. info.yml, this goes in the folder created for the custom module. In order for Drupal to recognise that we have added a module to the custom modules folder is to add an info.yml file, with the name of.
