DRUPAL8 Module Development Routing, Controller and menu link tutorial

Source: Internet
Author: User
Tags php file drupal

The biggest change in the

is the introduction of the Symfony framework, which has two significant impacts for developers

First, this could dramatically increase the number of Drupal developers, such as the traditional "high-end program apes" that once considered themselves masters, disdain for process development, Now the Drupal8, also face to the image, and with the new framework, they have no reason to refuse to join the Drupal developers of the ranks of

Second, the new framework of the core changes will give now those who do not have much PHP modern framework development experience many fear, They worry that they can't adapt to the big changes in the way they develop, but it doesn't matter, we all need to learn again, learn the symfony framework, and hopefully Drupal8, like other PHP frameworks, they are easy to expand and we need to give ourselves confidence.

At the moment, Drupal 8 is still in its release cycle, the latest version is Alpha12, and we will be basing this release on some of the basic changes that Drupal 7 developers will encounter first, and we should familiarize ourselves with these basic changes.

How do I create a module?

The first thing we do is to determine the necessary file and folder structure to tell Drupal8 to identify our new module. In Drupal 7, we have to create at least 2 files (. info and. module files), but in Drupal 8, the. info file is replaced with the. INFO.YML, the content data information in the file is the same, but the format is slightly different. Another major change for

is that the location of the custom module and Third-party Contribution module folder is now placed in the root directory of modules/, because all core code has been moved to its own folder Core/, of course, in the Modules folder under the root directory, We still encourage you, like Drupal 7, to create custom and contrib subfolders to store your customized modules and third party modules downloaded from your website.

Let's go ahead and let's learn to create a module called demo (very simple) and put it under the modules/custom/folder, and as I mentioned earlier, we need to create a new demo/folder in this newly created Demo.info.yml the file and enter the following code in the file:

  code is as follows copy code
name:drupal 8 Demo Module
Description: ' Demo module for Drupal 8 alpha12 '
Type:module
core:8.x



Most of these you should be very familiar with it (name, description and CORE), the Type field is also a must now, its value can be module,theme,theme_ Engine defines whether this file type is a module, a theme or a template engine, and one important detail to note is that note the spaces in the Yml file, and the correct indentation can organize the data like an array.

You can view the other available key|value in this document (https://www.drupal.org/node/2000204), and then open the. info.yml file under a module folder, trying to change Key|value, Here is the official change note (https://www.drupal.org/node/1935708)

Now, as long as you have this one file is enough, you can go to the navigation menu of the Extend (extended) page, find the demo module, and enable it.

As I mentioned earlier, I do not need to create another. module file, and Drupal 8 already recognizes this module because, in Drupal 8, the. module file is no longer a required file. Because most of the business logic code is now transferred to service classes, controllers and Plug-ins, we will see this later.

What is a route? What happens when you call Hook_menu ()? How does its callback work?


in Drupal 7, Hook_menu () is probably the best hook to implement these functional requirements, because it is used to set paths and connect these paths and callback functions, and it is also used to create menu links and other things

In Drupal 8, we no longer need hook_menu (), because we use SYMFONY2 components to handle routing, which involves defining a routing configuration and handling a callback (Controller class) in the controller, let's actually look at how it works , let's create a simple page that prints out the timeless classics of the program World: Hello world!

First, we need to create a routing file for our module: demo.routing.yml, this file is also placed in the demo module root directory, in this file, we can have the following (simple) routing settings:

  code is as follows copy code

Demo.demo:
  Path: '/demo '
  defaults:
    _content: ' Drupaldemocontrollerdemocontroller:: Demo '
    _title: ' demo '
  requirements:
    _permission: ' Access Content '

The first line of code in




is the beginning of a new route, the name of which is called demo (original author Real Egg pain, you can not take a different route name, must and the module name, you let my little friend misunderstanding how to do? , the first is the demo is the module name, and the second demo is the routing name.

The second line of path, in which we specify the registration path for this route.

The third line defaults the default setting, and we set two things: the default page title (_title) and one method referencing a Democontroller class (_content)

Line fourth requirements requirements, We have specified what permissions the user needs to be able to view the page.

You can check this document (https://www.drupal.org/node/2092643) to see what options you can set in this routing file.

Now, let's create the first controller called Democontroller, and it contains a method called demo () that calls this method when the user accesses the page.

in the module directory, create a SRC folder and a controller folder, which is where we store the controller class, OK, go to the Controller folder, create the file: democontroller.php.

The location of the controller, as we will see, putting other classes into the SRC folder is part of the PSR-4 standard, before we had a deeper and larger folder structure (PSR-0 standard), but now there is a transition phase, Both of these standard folder structure paths work, so if you see that the code class is still in the lib/folder, don't be surprised, that's the PSR-0 standard.

In the democontroller.php file, we can declare our class:

  code is as follows copy code

<?php
/**
 * @file
 * Contains drupaldemocontrollerdemocontroller.
 */

Namespace Drupaldemocontroller;

/**
 * democontroller.
 */
class Democontroller {
 /**
   * genera TES An example page.
   */
  Public Function demo () {
    return array (
&NBSP;&NBSP;&NBSP;&NBSP ;  ' #markup ' => t (' Hello world! '),
   );
 }     
}


This is what we want to display "Hello world!" on a page The simplest example of a structure, at the top, we specify the namespace of the class, and in the following we declare our class

in the Democontroller class, we just need the demo () method to return a rendering array like the previous Drupal 7. Without a lot of stuff, now all we have to do is empty the cache and enter the address in the browser: Http://example.com/demo, and then we should be able to see a page with the Hello world!!

How do I create a menu link?

in Drupal 7, in order for the menu link to appear on the site, we can add a path that has already been registered while implementing Hook_menu (), but in Drupal8, this no longer needs to be handled with hooks, We can use a yml file as a configuration to declare a menu link

Let's take a look at how to create a menu link to display under the structure (structure) management menu, and first, we create a demo.menu_ under our module root directory Links.yml file, in this file, we will set the menu link and the location on the existing Site menu, in order to achieve what we want to do, we need to enter the following code in the file:

  code is as follows copy code

Demo.demo:
Title:demo Link
Description: ' This is a Demo link '
parent:system.admin_structure
route_name:demo.demo< /td>



This is another YML structure file based on indentation.

The first line we define the machine name of the module's menu link (just as it was previously defined), and then we set the title of the link, the description, the parent menu path (as a submenu under the parent menu), and the route name associated with the menu. The value of the

parent menu option is the parent menu path (attached to its owning module), and you want to find this parent menu link, you need to look for some similar *.menu_links.yml file, because the value here is System.admin_structure, So I can tell that it is definitely set by the core system module, so you can tell that the parent menu link exists in the System.menu_links.yml file. The

Route_name is the routing name that we want to associate with this menu (which was previously created), when you empty the cache, and then enter the browser address: http://example.com/admin/ Structure you should be able to see a new menu title and description, and this link is linked to Demo/path, how's that good?
Conclusion

In this article, we begin to discuss the module development of Drupal 8, at this stage (Alpha12 release), is to start learning how to use the new API interface to develop the contribution module, for this reason, I will be together to exit the traditional framework of thinking, Explore the changes in Drupal 8 with a new mindset.

First, we learned some basics, how to start a Drupal8 module (file and folder structure, etc.), all of which are different from Drupal 7, and we see how to set the route and a controller class method to call a routing address, and finally, We've seen how to create a menu link that uses the route we've defined.

In the next tutorial, we'll continue to build and refine this module and look at some of the other cool things in Drupal8, and we'll see how we create blocks and how to work with forms and configuration management systems, see next time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.