Core points
.info.yml
files. The custom and contribution module folders are now directly under the root directory modules/
, and the core code is moved to a separate core/
folder. hook_menu()
function in Drupal 7, leveraging the Symfony2 component. This involves defining the route as a configuration and handling the callback function in the controller. The module's routing file demo.routing.yml
is created in the module root folder. hook_menu()
, but is declared as a configuration in the yml file. Create a file named demo.menu_links.yml
in the module root directory to define the menu link and its location in the site's existing menu. Note that since Drupal 8 is still in development at the time of writing, some of the code may be outdated. Please refer to the code base I tried to update the sample code and make it compatible with the latest Drupal 8 version.
Drupal 8 introduces many changes, seeking to align it with other modern PHP frameworks. This means that the old PHP 4 style procedural programming was largely replaced by object-oriented architecture. To achieve this, under the Proudly Found Elsewhere program, Drupal 8 contains code that is not specifically developed for Drupal.
One of the most important additions to Drupal is the Symfony component, which has two major impacts on Drupal developers. First, it has the potential to significantly increase the number of developers who want to develop for Drupal now. Second, it has left some concerns to some Drupal 7 developers who lack modern PHP practice experience. But that's OK, we're all learning, the experience we've learned from frameworks like Symfony (and hopefully Drupal 8) will be easily extended and applied to other PHP frameworks.
Meanwhile, Drupal 8 is in the late stages of its release cycle, with the current version at the time of writing alpha11. We will use this version to showcase some basic changes in module development that Drupal 7 developers will first encounter and should be familiar with. I set up a Git codebase where you can find the code I wrote in this series, and if you prefer, you can follow and learn like this.
The first thing we need to focus on is defining the necessary folder structures and files so that Drupal 8 can understand our new modules. In Drupal 7, we need to create at least two files (.info
and .module
), but in Drupal 8, the YAML version of the former is enough. Yes, the .info
file is now replaced by the .info.yml
file, containing similar data, but with different structures.
Another big change is that the custom and contribution module folders now go directly to the root directory modules/
folder. This is because all core code has been moved into its own separate core/
folder. Of course, in the modules/
directory, it is recommended to separate modules into custom and contribution as in Drupal 7.
Let's create a module called demo (very original) and put it in the modules/custom/
directory. As I mentioned, within this newly created demo/
folder, we first need only a demo.info.yml
file containing the following required content:
name: Drupal 8 Demo module description: 'Demo module for Drupal 8 alpha11' type: module core: 8.x
You should be familiar with three of the four key-value pairs (name, description, and core). type is also a requirement now, as you can also create yml files for the theme. Another important thing to note is that spaces in the yml file make sense and use the correct indentation to organize the data into an array-like structure.
You can view this document page for additional key-value pairs that can be added to the module .info.yml
file, and instructions for announcing changes to this format.
It's like this, a file. You can now navigate to the Extensions page, locate the Demo module and enable it.
As I mentioned, before we enable the module, we no longer need to create a .module
file. Architecturally speaking, the size of the .module
file will be greatly reduced, as most of the business logic will be moved to the service classes, controllers, and plugins, but we'll see some of them later.
hook_menu()
is probably the most implemented hook because it is used to define the paths of Drupal and connect these paths to the callback function. It is also responsible for creating menu links and many other content. hook_menu()
because we use Symfony2 components heavily to handle routing. This involves defining the route as a configuration and processing the callback function in the controller (a function of the controller class). Let's see how to do this by creating a simple page that outputs classic Hello world! . hook_menu()
for our module. This file is located in the module root folder (adjacent to demo.routing.yml
). In this file, we can have the following (simple) route definition: demo.info.yml
name: Drupal 8 Demo module description: 'Demo module for Drupal 8 alpha11' type: module core: 8.x
The first line marks the beginning of creating a new route called demo for the module demo (the first is the module name and the second is the route name). Under path, we specify the path to register for this route. Under defaults, we have two things: the default page title (_title
) and _content
, which references a function on the DemoController class. Under requirements, we specify the permissions that the user needs to have in order to view the page. You should consult this documentation page for more options this routing file can have.
Now, let's create our first controller called DemoController, which will call a function called demo() when the user requests this page.
In the module directory, create a folder named src/ and create a folder named Controller/ in it. This will be where the controller class is stored. Continue to create the first one: DemoController.php.
The controller and the other classes we'll see later into the src/ folder are part of the PSR-4 standard. Initially we had to create a larger folder structure (PSR-0 standard), but now there is a transition phase where both work. So if you still see the code placed in a folder called lib/ , it is PSR-0.
In our DemoController.php file, we can now declare our class:
demo.demo: path: '/demo' defaults: _content: '\Drupal\demo\Controller\DemoController::demo' _title: 'Demo' requirements: _permission: 'access content'
This is the easiest and least amount of thing to do to display something on the page. At the top, we specify the class namespace, below we declare the class.
In the DemoController class, we only have the demo() function, which returns a renderable array similar to Drupal 7. It's no big deal. All we have to do now is clear the cache and navigate to the https://www.php.cn/link/1a4a5f89e71e4bb9973355c964a950b4 Drupal page with Hello World.
In Drupal 7, when we implement hook_menu()
, we can also add the registered path to the menu to display the menu link on the site. Again, this is no longer handled with this hook, but instead defines the menu link as a configuration using the yml file.
Let's see how to create a menu link that appears under the managed "Structure" menu. First, we need to create a file named demo.menu_links.yml
in the root directory of the module. In this yml file, we will define the menu link and its location in the existing menu of the site. To achieve the goals we set, we need the following:
<?php /** * @file * Contains \Drupal\demo\Controller\DemoController. */ namespace Drupal\demo\Controller; /** * DemoController. */ class DemoController { /** * Generates an example page. */ public function demo() { return array( '#markup' => t('Hello World!'), ); } }
We once again have an indent-based yml structure where we first define the machine name (demo) of the menu link for the module demo (like we did with routing). Next we have the link title and description, followed by the parent of this link (where it should be placed) and what route it should use.
The value ofparent is the parent menu link (attach its module), and to find it you need to do some digging in the *.menu_links.yml file. I know that the "structure" link is defined in the core system module, so by looking at the system.menu_links.yml file I can determine the name of this link.
route_name is the name of the machine of the route we want to use for this link. We defined ours before. With this you can clear the cache and navigate to the https://www.php.cn/link/6c2665d7c3ed1e5bfd8ba600f026eb55 demo/ path. good.
In this article, we begin to explore module development in Drupal 8. At this stage (alpha11 version), it's time to start learning how to use the new API and port contribution modules. To do this, I am writing my exploration of this new exciting framework (Drupal 8) so that we can all learn about these changes and get into the work immediately when the release day arrives.
First, we learned some basics: how to start the Drupal 8 module (files, folder structures, etc.) and compare it to Drupal 7. We also learned how to define a route and a controller class, and how to call a function through this route. Finally, we see how to create a menu link that uses the route we defined.In the next tutorial, we will continue to build this module and learn about some other cool new features that Drupal 8 uses. We'll learn how to create blocks and how to use forms and configure the system. See you then.
FAQs about building Drupal 8 modules (FAQ)
file (providing metadata about the module), a .info.yml
file (including PHP code), and other optional files such as .module
, .css
, .js
, etc., for attaching Function. .twig
Files are mandatory, which define the name, description, package, type, core, and dependencies of the module. .info.yml
file to define routes with unique names, paths, default values, and requirements. The default value usually specifies the controller class responsible for handling the request. routing.yml
file and maps the URL to the controller. The controller is a PHP class that returns a rendered array for the page content. The controller class should be placed in the routing.yml
directory of the module. src/Controller
Adding a menu link to your custom page involves creating a links.menu.yml
file in your module. This file defines a menu link whose properties include title, description, parent, route name, and weight. The route name should match the route name defined in the routing.yml
file.
Creating a form in Drupal 8 involves creating a form class that extends the FormBase
class and implements FormInterface
. This class defines form elements, validation, and submission processing. The form can be displayed by returning it from the controller.
Creating a block in Drupal 8 involves creating a block class that extends the BlockBase
class. This class defines block content and other properties. Blocks can be placed on the page through the block layout interface.
Creating a configuration form involves creating a form class that extends the ConfigFormBase
class and implements FormInterface
. This class defines form elements and handles the storage and loading of configuration data.
Creating an administrative page involves defining a route under /admin
and a controller that returns the page contents. Pages can be added to the admin menu by defining a menu link with a parent under system.admin
.
Creating a topic in Drupal 8 involves creating a .info.yml
file that defines the name, description, type, core, and basic topics. Other .twig
, .css
and .js
files can be added to set themes for each element.
Creating a custom field type involves creating a field type class that extends the FieldTypePluginBase
class. This class defines field properties and methods for storing, displaying, and form processing. Field types can be used for content types, users, comments, and other entities.
The above is the detailed content of Build a Drupal 8 Module: Routing, Controllers and Menu Links. For more information, please follow other related articles on the PHP Chinese website!